trace_uprobe.c 32 KB

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