trace_ksym.c 12 KB

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