trace_uprobe.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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 <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "trace_uprobe: " fmt
  9. #include <linux/bpf-cgroup.h>
  10. #include <linux/security.h>
  11. #include <linux/ctype.h>
  12. #include <linux/module.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/uprobes.h>
  15. #include <linux/namei.h>
  16. #include <linux/string.h>
  17. #include <linux/rculist.h>
  18. #include <linux/filter.h>
  19. #include "trace_dynevent.h"
  20. #include "trace_probe.h"
  21. #include "trace_probe_tmpl.h"
  22. #define UPROBE_EVENT_SYSTEM "uprobes"
  23. struct uprobe_trace_entry_head {
  24. struct trace_entry ent;
  25. unsigned long vaddr[];
  26. };
  27. #define SIZEOF_TRACE_ENTRY(is_return) \
  28. (sizeof(struct uprobe_trace_entry_head) + \
  29. sizeof(unsigned long) * (is_return ? 2 : 1))
  30. #define DATAOF_TRACE_ENTRY(entry, is_return) \
  31. ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
  32. static int trace_uprobe_create(const char *raw_command);
  33. static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
  34. static int trace_uprobe_release(struct dyn_event *ev);
  35. static bool trace_uprobe_is_busy(struct dyn_event *ev);
  36. static bool trace_uprobe_match(const char *system, const char *event,
  37. int argc, const char **argv, struct dyn_event *ev);
  38. static struct dyn_event_operations trace_uprobe_ops = {
  39. .create = trace_uprobe_create,
  40. .show = trace_uprobe_show,
  41. .is_busy = trace_uprobe_is_busy,
  42. .free = trace_uprobe_release,
  43. .match = trace_uprobe_match,
  44. };
  45. /*
  46. * uprobe event core functions
  47. */
  48. struct trace_uprobe {
  49. struct dyn_event devent;
  50. struct uprobe_consumer consumer;
  51. struct path path;
  52. struct inode *inode;
  53. char *filename;
  54. unsigned long offset;
  55. unsigned long ref_ctr_offset;
  56. unsigned long nhit;
  57. struct trace_probe tp;
  58. };
  59. static bool is_trace_uprobe(struct dyn_event *ev)
  60. {
  61. return ev->ops == &trace_uprobe_ops;
  62. }
  63. static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
  64. {
  65. return container_of(ev, struct trace_uprobe, devent);
  66. }
  67. /**
  68. * for_each_trace_uprobe - iterate over the trace_uprobe list
  69. * @pos: the struct trace_uprobe * for each entry
  70. * @dpos: the struct dyn_event * to use as a loop cursor
  71. */
  72. #define for_each_trace_uprobe(pos, dpos) \
  73. for_each_dyn_event(dpos) \
  74. if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
  75. static int register_uprobe_event(struct trace_uprobe *tu);
  76. static int unregister_uprobe_event(struct trace_uprobe *tu);
  77. struct uprobe_dispatch_data {
  78. struct trace_uprobe *tu;
  79. unsigned long bp_addr;
  80. };
  81. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
  82. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  83. unsigned long func, struct pt_regs *regs);
  84. #ifdef CONFIG_STACK_GROWSUP
  85. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  86. {
  87. return addr - (n * sizeof(long));
  88. }
  89. #else
  90. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  91. {
  92. return addr + (n * sizeof(long));
  93. }
  94. #endif
  95. static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
  96. {
  97. unsigned long ret;
  98. unsigned long addr = user_stack_pointer(regs);
  99. addr = adjust_stack_addr(addr, n);
  100. if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
  101. return 0;
  102. return ret;
  103. }
  104. /*
  105. * Uprobes-specific fetch functions
  106. */
  107. static nokprobe_inline int
  108. probe_mem_read(void *dest, void *src, size_t size)
  109. {
  110. void __user *vaddr = (void __force __user *)src;
  111. return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
  112. }
  113. static nokprobe_inline int
  114. probe_mem_read_user(void *dest, void *src, size_t size)
  115. {
  116. return probe_mem_read(dest, src, size);
  117. }
  118. /*
  119. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  120. * length and relative data location.
  121. */
  122. static nokprobe_inline int
  123. fetch_store_string(unsigned long addr, void *dest, void *base)
  124. {
  125. long ret;
  126. u32 loc = *(u32 *)dest;
  127. int maxlen = get_loc_len(loc);
  128. u8 *dst = get_loc_data(dest, base);
  129. void __user *src = (void __force __user *) addr;
  130. if (unlikely(!maxlen))
  131. return -ENOMEM;
  132. if (addr == FETCH_TOKEN_COMM)
  133. ret = strlcpy(dst, current->comm, maxlen);
  134. else
  135. ret = strncpy_from_user(dst, src, maxlen);
  136. if (ret >= 0) {
  137. if (ret == maxlen)
  138. dst[ret - 1] = '\0';
  139. else
  140. /*
  141. * Include the terminating null byte. In this case it
  142. * was copied by strncpy_from_user but not accounted
  143. * for in ret.
  144. */
  145. ret++;
  146. *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
  147. } else
  148. *(u32 *)dest = make_data_loc(0, (void *)dst - base);
  149. return ret;
  150. }
  151. static nokprobe_inline int
  152. fetch_store_string_user(unsigned long addr, void *dest, void *base)
  153. {
  154. return fetch_store_string(addr, dest, base);
  155. }
  156. /* Return the length of string -- including null terminal byte */
  157. static nokprobe_inline int
  158. fetch_store_strlen(unsigned long addr)
  159. {
  160. int len;
  161. void __user *vaddr = (void __force __user *) addr;
  162. if (addr == FETCH_TOKEN_COMM)
  163. len = strlen(current->comm) + 1;
  164. else
  165. len = strnlen_user(vaddr, MAX_STRING_SIZE);
  166. return (len > MAX_STRING_SIZE) ? 0 : len;
  167. }
  168. static nokprobe_inline int
  169. fetch_store_strlen_user(unsigned long addr)
  170. {
  171. return fetch_store_strlen(addr);
  172. }
  173. static unsigned long translate_user_vaddr(unsigned long file_offset)
  174. {
  175. unsigned long base_addr;
  176. struct uprobe_dispatch_data *udd;
  177. udd = (void *) current->utask->vaddr;
  178. base_addr = udd->bp_addr - udd->tu->offset;
  179. return base_addr + file_offset;
  180. }
  181. /* Note that we don't verify it, since the code does not come from user space */
  182. static int
  183. process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
  184. void *base)
  185. {
  186. struct pt_regs *regs = rec;
  187. unsigned long val;
  188. /* 1st stage: get value from context */
  189. switch (code->op) {
  190. case FETCH_OP_REG:
  191. val = regs_get_register(regs, code->param);
  192. break;
  193. case FETCH_OP_STACK:
  194. val = get_user_stack_nth(regs, code->param);
  195. break;
  196. case FETCH_OP_STACKP:
  197. val = user_stack_pointer(regs);
  198. break;
  199. case FETCH_OP_RETVAL:
  200. val = regs_return_value(regs);
  201. break;
  202. case FETCH_OP_IMM:
  203. val = code->immediate;
  204. break;
  205. case FETCH_OP_COMM:
  206. val = FETCH_TOKEN_COMM;
  207. break;
  208. case FETCH_OP_DATA:
  209. val = (unsigned long)code->data;
  210. break;
  211. case FETCH_OP_FOFFS:
  212. val = translate_user_vaddr(code->immediate);
  213. break;
  214. default:
  215. return -EILSEQ;
  216. }
  217. code++;
  218. return process_fetch_insn_bottom(code, val, dest, base);
  219. }
  220. NOKPROBE_SYMBOL(process_fetch_insn)
  221. static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
  222. {
  223. rwlock_init(&filter->rwlock);
  224. filter->nr_systemwide = 0;
  225. INIT_LIST_HEAD(&filter->perf_events);
  226. }
  227. static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
  228. {
  229. return !filter->nr_systemwide && list_empty(&filter->perf_events);
  230. }
  231. static inline bool is_ret_probe(struct trace_uprobe *tu)
  232. {
  233. return tu->consumer.ret_handler != NULL;
  234. }
  235. static bool trace_uprobe_is_busy(struct dyn_event *ev)
  236. {
  237. struct trace_uprobe *tu = to_trace_uprobe(ev);
  238. return trace_probe_is_enabled(&tu->tp);
  239. }
  240. static bool trace_uprobe_match_command_head(struct trace_uprobe *tu,
  241. int argc, const char **argv)
  242. {
  243. char buf[MAX_ARGSTR_LEN + 1];
  244. int len;
  245. if (!argc)
  246. return true;
  247. len = strlen(tu->filename);
  248. if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':')
  249. return false;
  250. if (tu->ref_ctr_offset == 0)
  251. snprintf(buf, sizeof(buf), "0x%0*lx",
  252. (int)(sizeof(void *) * 2), tu->offset);
  253. else
  254. snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)",
  255. (int)(sizeof(void *) * 2), tu->offset,
  256. tu->ref_ctr_offset);
  257. if (strcmp(buf, &argv[0][len + 1]))
  258. return false;
  259. argc--; argv++;
  260. return trace_probe_match_command_args(&tu->tp, argc, argv);
  261. }
  262. static bool trace_uprobe_match(const char *system, const char *event,
  263. int argc, const char **argv, struct dyn_event *ev)
  264. {
  265. struct trace_uprobe *tu = to_trace_uprobe(ev);
  266. return (event[0] == '\0' ||
  267. strcmp(trace_probe_name(&tu->tp), event) == 0) &&
  268. (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) &&
  269. trace_uprobe_match_command_head(tu, argc, argv);
  270. }
  271. static nokprobe_inline struct trace_uprobe *
  272. trace_uprobe_primary_from_call(struct trace_event_call *call)
  273. {
  274. struct trace_probe *tp;
  275. tp = trace_probe_primary_from_call(call);
  276. if (WARN_ON_ONCE(!tp))
  277. return NULL;
  278. return container_of(tp, struct trace_uprobe, tp);
  279. }
  280. /*
  281. * Allocate new trace_uprobe and initialize it (including uprobes).
  282. */
  283. static struct trace_uprobe *
  284. alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
  285. {
  286. struct trace_uprobe *tu;
  287. int ret;
  288. tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL);
  289. if (!tu)
  290. return ERR_PTR(-ENOMEM);
  291. ret = trace_probe_init(&tu->tp, event, group, true);
  292. if (ret < 0)
  293. goto error;
  294. dyn_event_init(&tu->devent, &trace_uprobe_ops);
  295. tu->consumer.handler = uprobe_dispatcher;
  296. if (is_ret)
  297. tu->consumer.ret_handler = uretprobe_dispatcher;
  298. init_trace_uprobe_filter(tu->tp.event->filter);
  299. return tu;
  300. error:
  301. kfree(tu);
  302. return ERR_PTR(ret);
  303. }
  304. static void free_trace_uprobe(struct trace_uprobe *tu)
  305. {
  306. if (!tu)
  307. return;
  308. path_put(&tu->path);
  309. trace_probe_cleanup(&tu->tp);
  310. kfree(tu->filename);
  311. kfree(tu);
  312. }
  313. static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  314. {
  315. struct dyn_event *pos;
  316. struct trace_uprobe *tu;
  317. for_each_trace_uprobe(tu, pos)
  318. if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&
  319. strcmp(trace_probe_group_name(&tu->tp), group) == 0)
  320. return tu;
  321. return NULL;
  322. }
  323. /* Unregister a trace_uprobe and probe_event */
  324. static int unregister_trace_uprobe(struct trace_uprobe *tu)
  325. {
  326. int ret;
  327. if (trace_probe_has_sibling(&tu->tp))
  328. goto unreg;
  329. /* If there's a reference to the dynamic event */
  330. if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp)))
  331. return -EBUSY;
  332. ret = unregister_uprobe_event(tu);
  333. if (ret)
  334. return ret;
  335. unreg:
  336. dyn_event_remove(&tu->devent);
  337. trace_probe_unlink(&tu->tp);
  338. free_trace_uprobe(tu);
  339. return 0;
  340. }
  341. static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig,
  342. struct trace_uprobe *comp)
  343. {
  344. struct trace_probe_event *tpe = orig->tp.event;
  345. struct inode *comp_inode = d_real_inode(comp->path.dentry);
  346. int i;
  347. list_for_each_entry(orig, &tpe->probes, tp.list) {
  348. if (comp_inode != d_real_inode(orig->path.dentry) ||
  349. comp->offset != orig->offset)
  350. continue;
  351. /*
  352. * trace_probe_compare_arg_type() ensured that nr_args and
  353. * each argument name and type are same. Let's compare comm.
  354. */
  355. for (i = 0; i < orig->tp.nr_args; i++) {
  356. if (strcmp(orig->tp.args[i].comm,
  357. comp->tp.args[i].comm))
  358. break;
  359. }
  360. if (i == orig->tp.nr_args)
  361. return true;
  362. }
  363. return false;
  364. }
  365. static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to)
  366. {
  367. int ret;
  368. ret = trace_probe_compare_arg_type(&tu->tp, &to->tp);
  369. if (ret) {
  370. /* Note that argument starts index = 2 */
  371. trace_probe_log_set_index(ret + 1);
  372. trace_probe_log_err(0, DIFF_ARG_TYPE);
  373. return -EEXIST;
  374. }
  375. if (trace_uprobe_has_same_uprobe(to, tu)) {
  376. trace_probe_log_set_index(0);
  377. trace_probe_log_err(0, SAME_PROBE);
  378. return -EEXIST;
  379. }
  380. /* Append to existing event */
  381. ret = trace_probe_append(&tu->tp, &to->tp);
  382. if (!ret)
  383. dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
  384. return ret;
  385. }
  386. /*
  387. * Uprobe with multiple reference counter is not allowed. i.e.
  388. * If inode and offset matches, reference counter offset *must*
  389. * match as well. Though, there is one exception: If user is
  390. * replacing old trace_uprobe with new one(same group/event),
  391. * then we allow same uprobe with new reference counter as far
  392. * as the new one does not conflict with any other existing
  393. * ones.
  394. */
  395. static int validate_ref_ctr_offset(struct trace_uprobe *new)
  396. {
  397. struct dyn_event *pos;
  398. struct trace_uprobe *tmp;
  399. struct inode *new_inode = d_real_inode(new->path.dentry);
  400. for_each_trace_uprobe(tmp, pos) {
  401. if (new_inode == d_real_inode(tmp->path.dentry) &&
  402. new->offset == tmp->offset &&
  403. new->ref_ctr_offset != tmp->ref_ctr_offset) {
  404. pr_warn("Reference counter offset mismatch.");
  405. return -EINVAL;
  406. }
  407. }
  408. return 0;
  409. }
  410. /* Register a trace_uprobe and probe_event */
  411. static int register_trace_uprobe(struct trace_uprobe *tu)
  412. {
  413. struct trace_uprobe *old_tu;
  414. int ret;
  415. mutex_lock(&event_mutex);
  416. ret = validate_ref_ctr_offset(tu);
  417. if (ret)
  418. goto end;
  419. /* register as an event */
  420. old_tu = find_probe_event(trace_probe_name(&tu->tp),
  421. trace_probe_group_name(&tu->tp));
  422. if (old_tu) {
  423. if (is_ret_probe(tu) != is_ret_probe(old_tu)) {
  424. trace_probe_log_set_index(0);
  425. trace_probe_log_err(0, DIFF_PROBE_TYPE);
  426. ret = -EEXIST;
  427. } else {
  428. ret = append_trace_uprobe(tu, old_tu);
  429. }
  430. goto end;
  431. }
  432. ret = register_uprobe_event(tu);
  433. if (ret) {
  434. if (ret == -EEXIST) {
  435. trace_probe_log_set_index(0);
  436. trace_probe_log_err(0, EVENT_EXIST);
  437. } else
  438. pr_warn("Failed to register probe event(%d)\n", ret);
  439. goto end;
  440. }
  441. dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
  442. end:
  443. mutex_unlock(&event_mutex);
  444. return ret;
  445. }
  446. /*
  447. * Argument syntax:
  448. * - Add uprobe: p|r[:[GRP/][EVENT]] PATH:OFFSET[%return][(REF)] [FETCHARGS]
  449. */
  450. static int __trace_uprobe_create(int argc, const char **argv)
  451. {
  452. struct trace_uprobe *tu;
  453. const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
  454. char *arg, *filename, *rctr, *rctr_end, *tmp;
  455. char buf[MAX_EVENT_NAME_LEN];
  456. char gbuf[MAX_EVENT_NAME_LEN];
  457. enum probe_print_type ptype;
  458. struct path path;
  459. unsigned long offset, ref_ctr_offset;
  460. bool is_return = false;
  461. int i, ret;
  462. ref_ctr_offset = 0;
  463. switch (argv[0][0]) {
  464. case 'r':
  465. is_return = true;
  466. break;
  467. case 'p':
  468. break;
  469. default:
  470. return -ECANCELED;
  471. }
  472. if (argc < 2)
  473. return -ECANCELED;
  474. if (argv[0][1] == ':')
  475. event = &argv[0][2];
  476. if (!strchr(argv[1], '/'))
  477. return -ECANCELED;
  478. filename = kstrdup(argv[1], GFP_KERNEL);
  479. if (!filename)
  480. return -ENOMEM;
  481. /* Find the last occurrence, in case the path contains ':' too. */
  482. arg = strrchr(filename, ':');
  483. if (!arg || !isdigit(arg[1])) {
  484. kfree(filename);
  485. return -ECANCELED;
  486. }
  487. trace_probe_log_init("trace_uprobe", argc, argv);
  488. trace_probe_log_set_index(1); /* filename is the 2nd argument */
  489. *arg++ = '\0';
  490. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  491. if (ret) {
  492. trace_probe_log_err(0, FILE_NOT_FOUND);
  493. kfree(filename);
  494. trace_probe_log_clear();
  495. return ret;
  496. }
  497. if (!d_is_reg(path.dentry)) {
  498. trace_probe_log_err(0, NO_REGULAR_FILE);
  499. ret = -EINVAL;
  500. goto fail_address_parse;
  501. }
  502. /* Parse reference counter offset if specified. */
  503. rctr = strchr(arg, '(');
  504. if (rctr) {
  505. rctr_end = strchr(rctr, ')');
  506. if (!rctr_end) {
  507. ret = -EINVAL;
  508. rctr_end = rctr + strlen(rctr);
  509. trace_probe_log_err(rctr_end - filename,
  510. REFCNT_OPEN_BRACE);
  511. goto fail_address_parse;
  512. } else if (rctr_end[1] != '\0') {
  513. ret = -EINVAL;
  514. trace_probe_log_err(rctr_end + 1 - filename,
  515. BAD_REFCNT_SUFFIX);
  516. goto fail_address_parse;
  517. }
  518. *rctr++ = '\0';
  519. *rctr_end = '\0';
  520. ret = kstrtoul(rctr, 0, &ref_ctr_offset);
  521. if (ret) {
  522. trace_probe_log_err(rctr - filename, BAD_REFCNT);
  523. goto fail_address_parse;
  524. }
  525. }
  526. /* Check if there is %return suffix */
  527. tmp = strchr(arg, '%');
  528. if (tmp) {
  529. if (!strcmp(tmp, "%return")) {
  530. *tmp = '\0';
  531. is_return = true;
  532. } else {
  533. trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX);
  534. ret = -EINVAL;
  535. goto fail_address_parse;
  536. }
  537. }
  538. /* Parse uprobe offset. */
  539. ret = kstrtoul(arg, 0, &offset);
  540. if (ret) {
  541. trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
  542. goto fail_address_parse;
  543. }
  544. /* setup a probe */
  545. trace_probe_log_set_index(0);
  546. if (event) {
  547. ret = traceprobe_parse_event_name(&event, &group, gbuf,
  548. event - argv[0]);
  549. if (ret)
  550. goto fail_address_parse;
  551. }
  552. if (!event) {
  553. char *tail;
  554. char *ptr;
  555. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  556. if (!tail) {
  557. ret = -ENOMEM;
  558. goto fail_address_parse;
  559. }
  560. ptr = strpbrk(tail, ".-_");
  561. if (ptr)
  562. *ptr = '\0';
  563. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  564. event = buf;
  565. kfree(tail);
  566. }
  567. argc -= 2;
  568. argv += 2;
  569. tu = alloc_trace_uprobe(group, event, argc, is_return);
  570. if (IS_ERR(tu)) {
  571. ret = PTR_ERR(tu);
  572. /* This must return -ENOMEM otherwise there is a bug */
  573. WARN_ON_ONCE(ret != -ENOMEM);
  574. goto fail_address_parse;
  575. }
  576. tu->offset = offset;
  577. tu->ref_ctr_offset = ref_ctr_offset;
  578. tu->path = path;
  579. tu->filename = filename;
  580. /* parse arguments */
  581. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  582. trace_probe_log_set_index(i + 2);
  583. ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i],
  584. is_return ? TPARG_FL_RETURN : 0);
  585. if (ret)
  586. goto error;
  587. }
  588. ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
  589. ret = traceprobe_set_print_fmt(&tu->tp, ptype);
  590. if (ret < 0)
  591. goto error;
  592. ret = register_trace_uprobe(tu);
  593. if (!ret)
  594. goto out;
  595. error:
  596. free_trace_uprobe(tu);
  597. out:
  598. trace_probe_log_clear();
  599. return ret;
  600. fail_address_parse:
  601. trace_probe_log_clear();
  602. path_put(&path);
  603. kfree(filename);
  604. return ret;
  605. }
  606. int trace_uprobe_create(const char *raw_command)
  607. {
  608. return trace_probe_create(raw_command, __trace_uprobe_create);
  609. }
  610. static int create_or_delete_trace_uprobe(const char *raw_command)
  611. {
  612. int ret;
  613. if (raw_command[0] == '-')
  614. return dyn_event_release(raw_command, &trace_uprobe_ops);
  615. ret = trace_uprobe_create(raw_command);
  616. return ret == -ECANCELED ? -EINVAL : ret;
  617. }
  618. static int trace_uprobe_release(struct dyn_event *ev)
  619. {
  620. struct trace_uprobe *tu = to_trace_uprobe(ev);
  621. return unregister_trace_uprobe(tu);
  622. }
  623. /* Probes listing interfaces */
  624. static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
  625. {
  626. struct trace_uprobe *tu = to_trace_uprobe(ev);
  627. char c = is_ret_probe(tu) ? 'r' : 'p';
  628. int i;
  629. seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp),
  630. trace_probe_name(&tu->tp), tu->filename,
  631. (int)(sizeof(void *) * 2), tu->offset);
  632. if (tu->ref_ctr_offset)
  633. seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
  634. for (i = 0; i < tu->tp.nr_args; i++)
  635. seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
  636. seq_putc(m, '\n');
  637. return 0;
  638. }
  639. static int probes_seq_show(struct seq_file *m, void *v)
  640. {
  641. struct dyn_event *ev = v;
  642. if (!is_trace_uprobe(ev))
  643. return 0;
  644. return trace_uprobe_show(m, ev);
  645. }
  646. static const struct seq_operations probes_seq_op = {
  647. .start = dyn_event_seq_start,
  648. .next = dyn_event_seq_next,
  649. .stop = dyn_event_seq_stop,
  650. .show = probes_seq_show
  651. };
  652. static int probes_open(struct inode *inode, struct file *file)
  653. {
  654. int ret;
  655. ret = security_locked_down(LOCKDOWN_TRACEFS);
  656. if (ret)
  657. return ret;
  658. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  659. ret = dyn_events_release_all(&trace_uprobe_ops);
  660. if (ret)
  661. return ret;
  662. }
  663. return seq_open(file, &probes_seq_op);
  664. }
  665. static ssize_t probes_write(struct file *file, const char __user *buffer,
  666. size_t count, loff_t *ppos)
  667. {
  668. return trace_parse_run_command(file, buffer, count, ppos,
  669. create_or_delete_trace_uprobe);
  670. }
  671. static const struct file_operations uprobe_events_ops = {
  672. .owner = THIS_MODULE,
  673. .open = probes_open,
  674. .read = seq_read,
  675. .llseek = seq_lseek,
  676. .release = seq_release,
  677. .write = probes_write,
  678. };
  679. /* Probes profiling interfaces */
  680. static int probes_profile_seq_show(struct seq_file *m, void *v)
  681. {
  682. struct dyn_event *ev = v;
  683. struct trace_uprobe *tu;
  684. if (!is_trace_uprobe(ev))
  685. return 0;
  686. tu = to_trace_uprobe(ev);
  687. seq_printf(m, " %s %-44s %15lu\n", tu->filename,
  688. trace_probe_name(&tu->tp), tu->nhit);
  689. return 0;
  690. }
  691. static const struct seq_operations profile_seq_op = {
  692. .start = dyn_event_seq_start,
  693. .next = dyn_event_seq_next,
  694. .stop = dyn_event_seq_stop,
  695. .show = probes_profile_seq_show
  696. };
  697. static int profile_open(struct inode *inode, struct file *file)
  698. {
  699. int ret;
  700. ret = security_locked_down(LOCKDOWN_TRACEFS);
  701. if (ret)
  702. return ret;
  703. return seq_open(file, &profile_seq_op);
  704. }
  705. static const struct file_operations uprobe_profile_ops = {
  706. .owner = THIS_MODULE,
  707. .open = profile_open,
  708. .read = seq_read,
  709. .llseek = seq_lseek,
  710. .release = seq_release,
  711. };
  712. struct uprobe_cpu_buffer {
  713. struct mutex mutex;
  714. void *buf;
  715. };
  716. static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
  717. static int uprobe_buffer_refcnt;
  718. static int uprobe_buffer_init(void)
  719. {
  720. int cpu, err_cpu;
  721. uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
  722. if (uprobe_cpu_buffer == NULL)
  723. return -ENOMEM;
  724. for_each_possible_cpu(cpu) {
  725. struct page *p = alloc_pages_node(cpu_to_node(cpu),
  726. GFP_KERNEL, 0);
  727. if (p == NULL) {
  728. err_cpu = cpu;
  729. goto err;
  730. }
  731. per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
  732. mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
  733. }
  734. return 0;
  735. err:
  736. for_each_possible_cpu(cpu) {
  737. if (cpu == err_cpu)
  738. break;
  739. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
  740. }
  741. free_percpu(uprobe_cpu_buffer);
  742. return -ENOMEM;
  743. }
  744. static int uprobe_buffer_enable(void)
  745. {
  746. int ret = 0;
  747. BUG_ON(!mutex_is_locked(&event_mutex));
  748. if (uprobe_buffer_refcnt++ == 0) {
  749. ret = uprobe_buffer_init();
  750. if (ret < 0)
  751. uprobe_buffer_refcnt--;
  752. }
  753. return ret;
  754. }
  755. static void uprobe_buffer_disable(void)
  756. {
  757. int cpu;
  758. BUG_ON(!mutex_is_locked(&event_mutex));
  759. if (--uprobe_buffer_refcnt == 0) {
  760. for_each_possible_cpu(cpu)
  761. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
  762. cpu)->buf);
  763. free_percpu(uprobe_cpu_buffer);
  764. uprobe_cpu_buffer = NULL;
  765. }
  766. }
  767. static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
  768. {
  769. struct uprobe_cpu_buffer *ucb;
  770. int cpu;
  771. cpu = raw_smp_processor_id();
  772. ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
  773. /*
  774. * Use per-cpu buffers for fastest access, but we might migrate
  775. * so the mutex makes sure we have sole access to it.
  776. */
  777. mutex_lock(&ucb->mutex);
  778. return ucb;
  779. }
  780. static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
  781. {
  782. mutex_unlock(&ucb->mutex);
  783. }
  784. static void __uprobe_trace_func(struct trace_uprobe *tu,
  785. unsigned long func, struct pt_regs *regs,
  786. struct uprobe_cpu_buffer *ucb, int dsize,
  787. struct trace_event_file *trace_file)
  788. {
  789. struct uprobe_trace_entry_head *entry;
  790. struct trace_event_buffer fbuffer;
  791. void *data;
  792. int size, esize;
  793. struct trace_event_call *call = trace_probe_event_call(&tu->tp);
  794. WARN_ON(call != trace_file->event_call);
  795. if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
  796. return;
  797. if (trace_trigger_soft_disabled(trace_file))
  798. return;
  799. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  800. size = esize + tu->tp.size + dsize;
  801. entry = trace_event_buffer_reserve(&fbuffer, trace_file, size);
  802. if (!entry)
  803. return;
  804. if (is_ret_probe(tu)) {
  805. entry->vaddr[0] = func;
  806. entry->vaddr[1] = instruction_pointer(regs);
  807. data = DATAOF_TRACE_ENTRY(entry, true);
  808. } else {
  809. entry->vaddr[0] = instruction_pointer(regs);
  810. data = DATAOF_TRACE_ENTRY(entry, false);
  811. }
  812. memcpy(data, ucb->buf, tu->tp.size + dsize);
  813. trace_event_buffer_commit(&fbuffer);
  814. }
  815. /* uprobe handler */
  816. static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
  817. struct uprobe_cpu_buffer *ucb, int dsize)
  818. {
  819. struct event_file_link *link;
  820. if (is_ret_probe(tu))
  821. return 0;
  822. rcu_read_lock();
  823. trace_probe_for_each_link_rcu(link, &tu->tp)
  824. __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
  825. rcu_read_unlock();
  826. return 0;
  827. }
  828. static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  829. struct pt_regs *regs,
  830. struct uprobe_cpu_buffer *ucb, int dsize)
  831. {
  832. struct event_file_link *link;
  833. rcu_read_lock();
  834. trace_probe_for_each_link_rcu(link, &tu->tp)
  835. __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
  836. rcu_read_unlock();
  837. }
  838. /* Event entry printers */
  839. static enum print_line_t
  840. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  841. {
  842. struct uprobe_trace_entry_head *entry;
  843. struct trace_seq *s = &iter->seq;
  844. struct trace_uprobe *tu;
  845. u8 *data;
  846. entry = (struct uprobe_trace_entry_head *)iter->ent;
  847. tu = trace_uprobe_primary_from_call(
  848. container_of(event, struct trace_event_call, event));
  849. if (unlikely(!tu))
  850. goto out;
  851. if (is_ret_probe(tu)) {
  852. trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
  853. trace_probe_name(&tu->tp),
  854. entry->vaddr[1], entry->vaddr[0]);
  855. data = DATAOF_TRACE_ENTRY(entry, true);
  856. } else {
  857. trace_seq_printf(s, "%s: (0x%lx)",
  858. trace_probe_name(&tu->tp),
  859. entry->vaddr[0]);
  860. data = DATAOF_TRACE_ENTRY(entry, false);
  861. }
  862. if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
  863. goto out;
  864. trace_seq_putc(s, '\n');
  865. out:
  866. return trace_handle_return(s);
  867. }
  868. typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  869. enum uprobe_filter_ctx ctx,
  870. struct mm_struct *mm);
  871. static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter)
  872. {
  873. int ret;
  874. tu->consumer.filter = filter;
  875. tu->inode = d_real_inode(tu->path.dentry);
  876. if (tu->ref_ctr_offset)
  877. ret = uprobe_register_refctr(tu->inode, tu->offset,
  878. tu->ref_ctr_offset, &tu->consumer);
  879. else
  880. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  881. if (ret)
  882. tu->inode = NULL;
  883. return ret;
  884. }
  885. static void __probe_event_disable(struct trace_probe *tp)
  886. {
  887. struct trace_uprobe *tu;
  888. tu = container_of(tp, struct trace_uprobe, tp);
  889. WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
  890. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  891. if (!tu->inode)
  892. continue;
  893. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  894. tu->inode = NULL;
  895. }
  896. }
  897. static int probe_event_enable(struct trace_event_call *call,
  898. struct trace_event_file *file, filter_func_t filter)
  899. {
  900. struct trace_probe *tp;
  901. struct trace_uprobe *tu;
  902. bool enabled;
  903. int ret;
  904. tp = trace_probe_primary_from_call(call);
  905. if (WARN_ON_ONCE(!tp))
  906. return -ENODEV;
  907. enabled = trace_probe_is_enabled(tp);
  908. /* This may also change "enabled" state */
  909. if (file) {
  910. if (trace_probe_test_flag(tp, TP_FLAG_PROFILE))
  911. return -EINTR;
  912. ret = trace_probe_add_file(tp, file);
  913. if (ret < 0)
  914. return ret;
  915. } else {
  916. if (trace_probe_test_flag(tp, TP_FLAG_TRACE))
  917. return -EINTR;
  918. trace_probe_set_flag(tp, TP_FLAG_PROFILE);
  919. }
  920. tu = container_of(tp, struct trace_uprobe, tp);
  921. WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
  922. if (enabled)
  923. return 0;
  924. ret = uprobe_buffer_enable();
  925. if (ret)
  926. goto err_flags;
  927. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  928. ret = trace_uprobe_enable(tu, filter);
  929. if (ret) {
  930. __probe_event_disable(tp);
  931. goto err_buffer;
  932. }
  933. }
  934. return 0;
  935. err_buffer:
  936. uprobe_buffer_disable();
  937. err_flags:
  938. if (file)
  939. trace_probe_remove_file(tp, file);
  940. else
  941. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  942. return ret;
  943. }
  944. static void probe_event_disable(struct trace_event_call *call,
  945. struct trace_event_file *file)
  946. {
  947. struct trace_probe *tp;
  948. tp = trace_probe_primary_from_call(call);
  949. if (WARN_ON_ONCE(!tp))
  950. return;
  951. if (!trace_probe_is_enabled(tp))
  952. return;
  953. if (file) {
  954. if (trace_probe_remove_file(tp, file) < 0)
  955. return;
  956. if (trace_probe_is_enabled(tp))
  957. return;
  958. } else
  959. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  960. __probe_event_disable(tp);
  961. uprobe_buffer_disable();
  962. }
  963. static int uprobe_event_define_fields(struct trace_event_call *event_call)
  964. {
  965. int ret, size;
  966. struct uprobe_trace_entry_head field;
  967. struct trace_uprobe *tu;
  968. tu = trace_uprobe_primary_from_call(event_call);
  969. if (unlikely(!tu))
  970. return -ENODEV;
  971. if (is_ret_probe(tu)) {
  972. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  973. DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  974. size = SIZEOF_TRACE_ENTRY(true);
  975. } else {
  976. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  977. size = SIZEOF_TRACE_ENTRY(false);
  978. }
  979. return traceprobe_define_arg_fields(event_call, size, &tu->tp);
  980. }
  981. #ifdef CONFIG_PERF_EVENTS
  982. static bool
  983. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  984. {
  985. struct perf_event *event;
  986. if (filter->nr_systemwide)
  987. return true;
  988. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  989. if (event->hw.target->mm == mm)
  990. return true;
  991. }
  992. return false;
  993. }
  994. static inline bool
  995. trace_uprobe_filter_event(struct trace_uprobe_filter *filter,
  996. struct perf_event *event)
  997. {
  998. return __uprobe_perf_filter(filter, event->hw.target->mm);
  999. }
  1000. static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter,
  1001. struct perf_event *event)
  1002. {
  1003. bool done;
  1004. write_lock(&filter->rwlock);
  1005. if (event->hw.target) {
  1006. list_del(&event->hw.tp_list);
  1007. done = filter->nr_systemwide ||
  1008. (event->hw.target->flags & PF_EXITING) ||
  1009. trace_uprobe_filter_event(filter, event);
  1010. } else {
  1011. filter->nr_systemwide--;
  1012. done = filter->nr_systemwide;
  1013. }
  1014. write_unlock(&filter->rwlock);
  1015. return done;
  1016. }
  1017. /* This returns true if the filter always covers target mm */
  1018. static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter,
  1019. struct perf_event *event)
  1020. {
  1021. bool done;
  1022. write_lock(&filter->rwlock);
  1023. if (event->hw.target) {
  1024. /*
  1025. * event->parent != NULL means copy_process(), we can avoid
  1026. * uprobe_apply(). current->mm must be probed and we can rely
  1027. * on dup_mmap() which preserves the already installed bp's.
  1028. *
  1029. * attr.enable_on_exec means that exec/mmap will install the
  1030. * breakpoints we need.
  1031. */
  1032. done = filter->nr_systemwide ||
  1033. event->parent || event->attr.enable_on_exec ||
  1034. trace_uprobe_filter_event(filter, event);
  1035. list_add(&event->hw.tp_list, &filter->perf_events);
  1036. } else {
  1037. done = filter->nr_systemwide;
  1038. filter->nr_systemwide++;
  1039. }
  1040. write_unlock(&filter->rwlock);
  1041. return done;
  1042. }
  1043. static int uprobe_perf_close(struct trace_event_call *call,
  1044. struct perf_event *event)
  1045. {
  1046. struct trace_probe *tp;
  1047. struct trace_uprobe *tu;
  1048. int ret = 0;
  1049. tp = trace_probe_primary_from_call(call);
  1050. if (WARN_ON_ONCE(!tp))
  1051. return -ENODEV;
  1052. tu = container_of(tp, struct trace_uprobe, tp);
  1053. if (trace_uprobe_filter_remove(tu->tp.event->filter, event))
  1054. return 0;
  1055. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  1056. ret = uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  1057. if (ret)
  1058. break;
  1059. }
  1060. return ret;
  1061. }
  1062. static int uprobe_perf_open(struct trace_event_call *call,
  1063. struct perf_event *event)
  1064. {
  1065. struct trace_probe *tp;
  1066. struct trace_uprobe *tu;
  1067. int err = 0;
  1068. tp = trace_probe_primary_from_call(call);
  1069. if (WARN_ON_ONCE(!tp))
  1070. return -ENODEV;
  1071. tu = container_of(tp, struct trace_uprobe, tp);
  1072. if (trace_uprobe_filter_add(tu->tp.event->filter, event))
  1073. return 0;
  1074. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  1075. err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  1076. if (err) {
  1077. uprobe_perf_close(call, event);
  1078. break;
  1079. }
  1080. }
  1081. return err;
  1082. }
  1083. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  1084. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  1085. {
  1086. struct trace_uprobe_filter *filter;
  1087. struct trace_uprobe *tu;
  1088. int ret;
  1089. tu = container_of(uc, struct trace_uprobe, consumer);
  1090. filter = tu->tp.event->filter;
  1091. read_lock(&filter->rwlock);
  1092. ret = __uprobe_perf_filter(filter, mm);
  1093. read_unlock(&filter->rwlock);
  1094. return ret;
  1095. }
  1096. static void __uprobe_perf_func(struct trace_uprobe *tu,
  1097. unsigned long func, struct pt_regs *regs,
  1098. struct uprobe_cpu_buffer *ucb, int dsize)
  1099. {
  1100. struct trace_event_call *call = trace_probe_event_call(&tu->tp);
  1101. struct uprobe_trace_entry_head *entry;
  1102. struct hlist_head *head;
  1103. void *data;
  1104. int size, esize;
  1105. int rctx;
  1106. #ifdef CONFIG_BPF_EVENTS
  1107. if (bpf_prog_array_valid(call)) {
  1108. u32 ret;
  1109. ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run);
  1110. if (!ret)
  1111. return;
  1112. }
  1113. #endif /* CONFIG_BPF_EVENTS */
  1114. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1115. size = esize + tu->tp.size + dsize;
  1116. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  1117. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  1118. return;
  1119. preempt_disable();
  1120. head = this_cpu_ptr(call->perf_events);
  1121. if (hlist_empty(head))
  1122. goto out;
  1123. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1124. if (!entry)
  1125. goto out;
  1126. if (is_ret_probe(tu)) {
  1127. entry->vaddr[0] = func;
  1128. entry->vaddr[1] = instruction_pointer(regs);
  1129. data = DATAOF_TRACE_ENTRY(entry, true);
  1130. } else {
  1131. entry->vaddr[0] = instruction_pointer(regs);
  1132. data = DATAOF_TRACE_ENTRY(entry, false);
  1133. }
  1134. memcpy(data, ucb->buf, tu->tp.size + dsize);
  1135. if (size - esize > tu->tp.size + dsize) {
  1136. int len = tu->tp.size + dsize;
  1137. memset(data + len, 0, size - esize - len);
  1138. }
  1139. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1140. head, NULL);
  1141. out:
  1142. preempt_enable();
  1143. }
  1144. /* uprobe profile handler */
  1145. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
  1146. struct uprobe_cpu_buffer *ucb, int dsize)
  1147. {
  1148. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  1149. return UPROBE_HANDLER_REMOVE;
  1150. if (!is_ret_probe(tu))
  1151. __uprobe_perf_func(tu, 0, regs, ucb, dsize);
  1152. return 0;
  1153. }
  1154. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  1155. struct pt_regs *regs,
  1156. struct uprobe_cpu_buffer *ucb, int dsize)
  1157. {
  1158. __uprobe_perf_func(tu, func, regs, ucb, dsize);
  1159. }
  1160. int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
  1161. const char **filename, u64 *probe_offset,
  1162. u64 *probe_addr, bool perf_type_tracepoint)
  1163. {
  1164. const char *pevent = trace_event_name(event->tp_event);
  1165. const char *group = event->tp_event->class->system;
  1166. struct trace_uprobe *tu;
  1167. if (perf_type_tracepoint)
  1168. tu = find_probe_event(pevent, group);
  1169. else
  1170. tu = trace_uprobe_primary_from_call(event->tp_event);
  1171. if (!tu)
  1172. return -EINVAL;
  1173. *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
  1174. : BPF_FD_TYPE_UPROBE;
  1175. *filename = tu->filename;
  1176. *probe_offset = tu->offset;
  1177. *probe_addr = 0;
  1178. return 0;
  1179. }
  1180. #endif /* CONFIG_PERF_EVENTS */
  1181. static int
  1182. trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
  1183. void *data)
  1184. {
  1185. struct trace_event_file *file = data;
  1186. switch (type) {
  1187. case TRACE_REG_REGISTER:
  1188. return probe_event_enable(event, file, NULL);
  1189. case TRACE_REG_UNREGISTER:
  1190. probe_event_disable(event, file);
  1191. return 0;
  1192. #ifdef CONFIG_PERF_EVENTS
  1193. case TRACE_REG_PERF_REGISTER:
  1194. return probe_event_enable(event, NULL, uprobe_perf_filter);
  1195. case TRACE_REG_PERF_UNREGISTER:
  1196. probe_event_disable(event, NULL);
  1197. return 0;
  1198. case TRACE_REG_PERF_OPEN:
  1199. return uprobe_perf_open(event, data);
  1200. case TRACE_REG_PERF_CLOSE:
  1201. return uprobe_perf_close(event, data);
  1202. #endif
  1203. default:
  1204. return 0;
  1205. }
  1206. }
  1207. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  1208. {
  1209. struct trace_uprobe *tu;
  1210. struct uprobe_dispatch_data udd;
  1211. struct uprobe_cpu_buffer *ucb;
  1212. int dsize, esize;
  1213. int ret = 0;
  1214. tu = container_of(con, struct trace_uprobe, consumer);
  1215. tu->nhit++;
  1216. udd.tu = tu;
  1217. udd.bp_addr = instruction_pointer(regs);
  1218. current->utask->vaddr = (unsigned long) &udd;
  1219. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1220. return 0;
  1221. dsize = __get_data_size(&tu->tp, regs);
  1222. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1223. ucb = uprobe_buffer_get();
  1224. store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
  1225. if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
  1226. ret |= uprobe_trace_func(tu, regs, ucb, dsize);
  1227. #ifdef CONFIG_PERF_EVENTS
  1228. if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
  1229. ret |= uprobe_perf_func(tu, regs, ucb, dsize);
  1230. #endif
  1231. uprobe_buffer_put(ucb);
  1232. return ret;
  1233. }
  1234. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  1235. unsigned long func, struct pt_regs *regs)
  1236. {
  1237. struct trace_uprobe *tu;
  1238. struct uprobe_dispatch_data udd;
  1239. struct uprobe_cpu_buffer *ucb;
  1240. int dsize, esize;
  1241. tu = container_of(con, struct trace_uprobe, consumer);
  1242. udd.tu = tu;
  1243. udd.bp_addr = func;
  1244. current->utask->vaddr = (unsigned long) &udd;
  1245. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1246. return 0;
  1247. dsize = __get_data_size(&tu->tp, regs);
  1248. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1249. ucb = uprobe_buffer_get();
  1250. store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
  1251. if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
  1252. uretprobe_trace_func(tu, func, regs, ucb, dsize);
  1253. #ifdef CONFIG_PERF_EVENTS
  1254. if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
  1255. uretprobe_perf_func(tu, func, regs, ucb, dsize);
  1256. #endif
  1257. uprobe_buffer_put(ucb);
  1258. return 0;
  1259. }
  1260. static struct trace_event_functions uprobe_funcs = {
  1261. .trace = print_uprobe_event
  1262. };
  1263. static struct trace_event_fields uprobe_fields_array[] = {
  1264. { .type = TRACE_FUNCTION_TYPE,
  1265. .define_fields = uprobe_event_define_fields },
  1266. {}
  1267. };
  1268. static inline void init_trace_event_call(struct trace_uprobe *tu)
  1269. {
  1270. struct trace_event_call *call = trace_probe_event_call(&tu->tp);
  1271. call->event.funcs = &uprobe_funcs;
  1272. call->class->fields_array = uprobe_fields_array;
  1273. call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY;
  1274. call->class->reg = trace_uprobe_register;
  1275. }
  1276. static int register_uprobe_event(struct trace_uprobe *tu)
  1277. {
  1278. init_trace_event_call(tu);
  1279. return trace_probe_register_event_call(&tu->tp);
  1280. }
  1281. static int unregister_uprobe_event(struct trace_uprobe *tu)
  1282. {
  1283. return trace_probe_unregister_event_call(&tu->tp);
  1284. }
  1285. #ifdef CONFIG_PERF_EVENTS
  1286. struct trace_event_call *
  1287. create_local_trace_uprobe(char *name, unsigned long offs,
  1288. unsigned long ref_ctr_offset, bool is_return)
  1289. {
  1290. enum probe_print_type ptype;
  1291. struct trace_uprobe *tu;
  1292. struct path path;
  1293. int ret;
  1294. ret = kern_path(name, LOOKUP_FOLLOW, &path);
  1295. if (ret)
  1296. return ERR_PTR(ret);
  1297. if (!d_is_reg(path.dentry)) {
  1298. path_put(&path);
  1299. return ERR_PTR(-EINVAL);
  1300. }
  1301. /*
  1302. * local trace_kprobes are not added to dyn_event, so they are never
  1303. * searched in find_trace_kprobe(). Therefore, there is no concern of
  1304. * duplicated name "DUMMY_EVENT" here.
  1305. */
  1306. tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
  1307. is_return);
  1308. if (IS_ERR(tu)) {
  1309. pr_info("Failed to allocate trace_uprobe.(%d)\n",
  1310. (int)PTR_ERR(tu));
  1311. path_put(&path);
  1312. return ERR_CAST(tu);
  1313. }
  1314. tu->offset = offs;
  1315. tu->path = path;
  1316. tu->ref_ctr_offset = ref_ctr_offset;
  1317. tu->filename = kstrdup(name, GFP_KERNEL);
  1318. if (!tu->filename) {
  1319. ret = -ENOMEM;
  1320. goto error;
  1321. }
  1322. init_trace_event_call(tu);
  1323. ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
  1324. if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) {
  1325. ret = -ENOMEM;
  1326. goto error;
  1327. }
  1328. return trace_probe_event_call(&tu->tp);
  1329. error:
  1330. free_trace_uprobe(tu);
  1331. return ERR_PTR(ret);
  1332. }
  1333. void destroy_local_trace_uprobe(struct trace_event_call *event_call)
  1334. {
  1335. struct trace_uprobe *tu;
  1336. tu = trace_uprobe_primary_from_call(event_call);
  1337. free_trace_uprobe(tu);
  1338. }
  1339. #endif /* CONFIG_PERF_EVENTS */
  1340. /* Make a trace interface for controlling probe points */
  1341. static __init int init_uprobe_trace(void)
  1342. {
  1343. int ret;
  1344. ret = dyn_event_register(&trace_uprobe_ops);
  1345. if (ret)
  1346. return ret;
  1347. ret = tracing_init_dentry();
  1348. if (ret)
  1349. return 0;
  1350. trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL,
  1351. NULL, &uprobe_events_ops);
  1352. /* Profile interface */
  1353. trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL,
  1354. NULL, &uprobe_profile_ops);
  1355. return 0;
  1356. }
  1357. fs_initcall(init_uprobe_trace);