trace_ksym.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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_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. entry->type = op;
  159. entry->ksym_addr = addr;
  160. entry->len = HW_BREAKPOINT_LEN_4;
  161. ret = -EAGAIN;
  162. entry->ksym_hbp = register_wide_hw_breakpoint(entry->ksym_addr,
  163. entry->len, entry->type,
  164. ksym_hbp_handler, true);
  165. if (IS_ERR(entry->ksym_hbp)) {
  166. entry->ksym_hbp = NULL;
  167. ret = PTR_ERR(entry->ksym_hbp);
  168. }
  169. if (!entry->ksym_hbp) {
  170. printk(KERN_INFO "ksym_tracer request failed. Try again"
  171. " later!!\n");
  172. goto err;
  173. }
  174. hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
  175. ksym_filter_entry_count++;
  176. return 0;
  177. err:
  178. kfree(entry);
  179. return ret;
  180. }
  181. static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
  182. size_t count, loff_t *ppos)
  183. {
  184. struct trace_ksym *entry;
  185. struct hlist_node *node;
  186. struct trace_seq *s;
  187. ssize_t cnt = 0;
  188. int ret;
  189. s = kmalloc(sizeof(*s), GFP_KERNEL);
  190. if (!s)
  191. return -ENOMEM;
  192. trace_seq_init(s);
  193. mutex_lock(&ksym_tracer_mutex);
  194. hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
  195. ret = trace_seq_printf(s, "%pS:", (void *)entry->ksym_addr);
  196. if (entry->type == HW_BREAKPOINT_W)
  197. ret = trace_seq_puts(s, "-w-\n");
  198. else if (entry->type == (HW_BREAKPOINT_W | HW_BREAKPOINT_R))
  199. ret = trace_seq_puts(s, "rw-\n");
  200. WARN_ON_ONCE(!ret);
  201. }
  202. cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  203. mutex_unlock(&ksym_tracer_mutex);
  204. kfree(s);
  205. return cnt;
  206. }
  207. static void __ksym_trace_reset(void)
  208. {
  209. struct trace_ksym *entry;
  210. struct hlist_node *node, *node1;
  211. mutex_lock(&ksym_tracer_mutex);
  212. hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
  213. ksym_hlist) {
  214. unregister_wide_hw_breakpoint(entry->ksym_hbp);
  215. ksym_filter_entry_count--;
  216. hlist_del_rcu(&(entry->ksym_hlist));
  217. synchronize_rcu();
  218. kfree(entry);
  219. }
  220. mutex_unlock(&ksym_tracer_mutex);
  221. }
  222. static ssize_t ksym_trace_filter_write(struct file *file,
  223. const char __user *buffer,
  224. size_t count, loff_t *ppos)
  225. {
  226. struct trace_ksym *entry;
  227. struct hlist_node *node;
  228. char *input_string, *ksymname = NULL;
  229. unsigned long ksym_addr = 0;
  230. int ret, op, changed = 0;
  231. input_string = kzalloc(count + 1, GFP_KERNEL);
  232. if (!input_string)
  233. return -ENOMEM;
  234. if (copy_from_user(input_string, buffer, count)) {
  235. kfree(input_string);
  236. return -EFAULT;
  237. }
  238. input_string[count] = '\0';
  239. strstrip(input_string);
  240. /*
  241. * Clear all breakpoints if:
  242. * 1: echo > ksym_trace_filter
  243. * 2: echo 0 > ksym_trace_filter
  244. * 3: echo "*:---" > ksym_trace_filter
  245. */
  246. if (!input_string[0] || !strcmp(input_string, "0") ||
  247. !strcmp(input_string, "*:---")) {
  248. __ksym_trace_reset();
  249. kfree(input_string);
  250. return count;
  251. }
  252. ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
  253. if (ret < 0) {
  254. kfree(input_string);
  255. return ret;
  256. }
  257. mutex_lock(&ksym_tracer_mutex);
  258. ret = -EINVAL;
  259. hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
  260. if (entry->ksym_addr == ksym_addr) {
  261. /* Check for malformed request: (6) */
  262. if (entry->type != op)
  263. changed = 1;
  264. else
  265. goto out;
  266. break;
  267. }
  268. }
  269. if (changed) {
  270. unregister_wide_hw_breakpoint(entry->ksym_hbp);
  271. entry->type = op;
  272. if (op > 0) {
  273. entry->ksym_hbp =
  274. register_wide_hw_breakpoint(entry->ksym_addr,
  275. entry->len, entry->type,
  276. ksym_hbp_handler, true);
  277. if (IS_ERR(entry->ksym_hbp))
  278. entry->ksym_hbp = NULL;
  279. if (!entry->ksym_hbp)
  280. goto out;
  281. }
  282. ksym_filter_entry_count--;
  283. hlist_del_rcu(&(entry->ksym_hlist));
  284. synchronize_rcu();
  285. kfree(entry);
  286. ret = 0;
  287. goto out;
  288. } else {
  289. /* Check for malformed request: (4) */
  290. if (op == 0)
  291. goto out;
  292. ret = process_new_ksym_entry(ksymname, op, ksym_addr);
  293. }
  294. out:
  295. mutex_unlock(&ksym_tracer_mutex);
  296. kfree(input_string);
  297. if (!ret)
  298. ret = count;
  299. return ret;
  300. }
  301. static const struct file_operations ksym_tracing_fops = {
  302. .open = tracing_open_generic,
  303. .read = ksym_trace_filter_read,
  304. .write = ksym_trace_filter_write,
  305. };
  306. static void ksym_trace_reset(struct trace_array *tr)
  307. {
  308. ksym_tracing_enabled = 0;
  309. __ksym_trace_reset();
  310. }
  311. static int ksym_trace_init(struct trace_array *tr)
  312. {
  313. int cpu, ret = 0;
  314. for_each_online_cpu(cpu)
  315. tracing_reset(tr, cpu);
  316. ksym_tracing_enabled = 1;
  317. ksym_trace_array = tr;
  318. return ret;
  319. }
  320. static void ksym_trace_print_header(struct seq_file *m)
  321. {
  322. seq_puts(m,
  323. "# TASK-PID CPU# Symbol "
  324. "Type Function\n");
  325. seq_puts(m,
  326. "# | | | "
  327. " | |\n");
  328. }
  329. static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
  330. {
  331. struct trace_entry *entry = iter->ent;
  332. struct trace_seq *s = &iter->seq;
  333. struct ksym_trace_entry *field;
  334. char str[KSYM_SYMBOL_LEN];
  335. int ret;
  336. if (entry->type != TRACE_KSYM)
  337. return TRACE_TYPE_UNHANDLED;
  338. trace_assign_type(field, entry);
  339. ret = trace_seq_printf(s, "%11s-%-5d [%03d] %pS", field->cmd,
  340. entry->pid, iter->cpu, (char *)field->addr);
  341. if (!ret)
  342. return TRACE_TYPE_PARTIAL_LINE;
  343. switch (field->type) {
  344. case HW_BREAKPOINT_W:
  345. ret = trace_seq_printf(s, " W ");
  346. break;
  347. case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
  348. ret = trace_seq_printf(s, " RW ");
  349. break;
  350. default:
  351. return TRACE_TYPE_PARTIAL_LINE;
  352. }
  353. if (!ret)
  354. return TRACE_TYPE_PARTIAL_LINE;
  355. sprint_symbol(str, field->ip);
  356. ret = trace_seq_printf(s, "%s\n", str);
  357. if (!ret)
  358. return TRACE_TYPE_PARTIAL_LINE;
  359. return TRACE_TYPE_HANDLED;
  360. }
  361. struct tracer ksym_tracer __read_mostly =
  362. {
  363. .name = "ksym_tracer",
  364. .init = ksym_trace_init,
  365. .reset = ksym_trace_reset,
  366. #ifdef CONFIG_FTRACE_SELFTEST
  367. .selftest = trace_selftest_startup_ksym,
  368. #endif
  369. .print_header = ksym_trace_print_header,
  370. .print_line = ksym_trace_output
  371. };
  372. __init static int init_ksym_trace(void)
  373. {
  374. struct dentry *d_tracer;
  375. struct dentry *entry;
  376. d_tracer = tracing_init_dentry();
  377. ksym_filter_entry_count = 0;
  378. entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
  379. NULL, &ksym_tracing_fops);
  380. if (!entry)
  381. pr_warning("Could not create debugfs "
  382. "'ksym_trace_filter' file\n");
  383. return register_tracer(&ksym_tracer);
  384. }
  385. device_initcall(init_ksym_trace);
  386. #ifdef CONFIG_PROFILE_KSYM_TRACER
  387. static int ksym_tracer_stat_headers(struct seq_file *m)
  388. {
  389. seq_puts(m, " Access Type ");
  390. seq_puts(m, " Symbol Counter\n");
  391. seq_puts(m, " ----------- ");
  392. seq_puts(m, " ------ -------\n");
  393. return 0;
  394. }
  395. static int ksym_tracer_stat_show(struct seq_file *m, void *v)
  396. {
  397. struct hlist_node *stat = v;
  398. struct trace_ksym *entry;
  399. int access_type = 0;
  400. char fn_name[KSYM_NAME_LEN];
  401. entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
  402. access_type = entry->type;
  403. switch (access_type) {
  404. case HW_BREAKPOINT_W:
  405. seq_puts(m, " W ");
  406. break;
  407. case HW_BREAKPOINT_R | HW_BREAKPOINT_W:
  408. seq_puts(m, " RW ");
  409. break;
  410. default:
  411. seq_puts(m, " NA ");
  412. }
  413. if (lookup_symbol_name(entry->ksym_addr, fn_name) >= 0)
  414. seq_printf(m, " %-36s", fn_name);
  415. else
  416. seq_printf(m, " %-36s", "<NA>");
  417. seq_printf(m, " %15lu\n", entry->counter);
  418. return 0;
  419. }
  420. static void *ksym_tracer_stat_start(struct tracer_stat *trace)
  421. {
  422. return ksym_filter_head.first;
  423. }
  424. static void *
  425. ksym_tracer_stat_next(void *v, int idx)
  426. {
  427. struct hlist_node *stat = v;
  428. return stat->next;
  429. }
  430. static struct tracer_stat ksym_tracer_stats = {
  431. .name = "ksym_tracer",
  432. .stat_start = ksym_tracer_stat_start,
  433. .stat_next = ksym_tracer_stat_next,
  434. .stat_headers = ksym_tracer_stat_headers,
  435. .stat_show = ksym_tracer_stat_show
  436. };
  437. __init static int ksym_tracer_stat_init(void)
  438. {
  439. int ret;
  440. ret = register_stat_tracer(&ksym_tracer_stats);
  441. if (ret) {
  442. printk(KERN_WARNING "Warning: could not register "
  443. "ksym tracer stats\n");
  444. return 1;
  445. }
  446. return 0;
  447. }
  448. fs_initcall(ksym_tracer_stat_init);
  449. #endif /* CONFIG_PROFILE_KSYM_TRACER */