trace_ksym.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * trace_ksym.c - Kernel Symbol Tracer
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2009
  19. */
  20. #include <linux/kallsyms.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/module.h>
  25. #include <linux/fs.h>
  26. #include "trace_output.h"
  27. #include "trace_stat.h"
  28. #include "trace.h"
  29. /* For now, let us restrict the no. of symbols traced simultaneously to number
  30. * of available hardware breakpoint registers.
  31. */
  32. #define KSYM_TRACER_MAX HBP_NUM
  33. #define KSYM_TRACER_OP_LEN 3 /* rw- */
  34. struct trace_ksym {
  35. struct hw_breakpoint *ksym_hbp;
  36. unsigned long ksym_addr;
  37. #ifdef CONFIG_PROFILE_KSYM_TRACER
  38. unsigned long counter;
  39. #endif
  40. struct hlist_node ksym_hlist;
  41. };
  42. static struct trace_array *ksym_trace_array;
  43. static unsigned int ksym_filter_entry_count;
  44. static unsigned int ksym_tracing_enabled;
  45. static HLIST_HEAD(ksym_filter_head);
  46. static DEFINE_MUTEX(ksym_tracer_mutex);
  47. #ifdef CONFIG_PROFILE_KSYM_TRACER
  48. #define MAX_UL_INT 0xffffffff
  49. void ksym_collect_stats(unsigned long hbp_hit_addr)
  50. {
  51. struct hlist_node *node;
  52. struct trace_ksym *entry;
  53. rcu_read_lock();
  54. hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
  55. if ((entry->ksym_addr == hbp_hit_addr) &&
  56. (entry->counter <= MAX_UL_INT)) {
  57. entry->counter++;
  58. break;
  59. }
  60. }
  61. rcu_read_unlock();
  62. }
  63. #endif /* CONFIG_PROFILE_KSYM_TRACER */
  64. void ksym_hbp_handler(struct hw_breakpoint *hbp, struct pt_regs *regs)
  65. {
  66. struct ring_buffer_event *event;
  67. struct trace_array *tr;
  68. struct ksym_trace_entry *entry;
  69. int pc;
  70. if (!ksym_tracing_enabled)
  71. return;
  72. tr = ksym_trace_array;
  73. pc = preempt_count();
  74. event = trace_buffer_lock_reserve(tr, TRACE_KSYM,
  75. sizeof(*entry), 0, pc);
  76. if (!event)
  77. return;
  78. entry = ring_buffer_event_data(event);
  79. entry->ip = instruction_pointer(regs);
  80. entry->type = hbp->info.type;
  81. strlcpy(entry->ksym_name, hbp->info.name, KSYM_SYMBOL_LEN);
  82. strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
  83. #ifdef CONFIG_PROFILE_KSYM_TRACER
  84. ksym_collect_stats(hbp->info.address);
  85. #endif /* CONFIG_PROFILE_KSYM_TRACER */
  86. trace_buffer_unlock_commit(tr, event, 0, pc);
  87. }
  88. /* Valid access types are represented as
  89. *
  90. * rw- : Set Read/Write Access Breakpoint
  91. * -w- : Set Write Access Breakpoint
  92. * --- : Clear Breakpoints
  93. * --x : Set Execution Break points (Not available yet)
  94. *
  95. */
  96. static int ksym_trace_get_access_type(char *str)
  97. {
  98. int access = 0;
  99. if (str[0] == 'r')
  100. access += 4;
  101. else if (str[0] != '-')
  102. return -EINVAL;
  103. if (str[1] == 'w')
  104. access += 2;
  105. else if (str[1] != '-')
  106. return -EINVAL;
  107. if (str[2] != '-')
  108. return -EINVAL;
  109. switch (access) {
  110. case 6:
  111. access = HW_BREAKPOINT_RW;
  112. break;
  113. case 2:
  114. access = HW_BREAKPOINT_WRITE;
  115. break;
  116. }
  117. return access;
  118. }
  119. /*
  120. * There can be several possible malformed requests and we attempt to capture
  121. * all of them. We enumerate some of the rules
  122. * 1. We will not allow kernel symbols with ':' since it is used as a delimiter.
  123. * i.e. multiple ':' symbols disallowed. Possible uses are of the form
  124. * <module>:<ksym_name>:<op>.
  125. * 2. No delimiter symbol ':' in the input string
  126. * 3. Spurious operator symbols or symbols not in their respective positions
  127. * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file
  128. * 5. Kernel symbol not a part of /proc/kallsyms
  129. * 6. Duplicate requests
  130. */
  131. static int parse_ksym_trace_str(char *input_string, char **ksymname,
  132. unsigned long *addr)
  133. {
  134. int ret;
  135. strstrip(input_string);
  136. *ksymname = strsep(&input_string, ":");
  137. *addr = kallsyms_lookup_name(*ksymname);
  138. /* Check for malformed request: (2), (1) and (5) */
  139. if ((!input_string) ||
  140. (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
  141. (*addr == 0))
  142. return -EINVAL;;
  143. ret = ksym_trace_get_access_type(input_string);
  144. return ret;
  145. }
  146. int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
  147. {
  148. struct trace_ksym *entry;
  149. int ret;
  150. if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
  151. printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
  152. " new requests for tracing can be accepted now.\n",
  153. KSYM_TRACER_MAX);
  154. return -ENOSPC;
  155. }
  156. entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
  157. if (!entry)
  158. return -ENOMEM;
  159. entry->ksym_hbp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
  160. if (!entry->ksym_hbp) {
  161. kfree(entry);
  162. return -ENOMEM;
  163. }
  164. entry->ksym_hbp->info.name = ksymname;
  165. entry->ksym_hbp->info.type = op;
  166. entry->ksym_addr = entry->ksym_hbp->info.address = addr;
  167. #ifdef CONFIG_X86
  168. entry->ksym_hbp->info.len = HW_BREAKPOINT_LEN_4;
  169. #endif
  170. entry->ksym_hbp->triggered = (void *)ksym_hbp_handler;
  171. ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
  172. if (ret < 0) {
  173. printk(KERN_INFO "ksym_tracer request failed. Try again"
  174. " later!!\n");
  175. kfree(entry->ksym_hbp);
  176. kfree(entry);
  177. return -EAGAIN;
  178. }
  179. hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
  180. ksym_filter_entry_count++;
  181. return 0;
  182. }
  183. static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
  184. size_t count, loff_t *ppos)
  185. {
  186. struct trace_ksym *entry;
  187. struct hlist_node *node;
  188. struct trace_seq *s;
  189. ssize_t cnt = 0;
  190. int ret;
  191. s = kmalloc(sizeof(*s), GFP_KERNEL);
  192. if (!s)
  193. return -ENOMEM;
  194. trace_seq_init(s);
  195. mutex_lock(&ksym_tracer_mutex);
  196. hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
  197. ret = trace_seq_printf(s, "%s:", entry->ksym_hbp->info.name);
  198. if (entry->ksym_hbp->info.type == HW_BREAKPOINT_WRITE)
  199. ret = trace_seq_puts(s, "-w-\n");
  200. else if (entry->ksym_hbp->info.type == HW_BREAKPOINT_RW)
  201. ret = trace_seq_puts(s, "rw-\n");
  202. WARN_ON_ONCE(!ret);
  203. }
  204. cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  205. mutex_unlock(&ksym_tracer_mutex);
  206. kfree(s);
  207. return cnt;
  208. }
  209. static ssize_t ksym_trace_filter_write(struct file *file,
  210. const char __user *buffer,
  211. size_t count, loff_t *ppos)
  212. {
  213. struct trace_ksym *entry;
  214. struct hlist_node *node;
  215. char *input_string, *ksymname = NULL;
  216. unsigned long ksym_addr = 0;
  217. int ret, op, changed = 0;
  218. input_string = kzalloc(count + 1, GFP_KERNEL);
  219. if (!input_string)
  220. return -ENOMEM;
  221. if (copy_from_user(input_string, buffer, count)) {
  222. kfree(input_string);
  223. return -EFAULT;
  224. }
  225. input_string[count] = '\0';
  226. ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
  227. if (ret < 0) {
  228. kfree(input_string);
  229. return ret;
  230. }
  231. mutex_lock(&ksym_tracer_mutex);
  232. ret = -EINVAL;
  233. hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
  234. if (entry->ksym_addr == ksym_addr) {
  235. /* Check for malformed request: (6) */
  236. if (entry->ksym_hbp->info.type != op)
  237. changed = 1;
  238. else
  239. goto err_ret;
  240. break;
  241. }
  242. }
  243. if (changed) {
  244. unregister_kernel_hw_breakpoint(entry->ksym_hbp);
  245. entry->ksym_hbp->info.type = op;
  246. if (op > 0) {
  247. ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
  248. if (ret == 0) {
  249. ret = count;
  250. goto unlock_ret_path;
  251. }
  252. } else
  253. ret = count;
  254. ksym_filter_entry_count--;
  255. hlist_del_rcu(&(entry->ksym_hlist));
  256. synchronize_rcu();
  257. kfree(entry->ksym_hbp);
  258. kfree(entry);
  259. goto err_ret;
  260. } else {
  261. /* Check for malformed request: (4) */
  262. if (op == 0)
  263. goto err_ret;
  264. ret = process_new_ksym_entry(ksymname, op, ksym_addr);
  265. if (ret)
  266. goto err_ret;
  267. }
  268. ret = count;
  269. goto unlock_ret_path;
  270. err_ret:
  271. kfree(input_string);
  272. unlock_ret_path:
  273. mutex_unlock(&ksym_tracer_mutex);
  274. return ret;
  275. }
  276. static const struct file_operations ksym_tracing_fops = {
  277. .open = tracing_open_generic,
  278. .read = ksym_trace_filter_read,
  279. .write = ksym_trace_filter_write,
  280. };
  281. static void ksym_trace_reset(struct trace_array *tr)
  282. {
  283. struct trace_ksym *entry;
  284. struct hlist_node *node, *node1;
  285. ksym_tracing_enabled = 0;
  286. mutex_lock(&ksym_tracer_mutex);
  287. hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
  288. ksym_hlist) {
  289. unregister_kernel_hw_breakpoint(entry->ksym_hbp);
  290. ksym_filter_entry_count--;
  291. hlist_del_rcu(&(entry->ksym_hlist));
  292. synchronize_rcu();
  293. /* Free the 'input_string' only if reset
  294. * after startup self-test
  295. */
  296. #ifdef CONFIG_FTRACE_SELFTEST
  297. if (strncmp(entry->ksym_hbp->info.name, KSYM_SELFTEST_ENTRY,
  298. strlen(KSYM_SELFTEST_ENTRY)) != 0)
  299. #endif /* CONFIG_FTRACE_SELFTEST*/
  300. kfree(entry->ksym_hbp->info.name);
  301. kfree(entry->ksym_hbp);
  302. kfree(entry);
  303. }
  304. mutex_unlock(&ksym_tracer_mutex);
  305. }
  306. static int ksym_trace_init(struct trace_array *tr)
  307. {
  308. int cpu, ret = 0;
  309. for_each_online_cpu(cpu)
  310. tracing_reset(tr, cpu);
  311. ksym_tracing_enabled = 1;
  312. ksym_trace_array = tr;
  313. return ret;
  314. }
  315. static void ksym_trace_print_header(struct seq_file *m)
  316. {
  317. seq_puts(m,
  318. "# TASK-PID CPU# Symbol Type "
  319. "Function \n");
  320. seq_puts(m,
  321. "# | | | | "
  322. "| \n");
  323. }
  324. static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
  325. {
  326. struct trace_entry *entry = iter->ent;
  327. struct trace_seq *s = &iter->seq;
  328. struct ksym_trace_entry *field;
  329. char str[KSYM_SYMBOL_LEN];
  330. int ret;
  331. if (entry->type != TRACE_KSYM)
  332. return TRACE_TYPE_UNHANDLED;
  333. trace_assign_type(field, entry);
  334. ret = trace_seq_printf(s, "%-15s %-5d %-3d %-20s ", field->cmd,
  335. entry->pid, iter->cpu, field->ksym_name);
  336. if (!ret)
  337. return TRACE_TYPE_PARTIAL_LINE;
  338. switch (field->type) {
  339. case HW_BREAKPOINT_WRITE:
  340. ret = trace_seq_printf(s, " W ");
  341. break;
  342. case HW_BREAKPOINT_RW:
  343. ret = trace_seq_printf(s, " RW ");
  344. break;
  345. default:
  346. return TRACE_TYPE_PARTIAL_LINE;
  347. }
  348. if (!ret)
  349. return TRACE_TYPE_PARTIAL_LINE;
  350. sprint_symbol(str, field->ip);
  351. ret = trace_seq_printf(s, "%-20s\n", str);
  352. if (!ret)
  353. return TRACE_TYPE_PARTIAL_LINE;
  354. return TRACE_TYPE_HANDLED;
  355. }
  356. struct tracer ksym_tracer __read_mostly =
  357. {
  358. .name = "ksym_tracer",
  359. .init = ksym_trace_init,
  360. .reset = ksym_trace_reset,
  361. #ifdef CONFIG_FTRACE_SELFTEST
  362. .selftest = trace_selftest_startup_ksym,
  363. #endif
  364. .print_header = ksym_trace_print_header,
  365. .print_line = ksym_trace_output
  366. };
  367. __init static int init_ksym_trace(void)
  368. {
  369. struct dentry *d_tracer;
  370. struct dentry *entry;
  371. d_tracer = tracing_init_dentry();
  372. ksym_filter_entry_count = 0;
  373. entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
  374. NULL, &ksym_tracing_fops);
  375. if (!entry)
  376. pr_warning("Could not create debugfs "
  377. "'ksym_trace_filter' file\n");
  378. return register_tracer(&ksym_tracer);
  379. }
  380. device_initcall(init_ksym_trace);
  381. #ifdef CONFIG_PROFILE_KSYM_TRACER
  382. static int ksym_tracer_stat_headers(struct seq_file *m)
  383. {
  384. seq_printf(m, " Access type ");
  385. seq_printf(m, " Symbol Counter \n");
  386. return 0;
  387. }
  388. static int ksym_tracer_stat_show(struct seq_file *m, void *v)
  389. {
  390. struct hlist_node *stat = v;
  391. struct trace_ksym *entry;
  392. int access_type = 0;
  393. char fn_name[KSYM_NAME_LEN];
  394. entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
  395. if (entry->ksym_hbp)
  396. access_type = entry->ksym_hbp->info.type;
  397. switch (access_type) {
  398. case HW_BREAKPOINT_WRITE:
  399. seq_printf(m, " W ");
  400. break;
  401. case HW_BREAKPOINT_RW:
  402. seq_printf(m, " RW ");
  403. break;
  404. default:
  405. seq_printf(m, " NA ");
  406. }
  407. if (lookup_symbol_name(entry->ksym_addr, fn_name) >= 0)
  408. seq_printf(m, " %s ", fn_name);
  409. else
  410. seq_printf(m, " <NA> ");
  411. seq_printf(m, "%15lu\n", entry->counter);
  412. return 0;
  413. }
  414. static void *ksym_tracer_stat_start(struct tracer_stat *trace)
  415. {
  416. return &(ksym_filter_head.first);
  417. }
  418. static void *
  419. ksym_tracer_stat_next(void *v, int idx)
  420. {
  421. struct hlist_node *stat = v;
  422. return stat->next;
  423. }
  424. static struct tracer_stat ksym_tracer_stats = {
  425. .name = "ksym_tracer",
  426. .stat_start = ksym_tracer_stat_start,
  427. .stat_next = ksym_tracer_stat_next,
  428. .stat_headers = ksym_tracer_stat_headers,
  429. .stat_show = ksym_tracer_stat_show
  430. };
  431. __init static int ksym_tracer_stat_init(void)
  432. {
  433. int ret;
  434. ret = register_stat_tracer(&ksym_tracer_stats);
  435. if (ret) {
  436. printk(KERN_WARNING "Warning: could not register "
  437. "ksym tracer stats\n");
  438. return 1;
  439. }
  440. return 0;
  441. }
  442. fs_initcall(ksym_tracer_stat_init);
  443. #endif /* CONFIG_PROFILE_KSYM_TRACER */