trace_ksym.c 13 KB

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