trace_ksym.c 13 KB

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