trace_uprobe.c 32 KB

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