trace_uprobe.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * uprobes-based tracing events
  4. *
  5. * Copyright (C) IBM Corporation, 2010-2012
  6. * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
  7. */
  8. #define pr_fmt(fmt) "trace_uprobe: " fmt
  9. #include <linux/module.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/uprobes.h>
  12. #include <linux/namei.h>
  13. #include <linux/string.h>
  14. #include <linux/rculist.h>
  15. #include "trace_probe.h"
  16. #define UPROBE_EVENT_SYSTEM "uprobes"
  17. struct uprobe_trace_entry_head {
  18. struct trace_entry ent;
  19. unsigned long vaddr[];
  20. };
  21. #define SIZEOF_TRACE_ENTRY(is_return) \
  22. (sizeof(struct uprobe_trace_entry_head) + \
  23. sizeof(unsigned long) * (is_return ? 2 : 1))
  24. #define DATAOF_TRACE_ENTRY(entry, is_return) \
  25. ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
  26. struct trace_uprobe_filter {
  27. rwlock_t rwlock;
  28. int nr_systemwide;
  29. struct list_head perf_events;
  30. };
  31. /*
  32. * uprobe event core functions
  33. */
  34. struct trace_uprobe {
  35. struct list_head list;
  36. struct trace_uprobe_filter filter;
  37. struct uprobe_consumer consumer;
  38. struct path path;
  39. struct inode *inode;
  40. char *filename;
  41. unsigned long offset;
  42. unsigned long nhit;
  43. struct trace_probe tp;
  44. };
  45. #define SIZEOF_TRACE_UPROBE(n) \
  46. (offsetof(struct trace_uprobe, tp.args) + \
  47. (sizeof(struct probe_arg) * (n)))
  48. static int register_uprobe_event(struct trace_uprobe *tu);
  49. static int unregister_uprobe_event(struct trace_uprobe *tu);
  50. static DEFINE_MUTEX(uprobe_lock);
  51. static LIST_HEAD(uprobe_list);
  52. struct uprobe_dispatch_data {
  53. struct trace_uprobe *tu;
  54. unsigned long bp_addr;
  55. };
  56. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
  57. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  58. unsigned long func, struct pt_regs *regs);
  59. #ifdef CONFIG_STACK_GROWSUP
  60. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  61. {
  62. return addr - (n * sizeof(long));
  63. }
  64. #else
  65. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  66. {
  67. return addr + (n * sizeof(long));
  68. }
  69. #endif
  70. static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
  71. {
  72. unsigned long ret;
  73. unsigned long addr = user_stack_pointer(regs);
  74. addr = adjust_stack_addr(addr, n);
  75. if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
  76. return 0;
  77. return ret;
  78. }
  79. /*
  80. * Uprobes-specific fetch functions
  81. */
  82. #define DEFINE_FETCH_stack(type) \
  83. static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
  84. void *offset, void *dest) \
  85. { \
  86. *(type *)dest = (type)get_user_stack_nth(regs, \
  87. ((unsigned long)offset)); \
  88. }
  89. DEFINE_BASIC_FETCH_FUNCS(stack)
  90. /* No string on the stack entry */
  91. #define fetch_stack_string NULL
  92. #define fetch_stack_string_size NULL
  93. #define DEFINE_FETCH_memory(type) \
  94. static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
  95. void *addr, void *dest) \
  96. { \
  97. type retval; \
  98. void __user *vaddr = (void __force __user *) addr; \
  99. \
  100. if (copy_from_user(&retval, vaddr, sizeof(type))) \
  101. *(type *)dest = 0; \
  102. else \
  103. *(type *) dest = retval; \
  104. }
  105. DEFINE_BASIC_FETCH_FUNCS(memory)
  106. /*
  107. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  108. * length and relative data location.
  109. */
  110. static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  111. void *addr, void *dest)
  112. {
  113. long ret;
  114. u32 rloc = *(u32 *)dest;
  115. int maxlen = get_rloc_len(rloc);
  116. u8 *dst = get_rloc_data(dest);
  117. void __user *src = (void __force __user *) addr;
  118. if (!maxlen)
  119. return;
  120. ret = strncpy_from_user(dst, src, maxlen);
  121. if (ret == maxlen)
  122. dst[ret - 1] = '\0';
  123. else if (ret >= 0)
  124. /*
  125. * Include the terminating null byte. In this case it
  126. * was copied by strncpy_from_user but not accounted
  127. * for in ret.
  128. */
  129. ret++;
  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 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 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. static 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_ALIAS(x8, u8, u8, 0),
  184. ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
  185. ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
  186. ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
  187. ASSIGN_FETCH_TYPE_END
  188. };
  189. static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
  190. {
  191. rwlock_init(&filter->rwlock);
  192. filter->nr_systemwide = 0;
  193. INIT_LIST_HEAD(&filter->perf_events);
  194. }
  195. static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
  196. {
  197. return !filter->nr_systemwide && list_empty(&filter->perf_events);
  198. }
  199. static inline bool is_ret_probe(struct trace_uprobe *tu)
  200. {
  201. return tu->consumer.ret_handler != NULL;
  202. }
  203. /*
  204. * Allocate new trace_uprobe and initialize it (including uprobes).
  205. */
  206. static struct trace_uprobe *
  207. alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
  208. {
  209. struct trace_uprobe *tu;
  210. if (!event || !is_good_name(event))
  211. return ERR_PTR(-EINVAL);
  212. if (!group || !is_good_name(group))
  213. return ERR_PTR(-EINVAL);
  214. tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
  215. if (!tu)
  216. return ERR_PTR(-ENOMEM);
  217. tu->tp.call.class = &tu->tp.class;
  218. tu->tp.call.name = kstrdup(event, GFP_KERNEL);
  219. if (!tu->tp.call.name)
  220. goto error;
  221. tu->tp.class.system = kstrdup(group, GFP_KERNEL);
  222. if (!tu->tp.class.system)
  223. goto error;
  224. INIT_LIST_HEAD(&tu->list);
  225. INIT_LIST_HEAD(&tu->tp.files);
  226. tu->consumer.handler = uprobe_dispatcher;
  227. if (is_ret)
  228. tu->consumer.ret_handler = uretprobe_dispatcher;
  229. init_trace_uprobe_filter(&tu->filter);
  230. return tu;
  231. error:
  232. kfree(tu->tp.call.name);
  233. kfree(tu);
  234. return ERR_PTR(-ENOMEM);
  235. }
  236. static void free_trace_uprobe(struct trace_uprobe *tu)
  237. {
  238. int i;
  239. for (i = 0; i < tu->tp.nr_args; i++)
  240. traceprobe_free_probe_arg(&tu->tp.args[i]);
  241. path_put(&tu->path);
  242. kfree(tu->tp.call.class->system);
  243. kfree(tu->tp.call.name);
  244. kfree(tu->filename);
  245. kfree(tu);
  246. }
  247. static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  248. {
  249. struct trace_uprobe *tu;
  250. list_for_each_entry(tu, &uprobe_list, list)
  251. if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
  252. strcmp(tu->tp.call.class->system, group) == 0)
  253. return tu;
  254. return NULL;
  255. }
  256. /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
  257. static int unregister_trace_uprobe(struct trace_uprobe *tu)
  258. {
  259. int ret;
  260. ret = unregister_uprobe_event(tu);
  261. if (ret)
  262. return ret;
  263. list_del(&tu->list);
  264. free_trace_uprobe(tu);
  265. return 0;
  266. }
  267. /* Register a trace_uprobe and probe_event */
  268. static int register_trace_uprobe(struct trace_uprobe *tu)
  269. {
  270. struct trace_uprobe *old_tu;
  271. int ret;
  272. mutex_lock(&uprobe_lock);
  273. /* register as an event */
  274. old_tu = find_probe_event(trace_event_name(&tu->tp.call),
  275. tu->tp.call.class->system);
  276. if (old_tu) {
  277. /* delete old event */
  278. ret = unregister_trace_uprobe(old_tu);
  279. if (ret)
  280. goto end;
  281. }
  282. ret = register_uprobe_event(tu);
  283. if (ret) {
  284. pr_warn("Failed to register probe event(%d)\n", ret);
  285. goto end;
  286. }
  287. list_add_tail(&tu->list, &uprobe_list);
  288. end:
  289. mutex_unlock(&uprobe_lock);
  290. return ret;
  291. }
  292. /*
  293. * Argument syntax:
  294. * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
  295. *
  296. * - Remove uprobe: -:[GRP/]EVENT
  297. */
  298. static int create_trace_uprobe(int argc, char **argv)
  299. {
  300. struct trace_uprobe *tu;
  301. char *arg, *event, *group, *filename;
  302. char buf[MAX_EVENT_NAME_LEN];
  303. struct path path;
  304. unsigned long offset;
  305. bool is_delete, is_return;
  306. int i, ret;
  307. ret = 0;
  308. is_delete = false;
  309. is_return = false;
  310. event = NULL;
  311. group = NULL;
  312. /* argc must be >= 1 */
  313. if (argv[0][0] == '-')
  314. is_delete = true;
  315. else if (argv[0][0] == 'r')
  316. is_return = true;
  317. else if (argv[0][0] != 'p') {
  318. pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
  319. return -EINVAL;
  320. }
  321. if (argv[0][1] == ':') {
  322. event = &argv[0][2];
  323. arg = strchr(event, '/');
  324. if (arg) {
  325. group = event;
  326. event = arg + 1;
  327. event[-1] = '\0';
  328. if (strlen(group) == 0) {
  329. pr_info("Group name is not specified\n");
  330. return -EINVAL;
  331. }
  332. }
  333. if (strlen(event) == 0) {
  334. pr_info("Event name is not specified\n");
  335. return -EINVAL;
  336. }
  337. }
  338. if (!group)
  339. group = UPROBE_EVENT_SYSTEM;
  340. if (is_delete) {
  341. int ret;
  342. if (!event) {
  343. pr_info("Delete command needs an event name.\n");
  344. return -EINVAL;
  345. }
  346. mutex_lock(&uprobe_lock);
  347. tu = find_probe_event(event, group);
  348. if (!tu) {
  349. mutex_unlock(&uprobe_lock);
  350. pr_info("Event %s/%s doesn't exist.\n", group, event);
  351. return -ENOENT;
  352. }
  353. /* delete an event */
  354. ret = unregister_trace_uprobe(tu);
  355. mutex_unlock(&uprobe_lock);
  356. return ret;
  357. }
  358. if (argc < 2) {
  359. pr_info("Probe point is not specified.\n");
  360. return -EINVAL;
  361. }
  362. /* Find the last occurrence, in case the path contains ':' too. */
  363. arg = strrchr(argv[1], ':');
  364. if (!arg)
  365. return -EINVAL;
  366. *arg++ = '\0';
  367. filename = argv[1];
  368. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  369. if (ret)
  370. return ret;
  371. if (!d_is_reg(path.dentry)) {
  372. ret = -EINVAL;
  373. goto fail_address_parse;
  374. }
  375. ret = kstrtoul(arg, 0, &offset);
  376. if (ret)
  377. goto fail_address_parse;
  378. argc -= 2;
  379. argv += 2;
  380. /* setup a probe */
  381. if (!event) {
  382. char *tail;
  383. char *ptr;
  384. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  385. if (!tail) {
  386. ret = -ENOMEM;
  387. goto fail_address_parse;
  388. }
  389. ptr = strpbrk(tail, ".-_");
  390. if (ptr)
  391. *ptr = '\0';
  392. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  393. event = buf;
  394. kfree(tail);
  395. }
  396. tu = alloc_trace_uprobe(group, event, argc, is_return);
  397. if (IS_ERR(tu)) {
  398. pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
  399. ret = PTR_ERR(tu);
  400. goto fail_address_parse;
  401. }
  402. tu->offset = offset;
  403. tu->path = path;
  404. tu->filename = kstrdup(filename, GFP_KERNEL);
  405. if (!tu->filename) {
  406. pr_info("Failed to allocate filename.\n");
  407. ret = -ENOMEM;
  408. goto error;
  409. }
  410. /* parse arguments */
  411. ret = 0;
  412. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  413. struct probe_arg *parg = &tu->tp.args[i];
  414. /* Increment count for freeing args in error case */
  415. tu->tp.nr_args++;
  416. /* Parse argument name */
  417. arg = strchr(argv[i], '=');
  418. if (arg) {
  419. *arg++ = '\0';
  420. parg->name = kstrdup(argv[i], GFP_KERNEL);
  421. } else {
  422. arg = argv[i];
  423. /* If argument name is omitted, set "argN" */
  424. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  425. parg->name = kstrdup(buf, GFP_KERNEL);
  426. }
  427. if (!parg->name) {
  428. pr_info("Failed to allocate argument[%d] name.\n", i);
  429. ret = -ENOMEM;
  430. goto error;
  431. }
  432. if (!is_good_name(parg->name)) {
  433. pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
  434. ret = -EINVAL;
  435. goto error;
  436. }
  437. if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
  438. pr_info("Argument[%d] name '%s' conflicts with "
  439. "another field.\n", i, argv[i]);
  440. ret = -EINVAL;
  441. goto error;
  442. }
  443. /* Parse fetch argument */
  444. ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
  445. is_return, false,
  446. uprobes_fetch_type_table);
  447. if (ret) {
  448. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  449. goto error;
  450. }
  451. }
  452. ret = register_trace_uprobe(tu);
  453. if (ret)
  454. goto error;
  455. return 0;
  456. error:
  457. free_trace_uprobe(tu);
  458. return ret;
  459. fail_address_parse:
  460. path_put(&path);
  461. pr_info("Failed to parse address or file.\n");
  462. return ret;
  463. }
  464. static int cleanup_all_probes(void)
  465. {
  466. struct trace_uprobe *tu;
  467. int ret = 0;
  468. mutex_lock(&uprobe_lock);
  469. while (!list_empty(&uprobe_list)) {
  470. tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
  471. ret = unregister_trace_uprobe(tu);
  472. if (ret)
  473. break;
  474. }
  475. mutex_unlock(&uprobe_lock);
  476. return ret;
  477. }
  478. /* Probes listing interfaces */
  479. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  480. {
  481. mutex_lock(&uprobe_lock);
  482. return seq_list_start(&uprobe_list, *pos);
  483. }
  484. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  485. {
  486. return seq_list_next(v, &uprobe_list, pos);
  487. }
  488. static void probes_seq_stop(struct seq_file *m, void *v)
  489. {
  490. mutex_unlock(&uprobe_lock);
  491. }
  492. static int probes_seq_show(struct seq_file *m, void *v)
  493. {
  494. struct trace_uprobe *tu = v;
  495. char c = is_ret_probe(tu) ? 'r' : 'p';
  496. int i;
  497. seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
  498. trace_event_name(&tu->tp.call), tu->filename,
  499. (int)(sizeof(void *) * 2), tu->offset);
  500. for (i = 0; i < tu->tp.nr_args; i++)
  501. seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
  502. seq_putc(m, '\n');
  503. return 0;
  504. }
  505. static const struct seq_operations probes_seq_op = {
  506. .start = probes_seq_start,
  507. .next = probes_seq_next,
  508. .stop = probes_seq_stop,
  509. .show = probes_seq_show
  510. };
  511. static int probes_open(struct inode *inode, struct file *file)
  512. {
  513. int ret;
  514. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  515. ret = cleanup_all_probes();
  516. if (ret)
  517. return ret;
  518. }
  519. return seq_open(file, &probes_seq_op);
  520. }
  521. static ssize_t probes_write(struct file *file, const char __user *buffer,
  522. size_t count, loff_t *ppos)
  523. {
  524. return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
  525. }
  526. static const struct file_operations uprobe_events_ops = {
  527. .owner = THIS_MODULE,
  528. .open = probes_open,
  529. .read = seq_read,
  530. .llseek = seq_lseek,
  531. .release = seq_release,
  532. .write = probes_write,
  533. };
  534. /* Probes profiling interfaces */
  535. static int probes_profile_seq_show(struct seq_file *m, void *v)
  536. {
  537. struct trace_uprobe *tu = v;
  538. seq_printf(m, " %s %-44s %15lu\n", tu->filename,
  539. trace_event_name(&tu->tp.call), tu->nhit);
  540. return 0;
  541. }
  542. static const struct seq_operations profile_seq_op = {
  543. .start = probes_seq_start,
  544. .next = probes_seq_next,
  545. .stop = probes_seq_stop,
  546. .show = probes_profile_seq_show
  547. };
  548. static int profile_open(struct inode *inode, struct file *file)
  549. {
  550. return seq_open(file, &profile_seq_op);
  551. }
  552. static const struct file_operations uprobe_profile_ops = {
  553. .owner = THIS_MODULE,
  554. .open = profile_open,
  555. .read = seq_read,
  556. .llseek = seq_lseek,
  557. .release = seq_release,
  558. };
  559. struct uprobe_cpu_buffer {
  560. struct mutex mutex;
  561. void *buf;
  562. };
  563. static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
  564. static int uprobe_buffer_refcnt;
  565. static int uprobe_buffer_init(void)
  566. {
  567. int cpu, err_cpu;
  568. uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
  569. if (uprobe_cpu_buffer == NULL)
  570. return -ENOMEM;
  571. for_each_possible_cpu(cpu) {
  572. struct page *p = alloc_pages_node(cpu_to_node(cpu),
  573. GFP_KERNEL, 0);
  574. if (p == NULL) {
  575. err_cpu = cpu;
  576. goto err;
  577. }
  578. per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
  579. mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
  580. }
  581. return 0;
  582. err:
  583. for_each_possible_cpu(cpu) {
  584. if (cpu == err_cpu)
  585. break;
  586. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
  587. }
  588. free_percpu(uprobe_cpu_buffer);
  589. return -ENOMEM;
  590. }
  591. static int uprobe_buffer_enable(void)
  592. {
  593. int ret = 0;
  594. BUG_ON(!mutex_is_locked(&event_mutex));
  595. if (uprobe_buffer_refcnt++ == 0) {
  596. ret = uprobe_buffer_init();
  597. if (ret < 0)
  598. uprobe_buffer_refcnt--;
  599. }
  600. return ret;
  601. }
  602. static void uprobe_buffer_disable(void)
  603. {
  604. int cpu;
  605. BUG_ON(!mutex_is_locked(&event_mutex));
  606. if (--uprobe_buffer_refcnt == 0) {
  607. for_each_possible_cpu(cpu)
  608. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
  609. cpu)->buf);
  610. free_percpu(uprobe_cpu_buffer);
  611. uprobe_cpu_buffer = NULL;
  612. }
  613. }
  614. static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
  615. {
  616. struct uprobe_cpu_buffer *ucb;
  617. int cpu;
  618. cpu = raw_smp_processor_id();
  619. ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
  620. /*
  621. * Use per-cpu buffers for fastest access, but we might migrate
  622. * so the mutex makes sure we have sole access to it.
  623. */
  624. mutex_lock(&ucb->mutex);
  625. return ucb;
  626. }
  627. static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
  628. {
  629. mutex_unlock(&ucb->mutex);
  630. }
  631. static void __uprobe_trace_func(struct trace_uprobe *tu,
  632. unsigned long func, struct pt_regs *regs,
  633. struct uprobe_cpu_buffer *ucb, int dsize,
  634. struct trace_event_file *trace_file)
  635. {
  636. struct uprobe_trace_entry_head *entry;
  637. struct ring_buffer_event *event;
  638. struct ring_buffer *buffer;
  639. void *data;
  640. int size, esize;
  641. struct trace_event_call *call = &tu->tp.call;
  642. WARN_ON(call != trace_file->event_call);
  643. if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
  644. return;
  645. if (trace_trigger_soft_disabled(trace_file))
  646. return;
  647. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  648. size = esize + tu->tp.size + dsize;
  649. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  650. call->event.type, size, 0, 0);
  651. if (!event)
  652. return;
  653. entry = ring_buffer_event_data(event);
  654. if (is_ret_probe(tu)) {
  655. entry->vaddr[0] = func;
  656. entry->vaddr[1] = instruction_pointer(regs);
  657. data = DATAOF_TRACE_ENTRY(entry, true);
  658. } else {
  659. entry->vaddr[0] = instruction_pointer(regs);
  660. data = DATAOF_TRACE_ENTRY(entry, false);
  661. }
  662. memcpy(data, ucb->buf, tu->tp.size + dsize);
  663. event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
  664. }
  665. /* uprobe handler */
  666. static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
  667. struct uprobe_cpu_buffer *ucb, int dsize)
  668. {
  669. struct event_file_link *link;
  670. if (is_ret_probe(tu))
  671. return 0;
  672. rcu_read_lock();
  673. list_for_each_entry_rcu(link, &tu->tp.files, list)
  674. __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
  675. rcu_read_unlock();
  676. return 0;
  677. }
  678. static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  679. struct pt_regs *regs,
  680. struct uprobe_cpu_buffer *ucb, int dsize)
  681. {
  682. struct event_file_link *link;
  683. rcu_read_lock();
  684. list_for_each_entry_rcu(link, &tu->tp.files, list)
  685. __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
  686. rcu_read_unlock();
  687. }
  688. /* Event entry printers */
  689. static enum print_line_t
  690. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  691. {
  692. struct uprobe_trace_entry_head *entry;
  693. struct trace_seq *s = &iter->seq;
  694. struct trace_uprobe *tu;
  695. u8 *data;
  696. int i;
  697. entry = (struct uprobe_trace_entry_head *)iter->ent;
  698. tu = container_of(event, struct trace_uprobe, tp.call.event);
  699. if (is_ret_probe(tu)) {
  700. trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
  701. trace_event_name(&tu->tp.call),
  702. entry->vaddr[1], entry->vaddr[0]);
  703. data = DATAOF_TRACE_ENTRY(entry, true);
  704. } else {
  705. trace_seq_printf(s, "%s: (0x%lx)",
  706. trace_event_name(&tu->tp.call),
  707. entry->vaddr[0]);
  708. data = DATAOF_TRACE_ENTRY(entry, false);
  709. }
  710. for (i = 0; i < tu->tp.nr_args; i++) {
  711. struct probe_arg *parg = &tu->tp.args[i];
  712. if (!parg->type->print(s, parg->name, data + parg->offset, entry))
  713. goto out;
  714. }
  715. trace_seq_putc(s, '\n');
  716. out:
  717. return trace_handle_return(s);
  718. }
  719. typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  720. enum uprobe_filter_ctx ctx,
  721. struct mm_struct *mm);
  722. static int
  723. probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
  724. filter_func_t filter)
  725. {
  726. bool enabled = trace_probe_is_enabled(&tu->tp);
  727. struct event_file_link *link = NULL;
  728. int ret;
  729. if (file) {
  730. if (tu->tp.flags & TP_FLAG_PROFILE)
  731. return -EINTR;
  732. link = kmalloc(sizeof(*link), GFP_KERNEL);
  733. if (!link)
  734. return -ENOMEM;
  735. link->file = file;
  736. list_add_tail_rcu(&link->list, &tu->tp.files);
  737. tu->tp.flags |= TP_FLAG_TRACE;
  738. } else {
  739. if (tu->tp.flags & TP_FLAG_TRACE)
  740. return -EINTR;
  741. tu->tp.flags |= TP_FLAG_PROFILE;
  742. }
  743. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  744. if (enabled)
  745. return 0;
  746. ret = uprobe_buffer_enable();
  747. if (ret)
  748. goto err_flags;
  749. tu->consumer.filter = filter;
  750. tu->inode = d_real_inode(tu->path.dentry);
  751. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  752. if (ret)
  753. goto err_buffer;
  754. return 0;
  755. err_buffer:
  756. uprobe_buffer_disable();
  757. err_flags:
  758. if (file) {
  759. list_del(&link->list);
  760. kfree(link);
  761. tu->tp.flags &= ~TP_FLAG_TRACE;
  762. } else {
  763. tu->tp.flags &= ~TP_FLAG_PROFILE;
  764. }
  765. return ret;
  766. }
  767. static void
  768. probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
  769. {
  770. if (!trace_probe_is_enabled(&tu->tp))
  771. return;
  772. if (file) {
  773. struct event_file_link *link;
  774. link = find_event_file_link(&tu->tp, file);
  775. if (!link)
  776. return;
  777. list_del_rcu(&link->list);
  778. /* synchronize with u{,ret}probe_trace_func */
  779. synchronize_rcu();
  780. kfree(link);
  781. if (!list_empty(&tu->tp.files))
  782. return;
  783. }
  784. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  785. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  786. tu->inode = NULL;
  787. tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
  788. uprobe_buffer_disable();
  789. }
  790. static int uprobe_event_define_fields(struct trace_event_call *event_call)
  791. {
  792. int ret, i, size;
  793. struct uprobe_trace_entry_head field;
  794. struct trace_uprobe *tu = event_call->data;
  795. if (is_ret_probe(tu)) {
  796. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  797. DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  798. size = SIZEOF_TRACE_ENTRY(true);
  799. } else {
  800. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  801. size = SIZEOF_TRACE_ENTRY(false);
  802. }
  803. /* Set argument names as fields */
  804. for (i = 0; i < tu->tp.nr_args; i++) {
  805. struct probe_arg *parg = &tu->tp.args[i];
  806. ret = trace_define_field(event_call, parg->type->fmttype,
  807. parg->name, size + parg->offset,
  808. parg->type->size, parg->type->is_signed,
  809. FILTER_OTHER);
  810. if (ret)
  811. return ret;
  812. }
  813. return 0;
  814. }
  815. #ifdef CONFIG_PERF_EVENTS
  816. static bool
  817. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  818. {
  819. struct perf_event *event;
  820. if (filter->nr_systemwide)
  821. return true;
  822. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  823. if (event->hw.target->mm == mm)
  824. return true;
  825. }
  826. return false;
  827. }
  828. static inline bool
  829. uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
  830. {
  831. return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
  832. }
  833. static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  834. {
  835. bool done;
  836. write_lock(&tu->filter.rwlock);
  837. if (event->hw.target) {
  838. list_del(&event->hw.tp_list);
  839. done = tu->filter.nr_systemwide ||
  840. (event->hw.target->flags & PF_EXITING) ||
  841. uprobe_filter_event(tu, event);
  842. } else {
  843. tu->filter.nr_systemwide--;
  844. done = tu->filter.nr_systemwide;
  845. }
  846. write_unlock(&tu->filter.rwlock);
  847. if (!done)
  848. return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  849. return 0;
  850. }
  851. static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
  852. {
  853. bool done;
  854. int err;
  855. write_lock(&tu->filter.rwlock);
  856. if (event->hw.target) {
  857. /*
  858. * event->parent != NULL means copy_process(), we can avoid
  859. * uprobe_apply(). current->mm must be probed and we can rely
  860. * on dup_mmap() which preserves the already installed bp's.
  861. *
  862. * attr.enable_on_exec means that exec/mmap will install the
  863. * breakpoints we need.
  864. */
  865. done = tu->filter.nr_systemwide ||
  866. event->parent || event->attr.enable_on_exec ||
  867. uprobe_filter_event(tu, event);
  868. list_add(&event->hw.tp_list, &tu->filter.perf_events);
  869. } else {
  870. done = tu->filter.nr_systemwide;
  871. tu->filter.nr_systemwide++;
  872. }
  873. write_unlock(&tu->filter.rwlock);
  874. err = 0;
  875. if (!done) {
  876. err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  877. if (err)
  878. uprobe_perf_close(tu, event);
  879. }
  880. return err;
  881. }
  882. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  883. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  884. {
  885. struct trace_uprobe *tu;
  886. int ret;
  887. tu = container_of(uc, struct trace_uprobe, consumer);
  888. read_lock(&tu->filter.rwlock);
  889. ret = __uprobe_perf_filter(&tu->filter, mm);
  890. read_unlock(&tu->filter.rwlock);
  891. return ret;
  892. }
  893. static void __uprobe_perf_func(struct trace_uprobe *tu,
  894. unsigned long func, struct pt_regs *regs,
  895. struct uprobe_cpu_buffer *ucb, int dsize)
  896. {
  897. struct trace_event_call *call = &tu->tp.call;
  898. struct uprobe_trace_entry_head *entry;
  899. struct hlist_head *head;
  900. void *data;
  901. int size, esize;
  902. int rctx;
  903. if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
  904. return;
  905. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  906. size = esize + tu->tp.size + dsize;
  907. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  908. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  909. return;
  910. preempt_disable();
  911. head = this_cpu_ptr(call->perf_events);
  912. if (hlist_empty(head))
  913. goto out;
  914. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  915. if (!entry)
  916. goto out;
  917. if (is_ret_probe(tu)) {
  918. entry->vaddr[0] = func;
  919. entry->vaddr[1] = instruction_pointer(regs);
  920. data = DATAOF_TRACE_ENTRY(entry, true);
  921. } else {
  922. entry->vaddr[0] = instruction_pointer(regs);
  923. data = DATAOF_TRACE_ENTRY(entry, false);
  924. }
  925. memcpy(data, ucb->buf, tu->tp.size + dsize);
  926. if (size - esize > tu->tp.size + dsize) {
  927. int len = tu->tp.size + dsize;
  928. memset(data + len, 0, size - esize - len);
  929. }
  930. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  931. head, NULL);
  932. out:
  933. preempt_enable();
  934. }
  935. /* uprobe profile handler */
  936. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
  937. struct uprobe_cpu_buffer *ucb, int dsize)
  938. {
  939. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  940. return UPROBE_HANDLER_REMOVE;
  941. if (!is_ret_probe(tu))
  942. __uprobe_perf_func(tu, 0, regs, ucb, dsize);
  943. return 0;
  944. }
  945. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  946. struct pt_regs *regs,
  947. struct uprobe_cpu_buffer *ucb, int dsize)
  948. {
  949. __uprobe_perf_func(tu, func, regs, ucb, dsize);
  950. }
  951. int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
  952. const char **filename, u64 *probe_offset,
  953. bool perf_type_tracepoint)
  954. {
  955. const char *pevent = trace_event_name(event->tp_event);
  956. const char *group = event->tp_event->class->system;
  957. struct trace_uprobe *tu;
  958. if (perf_type_tracepoint)
  959. tu = find_probe_event(pevent, group);
  960. else
  961. tu = event->tp_event->data;
  962. if (!tu)
  963. return -EINVAL;
  964. *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
  965. : BPF_FD_TYPE_UPROBE;
  966. *filename = tu->filename;
  967. *probe_offset = tu->offset;
  968. return 0;
  969. }
  970. #endif /* CONFIG_PERF_EVENTS */
  971. static int
  972. trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
  973. void *data)
  974. {
  975. struct trace_uprobe *tu = event->data;
  976. struct trace_event_file *file = data;
  977. switch (type) {
  978. case TRACE_REG_REGISTER:
  979. return probe_event_enable(tu, file, NULL);
  980. case TRACE_REG_UNREGISTER:
  981. probe_event_disable(tu, file);
  982. return 0;
  983. #ifdef CONFIG_PERF_EVENTS
  984. case TRACE_REG_PERF_REGISTER:
  985. return probe_event_enable(tu, NULL, uprobe_perf_filter);
  986. case TRACE_REG_PERF_UNREGISTER:
  987. probe_event_disable(tu, NULL);
  988. return 0;
  989. case TRACE_REG_PERF_OPEN:
  990. return uprobe_perf_open(tu, data);
  991. case TRACE_REG_PERF_CLOSE:
  992. return uprobe_perf_close(tu, data);
  993. #endif
  994. default:
  995. return 0;
  996. }
  997. return 0;
  998. }
  999. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  1000. {
  1001. struct trace_uprobe *tu;
  1002. struct uprobe_dispatch_data udd;
  1003. struct uprobe_cpu_buffer *ucb;
  1004. int dsize, esize;
  1005. int ret = 0;
  1006. tu = container_of(con, struct trace_uprobe, consumer);
  1007. tu->nhit++;
  1008. udd.tu = tu;
  1009. udd.bp_addr = instruction_pointer(regs);
  1010. current->utask->vaddr = (unsigned long) &udd;
  1011. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1012. return 0;
  1013. dsize = __get_data_size(&tu->tp, regs);
  1014. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1015. ucb = uprobe_buffer_get();
  1016. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  1017. if (tu->tp.flags & TP_FLAG_TRACE)
  1018. ret |= uprobe_trace_func(tu, regs, ucb, dsize);
  1019. #ifdef CONFIG_PERF_EVENTS
  1020. if (tu->tp.flags & TP_FLAG_PROFILE)
  1021. ret |= uprobe_perf_func(tu, regs, ucb, dsize);
  1022. #endif
  1023. uprobe_buffer_put(ucb);
  1024. return ret;
  1025. }
  1026. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  1027. unsigned long func, struct pt_regs *regs)
  1028. {
  1029. struct trace_uprobe *tu;
  1030. struct uprobe_dispatch_data udd;
  1031. struct uprobe_cpu_buffer *ucb;
  1032. int dsize, esize;
  1033. tu = container_of(con, struct trace_uprobe, consumer);
  1034. udd.tu = tu;
  1035. udd.bp_addr = func;
  1036. current->utask->vaddr = (unsigned long) &udd;
  1037. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1038. return 0;
  1039. dsize = __get_data_size(&tu->tp, regs);
  1040. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1041. ucb = uprobe_buffer_get();
  1042. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  1043. if (tu->tp.flags & TP_FLAG_TRACE)
  1044. uretprobe_trace_func(tu, func, regs, ucb, dsize);
  1045. #ifdef CONFIG_PERF_EVENTS
  1046. if (tu->tp.flags & TP_FLAG_PROFILE)
  1047. uretprobe_perf_func(tu, func, regs, ucb, dsize);
  1048. #endif
  1049. uprobe_buffer_put(ucb);
  1050. return 0;
  1051. }
  1052. static struct trace_event_functions uprobe_funcs = {
  1053. .trace = print_uprobe_event
  1054. };
  1055. static inline void init_trace_event_call(struct trace_uprobe *tu,
  1056. struct trace_event_call *call)
  1057. {
  1058. INIT_LIST_HEAD(&call->class->fields);
  1059. call->event.funcs = &uprobe_funcs;
  1060. call->class->define_fields = uprobe_event_define_fields;
  1061. call->flags = TRACE_EVENT_FL_UPROBE;
  1062. call->class->reg = trace_uprobe_register;
  1063. call->data = tu;
  1064. }
  1065. static int register_uprobe_event(struct trace_uprobe *tu)
  1066. {
  1067. struct trace_event_call *call = &tu->tp.call;
  1068. int ret = 0;
  1069. init_trace_event_call(tu, call);
  1070. if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
  1071. return -ENOMEM;
  1072. ret = register_trace_event(&call->event);
  1073. if (!ret) {
  1074. kfree(call->print_fmt);
  1075. return -ENODEV;
  1076. }
  1077. ret = trace_add_event_call(call);
  1078. if (ret) {
  1079. pr_info("Failed to register uprobe event: %s\n",
  1080. trace_event_name(call));
  1081. kfree(call->print_fmt);
  1082. unregister_trace_event(&call->event);
  1083. }
  1084. return ret;
  1085. }
  1086. static int unregister_uprobe_event(struct trace_uprobe *tu)
  1087. {
  1088. int ret;
  1089. /* tu->event is unregistered in trace_remove_event_call() */
  1090. ret = trace_remove_event_call(&tu->tp.call);
  1091. if (ret)
  1092. return ret;
  1093. kfree(tu->tp.call.print_fmt);
  1094. tu->tp.call.print_fmt = NULL;
  1095. return 0;
  1096. }
  1097. #ifdef CONFIG_PERF_EVENTS
  1098. struct trace_event_call *
  1099. create_local_trace_uprobe(char *name, unsigned long offs, bool is_return)
  1100. {
  1101. struct trace_uprobe *tu;
  1102. struct path path;
  1103. int ret;
  1104. ret = kern_path(name, LOOKUP_FOLLOW, &path);
  1105. if (ret)
  1106. return ERR_PTR(ret);
  1107. if (!d_is_reg(path.dentry)) {
  1108. path_put(&path);
  1109. return ERR_PTR(-EINVAL);
  1110. }
  1111. /*
  1112. * local trace_kprobes are not added to probe_list, so they are never
  1113. * searched in find_trace_kprobe(). Therefore, there is no concern of
  1114. * duplicated name "DUMMY_EVENT" here.
  1115. */
  1116. tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
  1117. is_return);
  1118. if (IS_ERR(tu)) {
  1119. pr_info("Failed to allocate trace_uprobe.(%d)\n",
  1120. (int)PTR_ERR(tu));
  1121. path_put(&path);
  1122. return ERR_CAST(tu);
  1123. }
  1124. tu->offset = offs;
  1125. tu->path = path;
  1126. tu->filename = kstrdup(name, GFP_KERNEL);
  1127. init_trace_event_call(tu, &tu->tp.call);
  1128. if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
  1129. ret = -ENOMEM;
  1130. goto error;
  1131. }
  1132. return &tu->tp.call;
  1133. error:
  1134. free_trace_uprobe(tu);
  1135. return ERR_PTR(ret);
  1136. }
  1137. void destroy_local_trace_uprobe(struct trace_event_call *event_call)
  1138. {
  1139. struct trace_uprobe *tu;
  1140. tu = container_of(event_call, struct trace_uprobe, tp.call);
  1141. kfree(tu->tp.call.print_fmt);
  1142. tu->tp.call.print_fmt = NULL;
  1143. free_trace_uprobe(tu);
  1144. }
  1145. #endif /* CONFIG_PERF_EVENTS */
  1146. /* Make a trace interface for controling probe points */
  1147. static __init int init_uprobe_trace(void)
  1148. {
  1149. struct dentry *d_tracer;
  1150. d_tracer = tracing_init_dentry();
  1151. if (IS_ERR(d_tracer))
  1152. return 0;
  1153. trace_create_file("uprobe_events", 0644, d_tracer,
  1154. NULL, &uprobe_events_ops);
  1155. /* Profile interface */
  1156. trace_create_file("uprobe_profile", 0444, d_tracer,
  1157. NULL, &uprobe_profile_ops);
  1158. return 0;
  1159. }
  1160. fs_initcall(init_uprobe_trace);