trace_ksym.c 13 KB

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