trace_uprobe.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. return tu;
  227. error:
  228. kfree(tu->tp.call.name);
  229. kfree(tu);
  230. return ERR_PTR(-ENOMEM);
  231. }
  232. static void free_trace_uprobe(struct trace_uprobe *tu)
  233. {
  234. int i;
  235. for (i = 0; i < tu->tp.nr_args; i++)
  236. traceprobe_free_probe_arg(&tu->tp.args[i]);
  237. iput(tu->inode);
  238. kfree(tu->tp.call.class->system);
  239. kfree(tu->tp.call.name);
  240. kfree(tu->filename);
  241. kfree(tu);
  242. }
  243. static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  244. {
  245. struct trace_uprobe *tu;
  246. list_for_each_entry(tu, &uprobe_list, list)
  247. if (strcmp(ftrace_event_name(&tu->tp.call), event) == 0 &&
  248. strcmp(tu->tp.call.class->system, group) == 0)
  249. return tu;
  250. return NULL;
  251. }
  252. /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
  253. static int unregister_trace_uprobe(struct trace_uprobe *tu)
  254. {
  255. int ret;
  256. ret = unregister_uprobe_event(tu);
  257. if (ret)
  258. return ret;
  259. list_del(&tu->list);
  260. free_trace_uprobe(tu);
  261. return 0;
  262. }
  263. /* Register a trace_uprobe and probe_event */
  264. static int register_trace_uprobe(struct trace_uprobe *tu)
  265. {
  266. struct trace_uprobe *old_tu;
  267. int ret;
  268. mutex_lock(&uprobe_lock);
  269. /* register as an event */
  270. old_tu = find_probe_event(ftrace_event_name(&tu->tp.call),
  271. tu->tp.call.class->system);
  272. if (old_tu) {
  273. /* delete old event */
  274. ret = unregister_trace_uprobe(old_tu);
  275. if (ret)
  276. goto end;
  277. }
  278. ret = register_uprobe_event(tu);
  279. if (ret) {
  280. pr_warning("Failed to register probe event(%d)\n", ret);
  281. goto end;
  282. }
  283. list_add_tail(&tu->list, &uprobe_list);
  284. end:
  285. mutex_unlock(&uprobe_lock);
  286. return ret;
  287. }
  288. /*
  289. * Argument syntax:
  290. * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
  291. *
  292. * - Remove uprobe: -:[GRP/]EVENT
  293. */
  294. static int create_trace_uprobe(int argc, char **argv)
  295. {
  296. struct trace_uprobe *tu;
  297. struct inode *inode;
  298. char *arg, *event, *group, *filename;
  299. char buf[MAX_EVENT_NAME_LEN];
  300. struct path path;
  301. unsigned long offset;
  302. bool is_delete, is_return;
  303. int i, ret;
  304. inode = NULL;
  305. ret = 0;
  306. is_delete = false;
  307. is_return = false;
  308. event = NULL;
  309. group = NULL;
  310. /* argc must be >= 1 */
  311. if (argv[0][0] == '-')
  312. is_delete = true;
  313. else if (argv[0][0] == 'r')
  314. is_return = true;
  315. else if (argv[0][0] != 'p') {
  316. pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
  317. return -EINVAL;
  318. }
  319. if (argv[0][1] == ':') {
  320. event = &argv[0][2];
  321. arg = strchr(event, '/');
  322. if (arg) {
  323. group = event;
  324. event = arg + 1;
  325. event[-1] = '\0';
  326. if (strlen(group) == 0) {
  327. pr_info("Group name is not specified\n");
  328. return -EINVAL;
  329. }
  330. }
  331. if (strlen(event) == 0) {
  332. pr_info("Event name is not specified\n");
  333. return -EINVAL;
  334. }
  335. }
  336. if (!group)
  337. group = UPROBE_EVENT_SYSTEM;
  338. if (is_delete) {
  339. int ret;
  340. if (!event) {
  341. pr_info("Delete command needs an event name.\n");
  342. return -EINVAL;
  343. }
  344. mutex_lock(&uprobe_lock);
  345. tu = find_probe_event(event, group);
  346. if (!tu) {
  347. mutex_unlock(&uprobe_lock);
  348. pr_info("Event %s/%s doesn't exist.\n", group, event);
  349. return -ENOENT;
  350. }
  351. /* delete an event */
  352. ret = unregister_trace_uprobe(tu);
  353. mutex_unlock(&uprobe_lock);
  354. return ret;
  355. }
  356. if (argc < 2) {
  357. pr_info("Probe point is not specified.\n");
  358. return -EINVAL;
  359. }
  360. if (isdigit(argv[1][0])) {
  361. pr_info("probe point must be have a filename.\n");
  362. return -EINVAL;
  363. }
  364. arg = strchr(argv[1], ':');
  365. if (!arg) {
  366. ret = -EINVAL;
  367. goto fail_address_parse;
  368. }
  369. *arg++ = '\0';
  370. filename = argv[1];
  371. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  372. if (ret)
  373. goto fail_address_parse;
  374. inode = igrab(path.dentry->d_inode);
  375. path_put(&path);
  376. if (!inode || !S_ISREG(inode->i_mode)) {
  377. ret = -EINVAL;
  378. goto fail_address_parse;
  379. }
  380. ret = kstrtoul(arg, 0, &offset);
  381. if (ret)
  382. goto fail_address_parse;
  383. argc -= 2;
  384. argv += 2;
  385. /* setup a probe */
  386. if (!event) {
  387. char *tail;
  388. char *ptr;
  389. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  390. if (!tail) {
  391. ret = -ENOMEM;
  392. goto fail_address_parse;
  393. }
  394. ptr = strpbrk(tail, ".-_");
  395. if (ptr)
  396. *ptr = '\0';
  397. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  398. event = buf;
  399. kfree(tail);
  400. }
  401. tu = alloc_trace_uprobe(group, event, argc, is_return);
  402. if (IS_ERR(tu)) {
  403. pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
  404. ret = PTR_ERR(tu);
  405. goto fail_address_parse;
  406. }
  407. tu->offset = offset;
  408. tu->inode = inode;
  409. tu->filename = kstrdup(filename, GFP_KERNEL);
  410. if (!tu->filename) {
  411. pr_info("Failed to allocate filename.\n");
  412. ret = -ENOMEM;
  413. goto error;
  414. }
  415. /* parse arguments */
  416. ret = 0;
  417. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  418. struct probe_arg *parg = &tu->tp.args[i];
  419. /* Increment count for freeing args in error case */
  420. tu->tp.nr_args++;
  421. /* Parse argument name */
  422. arg = strchr(argv[i], '=');
  423. if (arg) {
  424. *arg++ = '\0';
  425. parg->name = kstrdup(argv[i], GFP_KERNEL);
  426. } else {
  427. arg = argv[i];
  428. /* If argument name is omitted, set "argN" */
  429. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  430. parg->name = kstrdup(buf, GFP_KERNEL);
  431. }
  432. if (!parg->name) {
  433. pr_info("Failed to allocate argument[%d] name.\n", i);
  434. ret = -ENOMEM;
  435. goto error;
  436. }
  437. if (!is_good_name(parg->name)) {
  438. pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
  439. ret = -EINVAL;
  440. goto error;
  441. }
  442. if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
  443. pr_info("Argument[%d] name '%s' conflicts with "
  444. "another field.\n", i, argv[i]);
  445. ret = -EINVAL;
  446. goto error;
  447. }
  448. /* Parse fetch argument */
  449. ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
  450. is_return, false);
  451. if (ret) {
  452. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  453. goto error;
  454. }
  455. }
  456. ret = register_trace_uprobe(tu);
  457. if (ret)
  458. goto error;
  459. return 0;
  460. error:
  461. free_trace_uprobe(tu);
  462. return ret;
  463. fail_address_parse:
  464. iput(inode);
  465. pr_info("Failed to parse address or file.\n");
  466. return ret;
  467. }
  468. static int cleanup_all_probes(void)
  469. {
  470. struct trace_uprobe *tu;
  471. int ret = 0;
  472. mutex_lock(&uprobe_lock);
  473. while (!list_empty(&uprobe_list)) {
  474. tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
  475. ret = unregister_trace_uprobe(tu);
  476. if (ret)
  477. break;
  478. }
  479. mutex_unlock(&uprobe_lock);
  480. return ret;
  481. }
  482. /* Probes listing interfaces */
  483. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  484. {
  485. mutex_lock(&uprobe_lock);
  486. return seq_list_start(&uprobe_list, *pos);
  487. }
  488. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  489. {
  490. return seq_list_next(v, &uprobe_list, pos);
  491. }
  492. static void probes_seq_stop(struct seq_file *m, void *v)
  493. {
  494. mutex_unlock(&uprobe_lock);
  495. }
  496. static int probes_seq_show(struct seq_file *m, void *v)
  497. {
  498. struct trace_uprobe *tu = v;
  499. char c = is_ret_probe(tu) ? 'r' : 'p';
  500. int i;
  501. seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system,
  502. ftrace_event_name(&tu->tp.call));
  503. seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
  504. for (i = 0; i < tu->tp.nr_args; i++)
  505. seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
  506. seq_putc(m, '\n');
  507. return 0;
  508. }
  509. static const struct seq_operations probes_seq_op = {
  510. .start = probes_seq_start,
  511. .next = probes_seq_next,
  512. .stop = probes_seq_stop,
  513. .show = probes_seq_show
  514. };
  515. static int probes_open(struct inode *inode, struct file *file)
  516. {
  517. int ret;
  518. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  519. ret = cleanup_all_probes();
  520. if (ret)
  521. return ret;
  522. }
  523. return seq_open(file, &probes_seq_op);
  524. }
  525. static ssize_t probes_write(struct file *file, const char __user *buffer,
  526. size_t count, loff_t *ppos)
  527. {
  528. return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
  529. }
  530. static const struct file_operations uprobe_events_ops = {
  531. .owner = THIS_MODULE,
  532. .open = probes_open,
  533. .read = seq_read,
  534. .llseek = seq_lseek,
  535. .release = seq_release,
  536. .write = probes_write,
  537. };
  538. /* Probes profiling interfaces */
  539. static int probes_profile_seq_show(struct seq_file *m, void *v)
  540. {
  541. struct trace_uprobe *tu = v;
  542. seq_printf(m, " %s %-44s %15lu\n", tu->filename,
  543. ftrace_event_name(&tu->tp.call), tu->nhit);
  544. return 0;
  545. }
  546. static const struct seq_operations profile_seq_op = {
  547. .start = probes_seq_start,
  548. .next = probes_seq_next,
  549. .stop = probes_seq_stop,
  550. .show = probes_profile_seq_show
  551. };
  552. static int profile_open(struct inode *inode, struct file *file)
  553. {
  554. return seq_open(file, &profile_seq_op);
  555. }
  556. static const struct file_operations uprobe_profile_ops = {
  557. .owner = THIS_MODULE,
  558. .open = profile_open,
  559. .read = seq_read,
  560. .llseek = seq_lseek,
  561. .release = seq_release,
  562. };
  563. struct uprobe_cpu_buffer {
  564. struct mutex mutex;
  565. void *buf;
  566. };
  567. static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
  568. static int uprobe_buffer_refcnt;
  569. static int uprobe_buffer_init(void)
  570. {
  571. int cpu, err_cpu;
  572. uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
  573. if (uprobe_cpu_buffer == NULL)
  574. return -ENOMEM;
  575. for_each_possible_cpu(cpu) {
  576. struct page *p = alloc_pages_node(cpu_to_node(cpu),
  577. GFP_KERNEL, 0);
  578. if (p == NULL) {
  579. err_cpu = cpu;
  580. goto err;
  581. }
  582. per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
  583. mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
  584. }
  585. return 0;
  586. err:
  587. for_each_possible_cpu(cpu) {
  588. if (cpu == err_cpu)
  589. break;
  590. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
  591. }
  592. free_percpu(uprobe_cpu_buffer);
  593. return -ENOMEM;
  594. }
  595. static int uprobe_buffer_enable(void)
  596. {
  597. int ret = 0;
  598. BUG_ON(!mutex_is_locked(&event_mutex));
  599. if (uprobe_buffer_refcnt++ == 0) {
  600. ret = uprobe_buffer_init();
  601. if (ret < 0)
  602. uprobe_buffer_refcnt--;
  603. }
  604. return ret;
  605. }
  606. static void uprobe_buffer_disable(void)
  607. {
  608. int cpu;
  609. BUG_ON(!mutex_is_locked(&event_mutex));
  610. if (--uprobe_buffer_refcnt == 0) {
  611. for_each_possible_cpu(cpu)
  612. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
  613. cpu)->buf);
  614. free_percpu(uprobe_cpu_buffer);
  615. uprobe_cpu_buffer = NULL;
  616. }
  617. }
  618. static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
  619. {
  620. struct uprobe_cpu_buffer *ucb;
  621. int cpu;
  622. cpu = raw_smp_processor_id();
  623. ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
  624. /*
  625. * Use per-cpu buffers for fastest access, but we might migrate
  626. * so the mutex makes sure we have sole access to it.
  627. */
  628. mutex_lock(&ucb->mutex);
  629. return ucb;
  630. }
  631. static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
  632. {
  633. mutex_unlock(&ucb->mutex);
  634. }
  635. static void __uprobe_trace_func(struct trace_uprobe *tu,
  636. unsigned long func, struct pt_regs *regs,
  637. struct uprobe_cpu_buffer *ucb, int dsize,
  638. struct ftrace_event_file *ftrace_file)
  639. {
  640. struct uprobe_trace_entry_head *entry;
  641. struct ring_buffer_event *event;
  642. struct ring_buffer *buffer;
  643. void *data;
  644. int size, esize;
  645. struct ftrace_event_call *call = &tu->tp.call;
  646. WARN_ON(call != ftrace_file->event_call);
  647. if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
  648. return;
  649. if (ftrace_trigger_soft_disabled(ftrace_file))
  650. return;
  651. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  652. size = esize + tu->tp.size + dsize;
  653. event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
  654. call->event.type, size, 0, 0);
  655. if (!event)
  656. return;
  657. entry = ring_buffer_event_data(event);
  658. if (is_ret_probe(tu)) {
  659. entry->vaddr[0] = func;
  660. entry->vaddr[1] = instruction_pointer(regs);
  661. data = DATAOF_TRACE_ENTRY(entry, true);
  662. } else {
  663. entry->vaddr[0] = instruction_pointer(regs);
  664. data = DATAOF_TRACE_ENTRY(entry, false);
  665. }
  666. memcpy(data, ucb->buf, tu->tp.size + dsize);
  667. event_trigger_unlock_commit(ftrace_file, buffer, event, entry, 0, 0);
  668. }
  669. /* uprobe handler */
  670. static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
  671. struct uprobe_cpu_buffer *ucb, int dsize)
  672. {
  673. struct event_file_link *link;
  674. if (is_ret_probe(tu))
  675. return 0;
  676. rcu_read_lock();
  677. list_for_each_entry_rcu(link, &tu->tp.files, list)
  678. __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
  679. rcu_read_unlock();
  680. return 0;
  681. }
  682. static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  683. struct pt_regs *regs,
  684. struct uprobe_cpu_buffer *ucb, int dsize)
  685. {
  686. struct event_file_link *link;
  687. rcu_read_lock();
  688. list_for_each_entry_rcu(link, &tu->tp.files, list)
  689. __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
  690. rcu_read_unlock();
  691. }
  692. /* Event entry printers */
  693. static enum print_line_t
  694. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  695. {
  696. struct uprobe_trace_entry_head *entry;
  697. struct trace_seq *s = &iter->seq;
  698. struct trace_uprobe *tu;
  699. u8 *data;
  700. int i;
  701. entry = (struct uprobe_trace_entry_head *)iter->ent;
  702. tu = container_of(event, struct trace_uprobe, tp.call.event);
  703. if (is_ret_probe(tu)) {
  704. trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
  705. ftrace_event_name(&tu->tp.call),
  706. entry->vaddr[1], entry->vaddr[0]);
  707. data = DATAOF_TRACE_ENTRY(entry, true);
  708. } else {
  709. trace_seq_printf(s, "%s: (0x%lx)",
  710. ftrace_event_name(&tu->tp.call),
  711. entry->vaddr[0]);
  712. data = DATAOF_TRACE_ENTRY(entry, false);
  713. }
  714. for (i = 0; i < tu->tp.nr_args; i++) {
  715. struct probe_arg *parg = &tu->tp.args[i];
  716. if (!parg->type->print(s, parg->name, data + parg->offset, entry))
  717. goto out;
  718. }
  719. trace_seq_putc(s, '\n');
  720. out:
  721. return trace_handle_return(s);
  722. }
  723. typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  724. enum uprobe_filter_ctx ctx,
  725. struct mm_struct *mm);
  726. static int
  727. probe_event_enable(struct trace_uprobe *tu, struct ftrace_event_file *file,
  728. filter_func_t filter)
  729. {
  730. bool enabled = trace_probe_is_enabled(&tu->tp);
  731. struct event_file_link *link = NULL;
  732. int ret;
  733. if (file) {
  734. if (tu->tp.flags & TP_FLAG_PROFILE)
  735. return -EINTR;
  736. link = kmalloc(sizeof(*link), GFP_KERNEL);
  737. if (!link)
  738. return -ENOMEM;
  739. link->file = file;
  740. list_add_tail_rcu(&link->list, &tu->tp.files);
  741. tu->tp.flags |= TP_FLAG_TRACE;
  742. } else {
  743. if (tu->tp.flags & TP_FLAG_TRACE)
  744. return -EINTR;
  745. tu->tp.flags |= TP_FLAG_PROFILE;
  746. }
  747. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  748. if (enabled)
  749. return 0;
  750. ret = uprobe_buffer_enable();
  751. if (ret)
  752. goto err_flags;
  753. tu->consumer.filter = filter;
  754. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  755. if (ret)
  756. goto err_buffer;
  757. return 0;
  758. err_buffer:
  759. uprobe_buffer_disable();
  760. err_flags:
  761. if (file) {
  762. list_del(&link->list);
  763. kfree(link);
  764. tu->tp.flags &= ~TP_FLAG_TRACE;
  765. } else {
  766. tu->tp.flags &= ~TP_FLAG_PROFILE;
  767. }
  768. return ret;
  769. }
  770. static void
  771. probe_event_disable(struct trace_uprobe *tu, struct ftrace_event_file *file)
  772. {
  773. if (!trace_probe_is_enabled(&tu->tp))
  774. return;
  775. if (file) {
  776. struct event_file_link *link;
  777. link = find_event_file_link(&tu->tp, file);
  778. if (!link)
  779. return;
  780. list_del_rcu(&link->list);
  781. /* synchronize with u{,ret}probe_trace_func */
  782. synchronize_sched();
  783. kfree(link);
  784. if (!list_empty(&tu->tp.files))
  785. return;
  786. }
  787. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  788. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  789. tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
  790. uprobe_buffer_disable();
  791. }
  792. static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
  793. {
  794. int ret, i, size;
  795. struct uprobe_trace_entry_head field;
  796. struct trace_uprobe *tu = event_call->data;
  797. if (is_ret_probe(tu)) {
  798. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  799. DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  800. size = SIZEOF_TRACE_ENTRY(true);
  801. } else {
  802. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  803. size = SIZEOF_TRACE_ENTRY(false);
  804. }
  805. /* Set argument names as fields */
  806. for (i = 0; i < tu->tp.nr_args; i++) {
  807. struct probe_arg *parg = &tu->tp.args[i];
  808. ret = trace_define_field(event_call, parg->type->fmttype,
  809. parg->name, size + parg->offset,
  810. parg->type->size, parg->type->is_signed,
  811. FILTER_OTHER);
  812. if (ret)
  813. return ret;
  814. }
  815. return 0;
  816. }
  817. #ifdef CONFIG_PERF_EVENTS
  818. static bool
  819. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  820. {
  821. struct perf_event *event;
  822. if (filter->nr_systemwide)
  823. return true;
  824. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  825. if (event->hw.tp_target->mm == mm)
  826. return true;
  827. }
  828. return false;
  829. }
  830. static inline bool
  831. uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
  832. {
  833. return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
  834. }
  835. static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  836. {
  837. bool done;
  838. write_lock(&tu->filter.rwlock);
  839. if (event->hw.tp_target) {
  840. list_del(&event->hw.tp_list);
  841. done = tu->filter.nr_systemwide ||
  842. (event->hw.tp_target->flags & PF_EXITING) ||
  843. uprobe_filter_event(tu, event);
  844. } else {
  845. tu->filter.nr_systemwide--;
  846. done = tu->filter.nr_systemwide;
  847. }
  848. write_unlock(&tu->filter.rwlock);
  849. if (!done)
  850. return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  851. return 0;
  852. }
  853. static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
  854. {
  855. bool done;
  856. int err;
  857. write_lock(&tu->filter.rwlock);
  858. if (event->hw.tp_target) {
  859. /*
  860. * event->parent != NULL means copy_process(), we can avoid
  861. * uprobe_apply(). current->mm must be probed and we can rely
  862. * on dup_mmap() which preserves the already installed bp's.
  863. *
  864. * attr.enable_on_exec means that exec/mmap will install the
  865. * breakpoints we need.
  866. */
  867. done = tu->filter.nr_systemwide ||
  868. event->parent || event->attr.enable_on_exec ||
  869. uprobe_filter_event(tu, event);
  870. list_add(&event->hw.tp_list, &tu->filter.perf_events);
  871. } else {
  872. done = tu->filter.nr_systemwide;
  873. tu->filter.nr_systemwide++;
  874. }
  875. write_unlock(&tu->filter.rwlock);
  876. err = 0;
  877. if (!done) {
  878. err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  879. if (err)
  880. uprobe_perf_close(tu, event);
  881. }
  882. return err;
  883. }
  884. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  885. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  886. {
  887. struct trace_uprobe *tu;
  888. int ret;
  889. tu = container_of(uc, struct trace_uprobe, consumer);
  890. read_lock(&tu->filter.rwlock);
  891. ret = __uprobe_perf_filter(&tu->filter, mm);
  892. read_unlock(&tu->filter.rwlock);
  893. return ret;
  894. }
  895. static void __uprobe_perf_func(struct trace_uprobe *tu,
  896. unsigned long func, struct pt_regs *regs,
  897. struct uprobe_cpu_buffer *ucb, int dsize)
  898. {
  899. struct ftrace_event_call *call = &tu->tp.call;
  900. struct uprobe_trace_entry_head *entry;
  901. struct hlist_head *head;
  902. void *data;
  903. int size, esize;
  904. int rctx;
  905. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  906. size = esize + tu->tp.size + dsize;
  907. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  908. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  909. return;
  910. preempt_disable();
  911. head = this_cpu_ptr(call->perf_events);
  912. if (hlist_empty(head))
  913. goto out;
  914. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  915. if (!entry)
  916. goto out;
  917. if (is_ret_probe(tu)) {
  918. entry->vaddr[0] = func;
  919. entry->vaddr[1] = instruction_pointer(regs);
  920. data = DATAOF_TRACE_ENTRY(entry, true);
  921. } else {
  922. entry->vaddr[0] = instruction_pointer(regs);
  923. data = DATAOF_TRACE_ENTRY(entry, false);
  924. }
  925. memcpy(data, ucb->buf, tu->tp.size + dsize);
  926. if (size - esize > tu->tp.size + dsize) {
  927. int len = tu->tp.size + dsize;
  928. memset(data + len, 0, size - esize - len);
  929. }
  930. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  931. out:
  932. preempt_enable();
  933. }
  934. /* uprobe profile handler */
  935. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
  936. struct uprobe_cpu_buffer *ucb, int dsize)
  937. {
  938. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  939. return UPROBE_HANDLER_REMOVE;
  940. if (!is_ret_probe(tu))
  941. __uprobe_perf_func(tu, 0, regs, ucb, dsize);
  942. return 0;
  943. }
  944. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  945. struct pt_regs *regs,
  946. struct uprobe_cpu_buffer *ucb, int dsize)
  947. {
  948. __uprobe_perf_func(tu, func, regs, ucb, dsize);
  949. }
  950. #endif /* CONFIG_PERF_EVENTS */
  951. static int
  952. trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type,
  953. void *data)
  954. {
  955. struct trace_uprobe *tu = event->data;
  956. struct ftrace_event_file *file = data;
  957. switch (type) {
  958. case TRACE_REG_REGISTER:
  959. return probe_event_enable(tu, file, NULL);
  960. case TRACE_REG_UNREGISTER:
  961. probe_event_disable(tu, file);
  962. return 0;
  963. #ifdef CONFIG_PERF_EVENTS
  964. case TRACE_REG_PERF_REGISTER:
  965. return probe_event_enable(tu, NULL, uprobe_perf_filter);
  966. case TRACE_REG_PERF_UNREGISTER:
  967. probe_event_disable(tu, NULL);
  968. return 0;
  969. case TRACE_REG_PERF_OPEN:
  970. return uprobe_perf_open(tu, data);
  971. case TRACE_REG_PERF_CLOSE:
  972. return uprobe_perf_close(tu, data);
  973. #endif
  974. default:
  975. return 0;
  976. }
  977. return 0;
  978. }
  979. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  980. {
  981. struct trace_uprobe *tu;
  982. struct uprobe_dispatch_data udd;
  983. struct uprobe_cpu_buffer *ucb;
  984. int dsize, esize;
  985. int ret = 0;
  986. tu = container_of(con, struct trace_uprobe, consumer);
  987. tu->nhit++;
  988. udd.tu = tu;
  989. udd.bp_addr = instruction_pointer(regs);
  990. current->utask->vaddr = (unsigned long) &udd;
  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->class->reg = trace_uprobe_register;
  1051. call->data = tu;
  1052. ret = trace_add_event_call(call);
  1053. if (ret) {
  1054. pr_info("Failed to register uprobe event: %s\n",
  1055. ftrace_event_name(call));
  1056. kfree(call->print_fmt);
  1057. unregister_ftrace_event(&call->event);
  1058. }
  1059. return ret;
  1060. }
  1061. static int unregister_uprobe_event(struct trace_uprobe *tu)
  1062. {
  1063. int ret;
  1064. /* tu->event is unregistered in trace_remove_event_call() */
  1065. ret = trace_remove_event_call(&tu->tp.call);
  1066. if (ret)
  1067. return ret;
  1068. kfree(tu->tp.call.print_fmt);
  1069. tu->tp.call.print_fmt = NULL;
  1070. return 0;
  1071. }
  1072. /* Make a trace interface for controling probe points */
  1073. static __init int init_uprobe_trace(void)
  1074. {
  1075. struct dentry *d_tracer;
  1076. d_tracer = tracing_init_dentry();
  1077. if (IS_ERR(d_tracer))
  1078. return 0;
  1079. trace_create_file("uprobe_events", 0644, d_tracer,
  1080. NULL, &uprobe_events_ops);
  1081. /* Profile interface */
  1082. trace_create_file("uprobe_profile", 0444, d_tracer,
  1083. NULL, &uprobe_profile_ops);
  1084. return 0;
  1085. }
  1086. fs_initcall(init_uprobe_trace);