trace_uprobe.c 30 KB

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