trace_ksym.c 12 KB

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