trace_ksym.c 13 KB

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