trace_ksym.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. #include <linux/hw_breakpoint.h>
  30. #include <asm/hw_breakpoint.h>
  31. /*
  32. * For now, let us restrict the no. of symbols traced simultaneously to number
  33. * of available hardware breakpoint registers.
  34. */
  35. #define KSYM_TRACER_MAX HBP_NUM
  36. #define KSYM_TRACER_OP_LEN 3 /* rw- */
  37. struct trace_ksym {
  38. struct perf_event **ksym_hbp;
  39. struct perf_event_attr attr;
  40. #ifdef CONFIG_PROFILE_KSYM_TRACER
  41. unsigned long counter;
  42. #endif
  43. struct hlist_node ksym_hlist;
  44. };
  45. static struct trace_array *ksym_trace_array;
  46. static unsigned int ksym_filter_entry_count;
  47. static unsigned int ksym_tracing_enabled;
  48. static HLIST_HEAD(ksym_filter_head);
  49. static DEFINE_MUTEX(ksym_tracer_mutex);
  50. #ifdef CONFIG_PROFILE_KSYM_TRACER
  51. #define MAX_UL_INT 0xffffffff
  52. void ksym_collect_stats(unsigned long hbp_hit_addr)
  53. {
  54. struct hlist_node *node;
  55. struct trace_ksym *entry;
  56. rcu_read_lock();
  57. hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
  58. if ((entry->attr.bp_addr == hbp_hit_addr) &&
  59. (entry->counter <= MAX_UL_INT)) {
  60. entry->counter++;
  61. break;
  62. }
  63. }
  64. rcu_read_unlock();
  65. }
  66. #endif /* CONFIG_PROFILE_KSYM_TRACER */
  67. void ksym_hbp_handler(struct perf_event *hbp, void *data)
  68. {
  69. struct ring_buffer_event *event;
  70. struct ksym_trace_entry *entry;
  71. struct pt_regs *regs = data;
  72. struct ring_buffer *buffer;
  73. int pc;
  74. if (!ksym_tracing_enabled)
  75. return;
  76. buffer = ksym_trace_array->buffer;
  77. pc = preempt_count();
  78. event = trace_buffer_lock_reserve(buffer, TRACE_KSYM,
  79. sizeof(*entry), 0, pc);
  80. if (!event)
  81. return;
  82. entry = ring_buffer_event_data(event);
  83. entry->ip = instruction_pointer(regs);
  84. entry->type = hw_breakpoint_type(hbp);
  85. entry->addr = hw_breakpoint_addr(hbp);
  86. strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
  87. #ifdef CONFIG_PROFILE_KSYM_TRACER
  88. ksym_collect_stats(hw_breakpoint_addr(hbp));
  89. #endif /* CONFIG_PROFILE_KSYM_TRACER */
  90. trace_buffer_unlock_commit(buffer, event, 0, pc);
  91. }
  92. /* Valid access types are represented as
  93. *
  94. * rw- : Set Read/Write Access Breakpoint
  95. * -w- : Set Write Access Breakpoint
  96. * --- : Clear Breakpoints
  97. * --x : Set Execution Break points (Not available yet)
  98. *
  99. */
  100. static int ksym_trace_get_access_type(char *str)
  101. {
  102. int access = 0;
  103. if (str[0] == 'r')
  104. access |= HW_BREAKPOINT_R;
  105. if (str[1] == 'w')
  106. access |= HW_BREAKPOINT_W;
  107. if (str[2] == 'x')
  108. access |= HW_BREAKPOINT_X;
  109. switch (access) {
  110. case HW_BREAKPOINT_R:
  111. case HW_BREAKPOINT_W:
  112. case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
  113. return access;
  114. default:
  115. return -EINVAL;
  116. }
  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. int ret;
  134. *ksymname = strsep(&input_string, ":");
  135. *addr = kallsyms_lookup_name(*ksymname);
  136. /* Check for malformed request: (2), (1) and (5) */
  137. if ((!input_string) ||
  138. (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
  139. (*addr == 0))
  140. return -EINVAL;;
  141. ret = ksym_trace_get_access_type(input_string);
  142. return ret;
  143. }
  144. int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
  145. {
  146. struct trace_ksym *entry;
  147. int ret = -ENOMEM;
  148. if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
  149. printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
  150. " new requests for tracing can be accepted now.\n",
  151. KSYM_TRACER_MAX);
  152. return -ENOSPC;
  153. }
  154. entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
  155. if (!entry)
  156. return -ENOMEM;
  157. hw_breakpoint_init(&entry->attr);
  158. entry->attr.bp_type = op;
  159. entry->attr.bp_addr = addr;
  160. entry->attr.bp_len = HW_BREAKPOINT_LEN_4;
  161. ret = -EAGAIN;
  162. entry->ksym_hbp = register_wide_hw_breakpoint(&entry->attr,
  163. ksym_hbp_handler);
  164. if (IS_ERR(entry->ksym_hbp)) {
  165. ret = PTR_ERR(entry->ksym_hbp);
  166. printk(KERN_INFO "ksym_tracer request failed. Try again"
  167. " later!!\n");
  168. goto err;
  169. }
  170. hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
  171. ksym_filter_entry_count++;
  172. return 0;
  173. err:
  174. kfree(entry);
  175. return ret;
  176. }
  177. static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
  178. size_t count, loff_t *ppos)
  179. {
  180. struct trace_ksym *entry;
  181. struct hlist_node *node;
  182. struct trace_seq *s;
  183. ssize_t cnt = 0;
  184. int ret;
  185. s = kmalloc(sizeof(*s), GFP_KERNEL);
  186. if (!s)
  187. return -ENOMEM;
  188. trace_seq_init(s);
  189. mutex_lock(&ksym_tracer_mutex);
  190. hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
  191. ret = trace_seq_printf(s, "%pS:", (void *)entry->attr.bp_addr);
  192. if (entry->attr.bp_type == HW_BREAKPOINT_R)
  193. ret = trace_seq_puts(s, "r--\n");
  194. else if (entry->attr.bp_type == HW_BREAKPOINT_W)
  195. ret = trace_seq_puts(s, "-w-\n");
  196. else if (entry->attr.bp_type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
  197. ret = trace_seq_puts(s, "rw-\n");
  198. WARN_ON_ONCE(!ret);
  199. }
  200. cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  201. mutex_unlock(&ksym_tracer_mutex);
  202. kfree(s);
  203. return cnt;
  204. }
  205. static void __ksym_trace_reset(void)
  206. {
  207. struct trace_ksym *entry;
  208. struct hlist_node *node, *node1;
  209. mutex_lock(&ksym_tracer_mutex);
  210. hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
  211. ksym_hlist) {
  212. unregister_wide_hw_breakpoint(entry->ksym_hbp);
  213. ksym_filter_entry_count--;
  214. hlist_del_rcu(&(entry->ksym_hlist));
  215. synchronize_rcu();
  216. kfree(entry);
  217. }
  218. mutex_unlock(&ksym_tracer_mutex);
  219. }
  220. static ssize_t ksym_trace_filter_write(struct file *file,
  221. const char __user *buffer,
  222. size_t count, loff_t *ppos)
  223. {
  224. struct trace_ksym *entry;
  225. struct hlist_node *node;
  226. char *input_string, *ksymname = NULL;
  227. unsigned long ksym_addr = 0;
  228. int ret, op, changed = 0;
  229. input_string = kzalloc(count + 1, 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. input_string[count] = '\0';
  237. strstrip(input_string);
  238. /*
  239. * Clear all breakpoints if:
  240. * 1: echo > ksym_trace_filter
  241. * 2: echo 0 > ksym_trace_filter
  242. * 3: echo "*:---" > ksym_trace_filter
  243. */
  244. if (!input_string[0] || !strcmp(input_string, "0") ||
  245. !strcmp(input_string, "*:---")) {
  246. __ksym_trace_reset();
  247. kfree(input_string);
  248. return count;
  249. }
  250. ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
  251. if (ret < 0) {
  252. kfree(input_string);
  253. return ret;
  254. }
  255. mutex_lock(&ksym_tracer_mutex);
  256. ret = -EINVAL;
  257. hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
  258. if (entry->attr.bp_addr == ksym_addr) {
  259. /* Check for malformed request: (6) */
  260. if (entry->attr.bp_type != op)
  261. changed = 1;
  262. else
  263. goto out;
  264. break;
  265. }
  266. }
  267. if (changed) {
  268. unregister_wide_hw_breakpoint(entry->ksym_hbp);
  269. entry->attr.bp_type = op;
  270. ret = 0;
  271. if (op > 0) {
  272. entry->ksym_hbp =
  273. register_wide_hw_breakpoint(&entry->attr,
  274. ksym_hbp_handler);
  275. if (IS_ERR(entry->ksym_hbp))
  276. ret = PTR_ERR(entry->ksym_hbp);
  277. else
  278. goto out;
  279. }
  280. /* Error or "symbol:---" case: drop it */
  281. ksym_filter_entry_count--;
  282. hlist_del_rcu(&(entry->ksym_hlist));
  283. synchronize_rcu();
  284. kfree(entry);
  285. goto out;
  286. } else {
  287. /* Check for malformed request: (4) */
  288. if (op == 0)
  289. goto out;
  290. ret = process_new_ksym_entry(ksymname, op, ksym_addr);
  291. }
  292. out:
  293. mutex_unlock(&ksym_tracer_mutex);
  294. kfree(input_string);
  295. if (!ret)
  296. ret = count;
  297. return ret;
  298. }
  299. static const struct file_operations ksym_tracing_fops = {
  300. .open = tracing_open_generic,
  301. .read = ksym_trace_filter_read,
  302. .write = ksym_trace_filter_write,
  303. };
  304. static void ksym_trace_reset(struct trace_array *tr)
  305. {
  306. ksym_tracing_enabled = 0;
  307. __ksym_trace_reset();
  308. }
  309. static int ksym_trace_init(struct trace_array *tr)
  310. {
  311. int cpu, ret = 0;
  312. for_each_online_cpu(cpu)
  313. tracing_reset(tr, cpu);
  314. ksym_tracing_enabled = 1;
  315. ksym_trace_array = tr;
  316. return ret;
  317. }
  318. static void ksym_trace_print_header(struct seq_file *m)
  319. {
  320. seq_puts(m,
  321. "# TASK-PID CPU# Symbol "
  322. "Type Function\n");
  323. seq_puts(m,
  324. "# | | | "
  325. " | |\n");
  326. }
  327. static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
  328. {
  329. struct trace_entry *entry = iter->ent;
  330. struct trace_seq *s = &iter->seq;
  331. struct ksym_trace_entry *field;
  332. char str[KSYM_SYMBOL_LEN];
  333. int ret;
  334. if (entry->type != TRACE_KSYM)
  335. return TRACE_TYPE_UNHANDLED;
  336. trace_assign_type(field, entry);
  337. ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
  338. entry->pid, iter->cpu, (char *)field->addr);
  339. if (!ret)
  340. return TRACE_TYPE_PARTIAL_LINE;
  341. switch (field->type) {
  342. case HW_BREAKPOINT_R:
  343. ret = trace_seq_printf(s, " R ");
  344. break;
  345. case HW_BREAKPOINT_W:
  346. ret = trace_seq_printf(s, " W ");
  347. break;
  348. case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
  349. ret = trace_seq_printf(s, " RW ");
  350. break;
  351. default:
  352. return TRACE_TYPE_PARTIAL_LINE;
  353. }
  354. if (!ret)
  355. return TRACE_TYPE_PARTIAL_LINE;
  356. sprint_symbol(str, field->ip);
  357. ret = trace_seq_printf(s, "%s\n", str);
  358. if (!ret)
  359. return TRACE_TYPE_PARTIAL_LINE;
  360. return TRACE_TYPE_HANDLED;
  361. }
  362. struct tracer ksym_tracer __read_mostly =
  363. {
  364. .name = "ksym_tracer",
  365. .init = ksym_trace_init,
  366. .reset = ksym_trace_reset,
  367. #ifdef CONFIG_FTRACE_SELFTEST
  368. .selftest = trace_selftest_startup_ksym,
  369. #endif
  370. .print_header = ksym_trace_print_header,
  371. .print_line = ksym_trace_output
  372. };
  373. __init static int init_ksym_trace(void)
  374. {
  375. struct dentry *d_tracer;
  376. struct dentry *entry;
  377. d_tracer = tracing_init_dentry();
  378. ksym_filter_entry_count = 0;
  379. entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
  380. NULL, &ksym_tracing_fops);
  381. if (!entry)
  382. pr_warning("Could not create debugfs "
  383. "'ksym_trace_filter' file\n");
  384. return register_tracer(&ksym_tracer);
  385. }
  386. device_initcall(init_ksym_trace);
  387. #ifdef CONFIG_PROFILE_KSYM_TRACER
  388. static int ksym_tracer_stat_headers(struct seq_file *m)
  389. {
  390. seq_puts(m, " Access Type ");
  391. seq_puts(m, " Symbol Counter\n");
  392. seq_puts(m, " ----------- ");
  393. seq_puts(m, " ------ -------\n");
  394. return 0;
  395. }
  396. static int ksym_tracer_stat_show(struct seq_file *m, void *v)
  397. {
  398. struct hlist_node *stat = v;
  399. struct trace_ksym *entry;
  400. int access_type = 0;
  401. char fn_name[KSYM_NAME_LEN];
  402. entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
  403. access_type = entry->attr.bp_type;
  404. switch (access_type) {
  405. case HW_BREAKPOINT_R:
  406. seq_puts(m, " R ");
  407. break;
  408. case HW_BREAKPOINT_W:
  409. seq_puts(m, " W ");
  410. break;
  411. case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
  412. seq_puts(m, " RW ");
  413. break;
  414. default:
  415. seq_puts(m, " NA ");
  416. }
  417. if (lookup_symbol_name(entry->attr.bp_addr, fn_name) >= 0)
  418. seq_printf(m, " %-36s", fn_name);
  419. else
  420. seq_printf(m, " %-36s", "<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 */