trace_uprobe.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * uprobes-based tracing events
  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 version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * Copyright (C) IBM Corporation, 2010-2012
  18. * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
  19. */
  20. #include <linux/module.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/uprobes.h>
  23. #include <linux/namei.h>
  24. #include <linux/string.h>
  25. #include "trace_probe.h"
  26. #define UPROBE_EVENT_SYSTEM "uprobes"
  27. struct uprobe_trace_entry_head {
  28. struct trace_entry ent;
  29. unsigned long vaddr[];
  30. };
  31. #define SIZEOF_TRACE_ENTRY(is_return) \
  32. (sizeof(struct uprobe_trace_entry_head) + \
  33. sizeof(unsigned long) * (is_return ? 2 : 1))
  34. #define DATAOF_TRACE_ENTRY(entry, is_return) \
  35. ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
  36. struct trace_uprobe_filter {
  37. rwlock_t rwlock;
  38. int nr_systemwide;
  39. struct list_head perf_events;
  40. };
  41. /*
  42. * uprobe event core functions
  43. */
  44. struct trace_uprobe {
  45. struct list_head list;
  46. struct trace_uprobe_filter filter;
  47. struct uprobe_consumer consumer;
  48. struct inode *inode;
  49. char *filename;
  50. unsigned long offset;
  51. unsigned long nhit;
  52. struct trace_probe tp;
  53. };
  54. #define SIZEOF_TRACE_UPROBE(n) \
  55. (offsetof(struct trace_uprobe, tp.args) + \
  56. (sizeof(struct probe_arg) * (n)))
  57. static int register_uprobe_event(struct trace_uprobe *tu);
  58. static int unregister_uprobe_event(struct trace_uprobe *tu);
  59. static DEFINE_MUTEX(uprobe_lock);
  60. static LIST_HEAD(uprobe_list);
  61. struct uprobe_dispatch_data {
  62. struct trace_uprobe *tu;
  63. unsigned long bp_addr;
  64. };
  65. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
  66. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  67. unsigned long func, struct pt_regs *regs);
  68. #ifdef CONFIG_STACK_GROWSUP
  69. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  70. {
  71. return addr - (n * sizeof(long));
  72. }
  73. #else
  74. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  75. {
  76. return addr + (n * sizeof(long));
  77. }
  78. #endif
  79. static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
  80. {
  81. unsigned long ret;
  82. unsigned long addr = user_stack_pointer(regs);
  83. addr = adjust_stack_addr(addr, n);
  84. if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
  85. return 0;
  86. return ret;
  87. }
  88. /*
  89. * Uprobes-specific fetch functions
  90. */
  91. #define DEFINE_FETCH_stack(type) \
  92. static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
  93. void *offset, void *dest) \
  94. { \
  95. *(type *)dest = (type)get_user_stack_nth(regs, \
  96. ((unsigned long)offset)); \
  97. }
  98. DEFINE_BASIC_FETCH_FUNCS(stack)
  99. /* No string on the stack entry */
  100. #define fetch_stack_string NULL
  101. #define fetch_stack_string_size NULL
  102. #define DEFINE_FETCH_memory(type) \
  103. static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
  104. void *addr, void *dest) \
  105. { \
  106. type retval; \
  107. void __user *vaddr = (void __force __user *) addr; \
  108. \
  109. if (copy_from_user(&retval, vaddr, sizeof(type))) \
  110. *(type *)dest = 0; \
  111. else \
  112. *(type *) dest = retval; \
  113. }
  114. DEFINE_BASIC_FETCH_FUNCS(memory)
  115. /*
  116. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  117. * length and relative data location.
  118. */
  119. static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  120. void *addr, void *dest)
  121. {
  122. long ret;
  123. u32 rloc = *(u32 *)dest;
  124. int maxlen = get_rloc_len(rloc);
  125. u8 *dst = get_rloc_data(dest);
  126. void __user *src = (void __force __user *) addr;
  127. if (!maxlen)
  128. return;
  129. ret = strncpy_from_user(dst, src, maxlen);
  130. if (ret < 0) { /* Failed to fetch string */
  131. ((u8 *)get_rloc_data(dest))[0] = '\0';
  132. *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
  133. } else {
  134. *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
  135. }
  136. }
  137. static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
  138. void *addr, void *dest)
  139. {
  140. int len;
  141. void __user *vaddr = (void __force __user *) addr;
  142. len = strnlen_user(vaddr, MAX_STRING_SIZE);
  143. if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
  144. *(u32 *)dest = 0;
  145. else
  146. *(u32 *)dest = len;
  147. }
  148. static unsigned long translate_user_vaddr(void *file_offset)
  149. {
  150. unsigned long base_addr;
  151. struct uprobe_dispatch_data *udd;
  152. udd = (void *) current->utask->vaddr;
  153. base_addr = udd->bp_addr - udd->tu->offset;
  154. return base_addr + (unsigned long)file_offset;
  155. }
  156. #define DEFINE_FETCH_file_offset(type) \
  157. static __kprobes void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs,\
  158. void *offset, void *dest) \
  159. { \
  160. void *vaddr = (void *)translate_user_vaddr(offset); \
  161. \
  162. FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
  163. }
  164. DEFINE_BASIC_FETCH_FUNCS(file_offset)
  165. DEFINE_FETCH_file_offset(string)
  166. DEFINE_FETCH_file_offset(string_size)
  167. /* Fetch type information table */
  168. const struct fetch_type uprobes_fetch_type_table[] = {
  169. /* Special types */
  170. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  171. sizeof(u32), 1, "__data_loc char[]"),
  172. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  173. string_size, sizeof(u32), 0, "u32"),
  174. /* Basic types */
  175. ASSIGN_FETCH_TYPE(u8, u8, 0),
  176. ASSIGN_FETCH_TYPE(u16, u16, 0),
  177. ASSIGN_FETCH_TYPE(u32, u32, 0),
  178. ASSIGN_FETCH_TYPE(u64, u64, 0),
  179. ASSIGN_FETCH_TYPE(s8, u8, 1),
  180. ASSIGN_FETCH_TYPE(s16, u16, 1),
  181. ASSIGN_FETCH_TYPE(s32, u32, 1),
  182. ASSIGN_FETCH_TYPE(s64, u64, 1),
  183. ASSIGN_FETCH_TYPE_END
  184. };
  185. static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
  186. {
  187. rwlock_init(&filter->rwlock);
  188. filter->nr_systemwide = 0;
  189. INIT_LIST_HEAD(&filter->perf_events);
  190. }
  191. static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
  192. {
  193. return !filter->nr_systemwide && list_empty(&filter->perf_events);
  194. }
  195. static inline bool is_ret_probe(struct trace_uprobe *tu)
  196. {
  197. return tu->consumer.ret_handler != NULL;
  198. }
  199. /*
  200. * Allocate new trace_uprobe and initialize it (including uprobes).
  201. */
  202. static struct trace_uprobe *
  203. alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
  204. {
  205. struct trace_uprobe *tu;
  206. if (!event || !is_good_name(event))
  207. return ERR_PTR(-EINVAL);
  208. if (!group || !is_good_name(group))
  209. return ERR_PTR(-EINVAL);
  210. tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
  211. if (!tu)
  212. return ERR_PTR(-ENOMEM);
  213. tu->tp.call.class = &tu->tp.class;
  214. tu->tp.call.name = kstrdup(event, GFP_KERNEL);
  215. if (!tu->tp.call.name)
  216. goto error;
  217. tu->tp.class.system = kstrdup(group, GFP_KERNEL);
  218. if (!tu->tp.class.system)
  219. goto error;
  220. INIT_LIST_HEAD(&tu->list);
  221. tu->consumer.handler = uprobe_dispatcher;
  222. if (is_ret)
  223. tu->consumer.ret_handler = uretprobe_dispatcher;
  224. init_trace_uprobe_filter(&tu->filter);
  225. tu->tp.call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER;
  226. return tu;
  227. error:
  228. kfree(tu->tp.call.name);
  229. kfree(tu);
  230. return ERR_PTR(-ENOMEM);
  231. }
  232. static void free_trace_uprobe(struct trace_uprobe *tu)
  233. {
  234. int i;
  235. for (i = 0; i < tu->tp.nr_args; i++)
  236. traceprobe_free_probe_arg(&tu->tp.args[i]);
  237. iput(tu->inode);
  238. kfree(tu->tp.call.class->system);
  239. kfree(tu->tp.call.name);
  240. kfree(tu->filename);
  241. kfree(tu);
  242. }
  243. static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  244. {
  245. struct trace_uprobe *tu;
  246. list_for_each_entry(tu, &uprobe_list, list)
  247. if (strcmp(tu->tp.call.name, event) == 0 &&
  248. strcmp(tu->tp.call.class->system, group) == 0)
  249. return tu;
  250. return NULL;
  251. }
  252. /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
  253. static int unregister_trace_uprobe(struct trace_uprobe *tu)
  254. {
  255. int ret;
  256. ret = unregister_uprobe_event(tu);
  257. if (ret)
  258. return ret;
  259. list_del(&tu->list);
  260. free_trace_uprobe(tu);
  261. return 0;
  262. }
  263. /* Register a trace_uprobe and probe_event */
  264. static int register_trace_uprobe(struct trace_uprobe *tu)
  265. {
  266. struct trace_uprobe *old_tu;
  267. int ret;
  268. mutex_lock(&uprobe_lock);
  269. /* register as an event */
  270. old_tu = find_probe_event(tu->tp.call.name, tu->tp.call.class->system);
  271. if (old_tu) {
  272. /* delete old event */
  273. ret = unregister_trace_uprobe(old_tu);
  274. if (ret)
  275. goto end;
  276. }
  277. ret = register_uprobe_event(tu);
  278. if (ret) {
  279. pr_warning("Failed to register probe event(%d)\n", ret);
  280. goto end;
  281. }
  282. list_add_tail(&tu->list, &uprobe_list);
  283. end:
  284. mutex_unlock(&uprobe_lock);
  285. return ret;
  286. }
  287. /*
  288. * Argument syntax:
  289. * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
  290. *
  291. * - Remove uprobe: -:[GRP/]EVENT
  292. */
  293. static int create_trace_uprobe(int argc, char **argv)
  294. {
  295. struct trace_uprobe *tu;
  296. struct inode *inode;
  297. char *arg, *event, *group, *filename;
  298. char buf[MAX_EVENT_NAME_LEN];
  299. struct path path;
  300. unsigned long offset;
  301. bool is_delete, is_return;
  302. int i, ret;
  303. inode = NULL;
  304. ret = 0;
  305. is_delete = false;
  306. is_return = false;
  307. event = NULL;
  308. group = NULL;
  309. /* argc must be >= 1 */
  310. if (argv[0][0] == '-')
  311. is_delete = true;
  312. else if (argv[0][0] == 'r')
  313. is_return = true;
  314. else if (argv[0][0] != 'p') {
  315. pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
  316. return -EINVAL;
  317. }
  318. if (argv[0][1] == ':') {
  319. event = &argv[0][2];
  320. arg = strchr(event, '/');
  321. if (arg) {
  322. group = event;
  323. event = arg + 1;
  324. event[-1] = '\0';
  325. if (strlen(group) == 0) {
  326. pr_info("Group name is not specified\n");
  327. return -EINVAL;
  328. }
  329. }
  330. if (strlen(event) == 0) {
  331. pr_info("Event name is not specified\n");
  332. return -EINVAL;
  333. }
  334. }
  335. if (!group)
  336. group = UPROBE_EVENT_SYSTEM;
  337. if (is_delete) {
  338. int ret;
  339. if (!event) {
  340. pr_info("Delete command needs an event name.\n");
  341. return -EINVAL;
  342. }
  343. mutex_lock(&uprobe_lock);
  344. tu = find_probe_event(event, group);
  345. if (!tu) {
  346. mutex_unlock(&uprobe_lock);
  347. pr_info("Event %s/%s doesn't exist.\n", group, event);
  348. return -ENOENT;
  349. }
  350. /* delete an event */
  351. ret = unregister_trace_uprobe(tu);
  352. mutex_unlock(&uprobe_lock);
  353. return ret;
  354. }
  355. if (argc < 2) {
  356. pr_info("Probe point is not specified.\n");
  357. return -EINVAL;
  358. }
  359. if (isdigit(argv[1][0])) {
  360. pr_info("probe point must be have a filename.\n");
  361. return -EINVAL;
  362. }
  363. arg = strchr(argv[1], ':');
  364. if (!arg) {
  365. ret = -EINVAL;
  366. goto fail_address_parse;
  367. }
  368. *arg++ = '\0';
  369. filename = argv[1];
  370. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  371. if (ret)
  372. goto fail_address_parse;
  373. inode = igrab(path.dentry->d_inode);
  374. path_put(&path);
  375. if (!inode || !S_ISREG(inode->i_mode)) {
  376. ret = -EINVAL;
  377. goto fail_address_parse;
  378. }
  379. ret = kstrtoul(arg, 0, &offset);
  380. if (ret)
  381. goto fail_address_parse;
  382. argc -= 2;
  383. argv += 2;
  384. /* setup a probe */
  385. if (!event) {
  386. char *tail;
  387. char *ptr;
  388. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  389. if (!tail) {
  390. ret = -ENOMEM;
  391. goto fail_address_parse;
  392. }
  393. ptr = strpbrk(tail, ".-_");
  394. if (ptr)
  395. *ptr = '\0';
  396. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  397. event = buf;
  398. kfree(tail);
  399. }
  400. tu = alloc_trace_uprobe(group, event, argc, is_return);
  401. if (IS_ERR(tu)) {
  402. pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
  403. ret = PTR_ERR(tu);
  404. goto fail_address_parse;
  405. }
  406. tu->offset = offset;
  407. tu->inode = inode;
  408. tu->filename = kstrdup(filename, GFP_KERNEL);
  409. if (!tu->filename) {
  410. pr_info("Failed to allocate filename.\n");
  411. ret = -ENOMEM;
  412. goto error;
  413. }
  414. /* parse arguments */
  415. ret = 0;
  416. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  417. struct probe_arg *parg = &tu->tp.args[i];
  418. /* Increment count for freeing args in error case */
  419. tu->tp.nr_args++;
  420. /* Parse argument name */
  421. arg = strchr(argv[i], '=');
  422. if (arg) {
  423. *arg++ = '\0';
  424. parg->name = kstrdup(argv[i], GFP_KERNEL);
  425. } else {
  426. arg = argv[i];
  427. /* If argument name is omitted, set "argN" */
  428. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  429. parg->name = kstrdup(buf, GFP_KERNEL);
  430. }
  431. if (!parg->name) {
  432. pr_info("Failed to allocate argument[%d] name.\n", i);
  433. ret = -ENOMEM;
  434. goto error;
  435. }
  436. if (!is_good_name(parg->name)) {
  437. pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
  438. ret = -EINVAL;
  439. goto error;
  440. }
  441. if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
  442. pr_info("Argument[%d] name '%s' conflicts with "
  443. "another field.\n", i, argv[i]);
  444. ret = -EINVAL;
  445. goto error;
  446. }
  447. /* Parse fetch argument */
  448. ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
  449. is_return, false);
  450. if (ret) {
  451. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  452. goto error;
  453. }
  454. }
  455. ret = register_trace_uprobe(tu);
  456. if (ret)
  457. goto error;
  458. return 0;
  459. error:
  460. free_trace_uprobe(tu);
  461. return ret;
  462. fail_address_parse:
  463. if (inode)
  464. iput(inode);
  465. pr_info("Failed to parse address or file.\n");
  466. return ret;
  467. }
  468. static int cleanup_all_probes(void)
  469. {
  470. struct trace_uprobe *tu;
  471. int ret = 0;
  472. mutex_lock(&uprobe_lock);
  473. while (!list_empty(&uprobe_list)) {
  474. tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
  475. ret = unregister_trace_uprobe(tu);
  476. if (ret)
  477. break;
  478. }
  479. mutex_unlock(&uprobe_lock);
  480. return ret;
  481. }
  482. /* Probes listing interfaces */
  483. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  484. {
  485. mutex_lock(&uprobe_lock);
  486. return seq_list_start(&uprobe_list, *pos);
  487. }
  488. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  489. {
  490. return seq_list_next(v, &uprobe_list, pos);
  491. }
  492. static void probes_seq_stop(struct seq_file *m, void *v)
  493. {
  494. mutex_unlock(&uprobe_lock);
  495. }
  496. static int probes_seq_show(struct seq_file *m, void *v)
  497. {
  498. struct trace_uprobe *tu = v;
  499. char c = is_ret_probe(tu) ? 'r' : 'p';
  500. int i;
  501. seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system, tu->tp.call.name);
  502. seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
  503. for (i = 0; i < tu->tp.nr_args; i++)
  504. seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
  505. seq_printf(m, "\n");
  506. return 0;
  507. }
  508. static const struct seq_operations probes_seq_op = {
  509. .start = probes_seq_start,
  510. .next = probes_seq_next,
  511. .stop = probes_seq_stop,
  512. .show = probes_seq_show
  513. };
  514. static int probes_open(struct inode *inode, struct file *file)
  515. {
  516. int ret;
  517. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  518. ret = cleanup_all_probes();
  519. if (ret)
  520. return ret;
  521. }
  522. return seq_open(file, &probes_seq_op);
  523. }
  524. static ssize_t probes_write(struct file *file, const char __user *buffer,
  525. size_t count, loff_t *ppos)
  526. {
  527. return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
  528. }
  529. static const struct file_operations uprobe_events_ops = {
  530. .owner = THIS_MODULE,
  531. .open = probes_open,
  532. .read = seq_read,
  533. .llseek = seq_lseek,
  534. .release = seq_release,
  535. .write = probes_write,
  536. };
  537. /* Probes profiling interfaces */
  538. static int probes_profile_seq_show(struct seq_file *m, void *v)
  539. {
  540. struct trace_uprobe *tu = v;
  541. seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->tp.call.name, tu->nhit);
  542. return 0;
  543. }
  544. static const struct seq_operations profile_seq_op = {
  545. .start = probes_seq_start,
  546. .next = probes_seq_next,
  547. .stop = probes_seq_stop,
  548. .show = probes_profile_seq_show
  549. };
  550. static int profile_open(struct inode *inode, struct file *file)
  551. {
  552. return seq_open(file, &profile_seq_op);
  553. }
  554. static const struct file_operations uprobe_profile_ops = {
  555. .owner = THIS_MODULE,
  556. .open = profile_open,
  557. .read = seq_read,
  558. .llseek = seq_lseek,
  559. .release = seq_release,
  560. };
  561. struct uprobe_cpu_buffer {
  562. struct mutex mutex;
  563. void *buf;
  564. };
  565. static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
  566. static int uprobe_buffer_refcnt;
  567. static int uprobe_buffer_init(void)
  568. {
  569. int cpu, err_cpu;
  570. uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
  571. if (uprobe_cpu_buffer == NULL)
  572. return -ENOMEM;
  573. for_each_possible_cpu(cpu) {
  574. struct page *p = alloc_pages_node(cpu_to_node(cpu),
  575. GFP_KERNEL, 0);
  576. if (p == NULL) {
  577. err_cpu = cpu;
  578. goto err;
  579. }
  580. per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
  581. mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
  582. }
  583. return 0;
  584. err:
  585. for_each_possible_cpu(cpu) {
  586. if (cpu == err_cpu)
  587. break;
  588. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
  589. }
  590. free_percpu(uprobe_cpu_buffer);
  591. return -ENOMEM;
  592. }
  593. static int uprobe_buffer_enable(void)
  594. {
  595. int ret = 0;
  596. BUG_ON(!mutex_is_locked(&event_mutex));
  597. if (uprobe_buffer_refcnt++ == 0) {
  598. ret = uprobe_buffer_init();
  599. if (ret < 0)
  600. uprobe_buffer_refcnt--;
  601. }
  602. return ret;
  603. }
  604. static void uprobe_buffer_disable(void)
  605. {
  606. BUG_ON(!mutex_is_locked(&event_mutex));
  607. if (--uprobe_buffer_refcnt == 0) {
  608. free_percpu(uprobe_cpu_buffer);
  609. uprobe_cpu_buffer = NULL;
  610. }
  611. }
  612. static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
  613. {
  614. struct uprobe_cpu_buffer *ucb;
  615. int cpu;
  616. cpu = raw_smp_processor_id();
  617. ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
  618. /*
  619. * Use per-cpu buffers for fastest access, but we might migrate
  620. * so the mutex makes sure we have sole access to it.
  621. */
  622. mutex_lock(&ucb->mutex);
  623. return ucb;
  624. }
  625. static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
  626. {
  627. mutex_unlock(&ucb->mutex);
  628. }
  629. static void uprobe_trace_print(struct trace_uprobe *tu,
  630. unsigned long func, struct pt_regs *regs)
  631. {
  632. struct uprobe_trace_entry_head *entry;
  633. struct ring_buffer_event *event;
  634. struct ring_buffer *buffer;
  635. struct uprobe_cpu_buffer *ucb;
  636. void *data;
  637. int size, dsize, esize;
  638. struct ftrace_event_call *call = &tu->tp.call;
  639. dsize = __get_data_size(&tu->tp, regs);
  640. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  641. if (WARN_ON_ONCE(!uprobe_cpu_buffer || tu->tp.size + dsize > PAGE_SIZE))
  642. return;
  643. ucb = uprobe_buffer_get();
  644. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  645. size = esize + tu->tp.size + dsize;
  646. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  647. size, 0, 0);
  648. if (!event)
  649. goto out;
  650. entry = ring_buffer_event_data(event);
  651. if (is_ret_probe(tu)) {
  652. entry->vaddr[0] = func;
  653. entry->vaddr[1] = instruction_pointer(regs);
  654. data = DATAOF_TRACE_ENTRY(entry, true);
  655. } else {
  656. entry->vaddr[0] = instruction_pointer(regs);
  657. data = DATAOF_TRACE_ENTRY(entry, false);
  658. }
  659. memcpy(data, ucb->buf, tu->tp.size + dsize);
  660. if (!call_filter_check_discard(call, entry, buffer, event))
  661. trace_buffer_unlock_commit(buffer, event, 0, 0);
  662. out:
  663. uprobe_buffer_put(ucb);
  664. }
  665. /* uprobe handler */
  666. static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
  667. {
  668. if (!is_ret_probe(tu))
  669. uprobe_trace_print(tu, 0, regs);
  670. return 0;
  671. }
  672. static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  673. struct pt_regs *regs)
  674. {
  675. uprobe_trace_print(tu, func, regs);
  676. }
  677. /* Event entry printers */
  678. static enum print_line_t
  679. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  680. {
  681. struct uprobe_trace_entry_head *entry;
  682. struct trace_seq *s = &iter->seq;
  683. struct trace_uprobe *tu;
  684. u8 *data;
  685. int i;
  686. entry = (struct uprobe_trace_entry_head *)iter->ent;
  687. tu = container_of(event, struct trace_uprobe, tp.call.event);
  688. if (is_ret_probe(tu)) {
  689. if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->tp.call.name,
  690. entry->vaddr[1], entry->vaddr[0]))
  691. goto partial;
  692. data = DATAOF_TRACE_ENTRY(entry, true);
  693. } else {
  694. if (!trace_seq_printf(s, "%s: (0x%lx)", tu->tp.call.name,
  695. entry->vaddr[0]))
  696. goto partial;
  697. data = DATAOF_TRACE_ENTRY(entry, false);
  698. }
  699. for (i = 0; i < tu->tp.nr_args; i++) {
  700. struct probe_arg *parg = &tu->tp.args[i];
  701. if (!parg->type->print(s, parg->name, data + parg->offset, entry))
  702. goto partial;
  703. }
  704. if (trace_seq_puts(s, "\n"))
  705. return TRACE_TYPE_HANDLED;
  706. partial:
  707. return TRACE_TYPE_PARTIAL_LINE;
  708. }
  709. typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  710. enum uprobe_filter_ctx ctx,
  711. struct mm_struct *mm);
  712. static int
  713. probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
  714. {
  715. int ret = 0;
  716. if (trace_probe_is_enabled(&tu->tp))
  717. return -EINTR;
  718. ret = uprobe_buffer_enable();
  719. if (ret < 0)
  720. return ret;
  721. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  722. tu->tp.flags |= flag;
  723. tu->consumer.filter = filter;
  724. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  725. if (ret)
  726. tu->tp.flags &= ~flag;
  727. return ret;
  728. }
  729. static void probe_event_disable(struct trace_uprobe *tu, int flag)
  730. {
  731. if (!trace_probe_is_enabled(&tu->tp))
  732. return;
  733. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  734. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  735. tu->tp.flags &= ~flag;
  736. uprobe_buffer_disable();
  737. }
  738. static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
  739. {
  740. int ret, i, size;
  741. struct uprobe_trace_entry_head field;
  742. struct trace_uprobe *tu = event_call->data;
  743. if (is_ret_probe(tu)) {
  744. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  745. DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  746. size = SIZEOF_TRACE_ENTRY(true);
  747. } else {
  748. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  749. size = SIZEOF_TRACE_ENTRY(false);
  750. }
  751. /* Set argument names as fields */
  752. for (i = 0; i < tu->tp.nr_args; i++) {
  753. struct probe_arg *parg = &tu->tp.args[i];
  754. ret = trace_define_field(event_call, parg->type->fmttype,
  755. parg->name, size + parg->offset,
  756. parg->type->size, parg->type->is_signed,
  757. FILTER_OTHER);
  758. if (ret)
  759. return ret;
  760. }
  761. return 0;
  762. }
  763. #ifdef CONFIG_PERF_EVENTS
  764. static bool
  765. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  766. {
  767. struct perf_event *event;
  768. if (filter->nr_systemwide)
  769. return true;
  770. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  771. if (event->hw.tp_target->mm == mm)
  772. return true;
  773. }
  774. return false;
  775. }
  776. static inline bool
  777. uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
  778. {
  779. return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
  780. }
  781. static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
  782. {
  783. bool done;
  784. write_lock(&tu->filter.rwlock);
  785. if (event->hw.tp_target) {
  786. /*
  787. * event->parent != NULL means copy_process(), we can avoid
  788. * uprobe_apply(). current->mm must be probed and we can rely
  789. * on dup_mmap() which preserves the already installed bp's.
  790. *
  791. * attr.enable_on_exec means that exec/mmap will install the
  792. * breakpoints we need.
  793. */
  794. done = tu->filter.nr_systemwide ||
  795. event->parent || event->attr.enable_on_exec ||
  796. uprobe_filter_event(tu, event);
  797. list_add(&event->hw.tp_list, &tu->filter.perf_events);
  798. } else {
  799. done = tu->filter.nr_systemwide;
  800. tu->filter.nr_systemwide++;
  801. }
  802. write_unlock(&tu->filter.rwlock);
  803. if (!done)
  804. uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  805. return 0;
  806. }
  807. static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  808. {
  809. bool done;
  810. write_lock(&tu->filter.rwlock);
  811. if (event->hw.tp_target) {
  812. list_del(&event->hw.tp_list);
  813. done = tu->filter.nr_systemwide ||
  814. (event->hw.tp_target->flags & PF_EXITING) ||
  815. uprobe_filter_event(tu, event);
  816. } else {
  817. tu->filter.nr_systemwide--;
  818. done = tu->filter.nr_systemwide;
  819. }
  820. write_unlock(&tu->filter.rwlock);
  821. if (!done)
  822. uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  823. return 0;
  824. }
  825. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  826. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  827. {
  828. struct trace_uprobe *tu;
  829. int ret;
  830. tu = container_of(uc, struct trace_uprobe, consumer);
  831. read_lock(&tu->filter.rwlock);
  832. ret = __uprobe_perf_filter(&tu->filter, mm);
  833. read_unlock(&tu->filter.rwlock);
  834. return ret;
  835. }
  836. static void uprobe_perf_print(struct trace_uprobe *tu,
  837. unsigned long func, struct pt_regs *regs)
  838. {
  839. struct ftrace_event_call *call = &tu->tp.call;
  840. struct uprobe_trace_entry_head *entry;
  841. struct hlist_head *head;
  842. struct uprobe_cpu_buffer *ucb;
  843. void *data;
  844. int size, dsize, esize;
  845. int rctx;
  846. dsize = __get_data_size(&tu->tp, regs);
  847. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  848. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  849. return;
  850. size = esize + tu->tp.size + dsize;
  851. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  852. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  853. return;
  854. ucb = uprobe_buffer_get();
  855. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  856. preempt_disable();
  857. head = this_cpu_ptr(call->perf_events);
  858. if (hlist_empty(head))
  859. goto out;
  860. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  861. if (!entry)
  862. goto out;
  863. if (is_ret_probe(tu)) {
  864. entry->vaddr[0] = func;
  865. entry->vaddr[1] = instruction_pointer(regs);
  866. data = DATAOF_TRACE_ENTRY(entry, true);
  867. } else {
  868. entry->vaddr[0] = instruction_pointer(regs);
  869. data = DATAOF_TRACE_ENTRY(entry, false);
  870. }
  871. memcpy(data, ucb->buf, tu->tp.size + dsize);
  872. if (size - esize > tu->tp.size + dsize) {
  873. int len = tu->tp.size + dsize;
  874. memset(data + len, 0, size - esize - len);
  875. }
  876. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  877. out:
  878. preempt_enable();
  879. uprobe_buffer_put(ucb);
  880. }
  881. /* uprobe profile handler */
  882. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
  883. {
  884. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  885. return UPROBE_HANDLER_REMOVE;
  886. if (!is_ret_probe(tu))
  887. uprobe_perf_print(tu, 0, regs);
  888. return 0;
  889. }
  890. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  891. struct pt_regs *regs)
  892. {
  893. uprobe_perf_print(tu, func, regs);
  894. }
  895. #endif /* CONFIG_PERF_EVENTS */
  896. static
  897. int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
  898. {
  899. struct trace_uprobe *tu = event->data;
  900. switch (type) {
  901. case TRACE_REG_REGISTER:
  902. return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
  903. case TRACE_REG_UNREGISTER:
  904. probe_event_disable(tu, TP_FLAG_TRACE);
  905. return 0;
  906. #ifdef CONFIG_PERF_EVENTS
  907. case TRACE_REG_PERF_REGISTER:
  908. return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
  909. case TRACE_REG_PERF_UNREGISTER:
  910. probe_event_disable(tu, TP_FLAG_PROFILE);
  911. return 0;
  912. case TRACE_REG_PERF_OPEN:
  913. return uprobe_perf_open(tu, data);
  914. case TRACE_REG_PERF_CLOSE:
  915. return uprobe_perf_close(tu, data);
  916. #endif
  917. default:
  918. return 0;
  919. }
  920. return 0;
  921. }
  922. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  923. {
  924. struct trace_uprobe *tu;
  925. struct uprobe_dispatch_data udd;
  926. int ret = 0;
  927. tu = container_of(con, struct trace_uprobe, consumer);
  928. tu->nhit++;
  929. udd.tu = tu;
  930. udd.bp_addr = instruction_pointer(regs);
  931. current->utask->vaddr = (unsigned long) &udd;
  932. if (tu->tp.flags & TP_FLAG_TRACE)
  933. ret |= uprobe_trace_func(tu, regs);
  934. #ifdef CONFIG_PERF_EVENTS
  935. if (tu->tp.flags & TP_FLAG_PROFILE)
  936. ret |= uprobe_perf_func(tu, regs);
  937. #endif
  938. return ret;
  939. }
  940. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  941. unsigned long func, struct pt_regs *regs)
  942. {
  943. struct trace_uprobe *tu;
  944. struct uprobe_dispatch_data udd;
  945. tu = container_of(con, struct trace_uprobe, consumer);
  946. udd.tu = tu;
  947. udd.bp_addr = func;
  948. current->utask->vaddr = (unsigned long) &udd;
  949. if (tu->tp.flags & TP_FLAG_TRACE)
  950. uretprobe_trace_func(tu, func, regs);
  951. #ifdef CONFIG_PERF_EVENTS
  952. if (tu->tp.flags & TP_FLAG_PROFILE)
  953. uretprobe_perf_func(tu, func, regs);
  954. #endif
  955. return 0;
  956. }
  957. static struct trace_event_functions uprobe_funcs = {
  958. .trace = print_uprobe_event
  959. };
  960. static int register_uprobe_event(struct trace_uprobe *tu)
  961. {
  962. struct ftrace_event_call *call = &tu->tp.call;
  963. int ret;
  964. /* Initialize ftrace_event_call */
  965. INIT_LIST_HEAD(&call->class->fields);
  966. call->event.funcs = &uprobe_funcs;
  967. call->class->define_fields = uprobe_event_define_fields;
  968. if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
  969. return -ENOMEM;
  970. ret = register_ftrace_event(&call->event);
  971. if (!ret) {
  972. kfree(call->print_fmt);
  973. return -ENODEV;
  974. }
  975. call->flags = 0;
  976. call->class->reg = trace_uprobe_register;
  977. call->data = tu;
  978. ret = trace_add_event_call(call);
  979. if (ret) {
  980. pr_info("Failed to register uprobe event: %s\n", call->name);
  981. kfree(call->print_fmt);
  982. unregister_ftrace_event(&call->event);
  983. }
  984. return ret;
  985. }
  986. static int unregister_uprobe_event(struct trace_uprobe *tu)
  987. {
  988. int ret;
  989. /* tu->event is unregistered in trace_remove_event_call() */
  990. ret = trace_remove_event_call(&tu->tp.call);
  991. if (ret)
  992. return ret;
  993. kfree(tu->tp.call.print_fmt);
  994. tu->tp.call.print_fmt = NULL;
  995. return 0;
  996. }
  997. /* Make a trace interface for controling probe points */
  998. static __init int init_uprobe_trace(void)
  999. {
  1000. struct dentry *d_tracer;
  1001. d_tracer = tracing_init_dentry();
  1002. if (!d_tracer)
  1003. return 0;
  1004. trace_create_file("uprobe_events", 0644, d_tracer,
  1005. NULL, &uprobe_events_ops);
  1006. /* Profile interface */
  1007. trace_create_file("uprobe_profile", 0444, d_tracer,
  1008. NULL, &uprobe_profile_ops);
  1009. return 0;
  1010. }
  1011. fs_initcall(init_uprobe_trace);