trace_uprobe.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. static 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(trace_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(trace_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(d_inode(path.dentry));
  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. uprobes_fetch_type_table);
  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. iput(inode);
  466. pr_info("Failed to parse address or file.\n");
  467. return ret;
  468. }
  469. static int cleanup_all_probes(void)
  470. {
  471. struct trace_uprobe *tu;
  472. int ret = 0;
  473. mutex_lock(&uprobe_lock);
  474. while (!list_empty(&uprobe_list)) {
  475. tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
  476. ret = unregister_trace_uprobe(tu);
  477. if (ret)
  478. break;
  479. }
  480. mutex_unlock(&uprobe_lock);
  481. return ret;
  482. }
  483. /* Probes listing interfaces */
  484. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  485. {
  486. mutex_lock(&uprobe_lock);
  487. return seq_list_start(&uprobe_list, *pos);
  488. }
  489. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  490. {
  491. return seq_list_next(v, &uprobe_list, pos);
  492. }
  493. static void probes_seq_stop(struct seq_file *m, void *v)
  494. {
  495. mutex_unlock(&uprobe_lock);
  496. }
  497. static int probes_seq_show(struct seq_file *m, void *v)
  498. {
  499. struct trace_uprobe *tu = v;
  500. char c = is_ret_probe(tu) ? 'r' : 'p';
  501. int i;
  502. seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system,
  503. trace_event_name(&tu->tp.call));
  504. seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
  505. for (i = 0; i < tu->tp.nr_args; i++)
  506. seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
  507. seq_putc(m, '\n');
  508. return 0;
  509. }
  510. static const struct seq_operations probes_seq_op = {
  511. .start = probes_seq_start,
  512. .next = probes_seq_next,
  513. .stop = probes_seq_stop,
  514. .show = probes_seq_show
  515. };
  516. static int probes_open(struct inode *inode, struct file *file)
  517. {
  518. int ret;
  519. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  520. ret = cleanup_all_probes();
  521. if (ret)
  522. return ret;
  523. }
  524. return seq_open(file, &probes_seq_op);
  525. }
  526. static ssize_t probes_write(struct file *file, const char __user *buffer,
  527. size_t count, loff_t *ppos)
  528. {
  529. return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
  530. }
  531. static const struct file_operations uprobe_events_ops = {
  532. .owner = THIS_MODULE,
  533. .open = probes_open,
  534. .read = seq_read,
  535. .llseek = seq_lseek,
  536. .release = seq_release,
  537. .write = probes_write,
  538. };
  539. /* Probes profiling interfaces */
  540. static int probes_profile_seq_show(struct seq_file *m, void *v)
  541. {
  542. struct trace_uprobe *tu = v;
  543. seq_printf(m, " %s %-44s %15lu\n", tu->filename,
  544. trace_event_name(&tu->tp.call), tu->nhit);
  545. return 0;
  546. }
  547. static const struct seq_operations profile_seq_op = {
  548. .start = probes_seq_start,
  549. .next = probes_seq_next,
  550. .stop = probes_seq_stop,
  551. .show = probes_profile_seq_show
  552. };
  553. static int profile_open(struct inode *inode, struct file *file)
  554. {
  555. return seq_open(file, &profile_seq_op);
  556. }
  557. static const struct file_operations uprobe_profile_ops = {
  558. .owner = THIS_MODULE,
  559. .open = profile_open,
  560. .read = seq_read,
  561. .llseek = seq_lseek,
  562. .release = seq_release,
  563. };
  564. struct uprobe_cpu_buffer {
  565. struct mutex mutex;
  566. void *buf;
  567. };
  568. static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
  569. static int uprobe_buffer_refcnt;
  570. static int uprobe_buffer_init(void)
  571. {
  572. int cpu, err_cpu;
  573. uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
  574. if (uprobe_cpu_buffer == NULL)
  575. return -ENOMEM;
  576. for_each_possible_cpu(cpu) {
  577. struct page *p = alloc_pages_node(cpu_to_node(cpu),
  578. GFP_KERNEL, 0);
  579. if (p == NULL) {
  580. err_cpu = cpu;
  581. goto err;
  582. }
  583. per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
  584. mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
  585. }
  586. return 0;
  587. err:
  588. for_each_possible_cpu(cpu) {
  589. if (cpu == err_cpu)
  590. break;
  591. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
  592. }
  593. free_percpu(uprobe_cpu_buffer);
  594. return -ENOMEM;
  595. }
  596. static int uprobe_buffer_enable(void)
  597. {
  598. int ret = 0;
  599. BUG_ON(!mutex_is_locked(&event_mutex));
  600. if (uprobe_buffer_refcnt++ == 0) {
  601. ret = uprobe_buffer_init();
  602. if (ret < 0)
  603. uprobe_buffer_refcnt--;
  604. }
  605. return ret;
  606. }
  607. static void uprobe_buffer_disable(void)
  608. {
  609. int cpu;
  610. BUG_ON(!mutex_is_locked(&event_mutex));
  611. if (--uprobe_buffer_refcnt == 0) {
  612. for_each_possible_cpu(cpu)
  613. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
  614. cpu)->buf);
  615. free_percpu(uprobe_cpu_buffer);
  616. uprobe_cpu_buffer = NULL;
  617. }
  618. }
  619. static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
  620. {
  621. struct uprobe_cpu_buffer *ucb;
  622. int cpu;
  623. cpu = raw_smp_processor_id();
  624. ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
  625. /*
  626. * Use per-cpu buffers for fastest access, but we might migrate
  627. * so the mutex makes sure we have sole access to it.
  628. */
  629. mutex_lock(&ucb->mutex);
  630. return ucb;
  631. }
  632. static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
  633. {
  634. mutex_unlock(&ucb->mutex);
  635. }
  636. static void __uprobe_trace_func(struct trace_uprobe *tu,
  637. unsigned long func, struct pt_regs *regs,
  638. struct uprobe_cpu_buffer *ucb, int dsize,
  639. struct trace_event_file *trace_file)
  640. {
  641. struct uprobe_trace_entry_head *entry;
  642. struct ring_buffer_event *event;
  643. struct ring_buffer *buffer;
  644. void *data;
  645. int size, esize;
  646. struct trace_event_call *call = &tu->tp.call;
  647. WARN_ON(call != trace_file->event_call);
  648. if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
  649. return;
  650. if (trace_trigger_soft_disabled(trace_file))
  651. return;
  652. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  653. size = esize + tu->tp.size + dsize;
  654. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  655. call->event.type, size, 0, 0);
  656. if (!event)
  657. return;
  658. entry = ring_buffer_event_data(event);
  659. if (is_ret_probe(tu)) {
  660. entry->vaddr[0] = func;
  661. entry->vaddr[1] = instruction_pointer(regs);
  662. data = DATAOF_TRACE_ENTRY(entry, true);
  663. } else {
  664. entry->vaddr[0] = instruction_pointer(regs);
  665. data = DATAOF_TRACE_ENTRY(entry, false);
  666. }
  667. memcpy(data, ucb->buf, tu->tp.size + dsize);
  668. event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
  669. }
  670. /* uprobe handler */
  671. static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
  672. struct uprobe_cpu_buffer *ucb, int dsize)
  673. {
  674. struct event_file_link *link;
  675. if (is_ret_probe(tu))
  676. return 0;
  677. rcu_read_lock();
  678. list_for_each_entry_rcu(link, &tu->tp.files, list)
  679. __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
  680. rcu_read_unlock();
  681. return 0;
  682. }
  683. static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  684. struct pt_regs *regs,
  685. struct uprobe_cpu_buffer *ucb, int dsize)
  686. {
  687. struct event_file_link *link;
  688. rcu_read_lock();
  689. list_for_each_entry_rcu(link, &tu->tp.files, list)
  690. __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
  691. rcu_read_unlock();
  692. }
  693. /* Event entry printers */
  694. static enum print_line_t
  695. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  696. {
  697. struct uprobe_trace_entry_head *entry;
  698. struct trace_seq *s = &iter->seq;
  699. struct trace_uprobe *tu;
  700. u8 *data;
  701. int i;
  702. entry = (struct uprobe_trace_entry_head *)iter->ent;
  703. tu = container_of(event, struct trace_uprobe, tp.call.event);
  704. if (is_ret_probe(tu)) {
  705. trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
  706. trace_event_name(&tu->tp.call),
  707. entry->vaddr[1], entry->vaddr[0]);
  708. data = DATAOF_TRACE_ENTRY(entry, true);
  709. } else {
  710. trace_seq_printf(s, "%s: (0x%lx)",
  711. trace_event_name(&tu->tp.call),
  712. entry->vaddr[0]);
  713. data = DATAOF_TRACE_ENTRY(entry, false);
  714. }
  715. for (i = 0; i < tu->tp.nr_args; i++) {
  716. struct probe_arg *parg = &tu->tp.args[i];
  717. if (!parg->type->print(s, parg->name, data + parg->offset, entry))
  718. goto out;
  719. }
  720. trace_seq_putc(s, '\n');
  721. out:
  722. return trace_handle_return(s);
  723. }
  724. typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  725. enum uprobe_filter_ctx ctx,
  726. struct mm_struct *mm);
  727. static int
  728. probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
  729. filter_func_t filter)
  730. {
  731. bool enabled = trace_probe_is_enabled(&tu->tp);
  732. struct event_file_link *link = NULL;
  733. int ret;
  734. if (file) {
  735. if (tu->tp.flags & TP_FLAG_PROFILE)
  736. return -EINTR;
  737. link = kmalloc(sizeof(*link), GFP_KERNEL);
  738. if (!link)
  739. return -ENOMEM;
  740. link->file = file;
  741. list_add_tail_rcu(&link->list, &tu->tp.files);
  742. tu->tp.flags |= TP_FLAG_TRACE;
  743. } else {
  744. if (tu->tp.flags & TP_FLAG_TRACE)
  745. return -EINTR;
  746. tu->tp.flags |= TP_FLAG_PROFILE;
  747. }
  748. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  749. if (enabled)
  750. return 0;
  751. ret = uprobe_buffer_enable();
  752. if (ret)
  753. goto err_flags;
  754. tu->consumer.filter = filter;
  755. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  756. if (ret)
  757. goto err_buffer;
  758. return 0;
  759. err_buffer:
  760. uprobe_buffer_disable();
  761. err_flags:
  762. if (file) {
  763. list_del(&link->list);
  764. kfree(link);
  765. tu->tp.flags &= ~TP_FLAG_TRACE;
  766. } else {
  767. tu->tp.flags &= ~TP_FLAG_PROFILE;
  768. }
  769. return ret;
  770. }
  771. static void
  772. probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
  773. {
  774. if (!trace_probe_is_enabled(&tu->tp))
  775. return;
  776. if (file) {
  777. struct event_file_link *link;
  778. link = find_event_file_link(&tu->tp, file);
  779. if (!link)
  780. return;
  781. list_del_rcu(&link->list);
  782. /* synchronize with u{,ret}probe_trace_func */
  783. synchronize_sched();
  784. kfree(link);
  785. if (!list_empty(&tu->tp.files))
  786. return;
  787. }
  788. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  789. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  790. tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
  791. uprobe_buffer_disable();
  792. }
  793. static int uprobe_event_define_fields(struct trace_event_call *event_call)
  794. {
  795. int ret, i, size;
  796. struct uprobe_trace_entry_head field;
  797. struct trace_uprobe *tu = event_call->data;
  798. if (is_ret_probe(tu)) {
  799. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  800. DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  801. size = SIZEOF_TRACE_ENTRY(true);
  802. } else {
  803. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  804. size = SIZEOF_TRACE_ENTRY(false);
  805. }
  806. /* Set argument names as fields */
  807. for (i = 0; i < tu->tp.nr_args; i++) {
  808. struct probe_arg *parg = &tu->tp.args[i];
  809. ret = trace_define_field(event_call, parg->type->fmttype,
  810. parg->name, size + parg->offset,
  811. parg->type->size, parg->type->is_signed,
  812. FILTER_OTHER);
  813. if (ret)
  814. return ret;
  815. }
  816. return 0;
  817. }
  818. #ifdef CONFIG_PERF_EVENTS
  819. static bool
  820. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  821. {
  822. struct perf_event *event;
  823. if (filter->nr_systemwide)
  824. return true;
  825. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  826. if (event->hw.target->mm == mm)
  827. return true;
  828. }
  829. return false;
  830. }
  831. static inline bool
  832. uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
  833. {
  834. return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
  835. }
  836. static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  837. {
  838. bool done;
  839. write_lock(&tu->filter.rwlock);
  840. if (event->hw.target) {
  841. list_del(&event->hw.tp_list);
  842. done = tu->filter.nr_systemwide ||
  843. (event->hw.target->flags & PF_EXITING) ||
  844. uprobe_filter_event(tu, event);
  845. } else {
  846. tu->filter.nr_systemwide--;
  847. done = tu->filter.nr_systemwide;
  848. }
  849. write_unlock(&tu->filter.rwlock);
  850. if (!done)
  851. return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  852. return 0;
  853. }
  854. static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
  855. {
  856. bool done;
  857. int err;
  858. write_lock(&tu->filter.rwlock);
  859. if (event->hw.target) {
  860. /*
  861. * event->parent != NULL means copy_process(), we can avoid
  862. * uprobe_apply(). current->mm must be probed and we can rely
  863. * on dup_mmap() which preserves the already installed bp's.
  864. *
  865. * attr.enable_on_exec means that exec/mmap will install the
  866. * breakpoints we need.
  867. */
  868. done = tu->filter.nr_systemwide ||
  869. event->parent || event->attr.enable_on_exec ||
  870. uprobe_filter_event(tu, event);
  871. list_add(&event->hw.tp_list, &tu->filter.perf_events);
  872. } else {
  873. done = tu->filter.nr_systemwide;
  874. tu->filter.nr_systemwide++;
  875. }
  876. write_unlock(&tu->filter.rwlock);
  877. err = 0;
  878. if (!done) {
  879. err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  880. if (err)
  881. uprobe_perf_close(tu, event);
  882. }
  883. return err;
  884. }
  885. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  886. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  887. {
  888. struct trace_uprobe *tu;
  889. int ret;
  890. tu = container_of(uc, struct trace_uprobe, consumer);
  891. read_lock(&tu->filter.rwlock);
  892. ret = __uprobe_perf_filter(&tu->filter, mm);
  893. read_unlock(&tu->filter.rwlock);
  894. return ret;
  895. }
  896. static void __uprobe_perf_func(struct trace_uprobe *tu,
  897. unsigned long func, struct pt_regs *regs,
  898. struct uprobe_cpu_buffer *ucb, int dsize)
  899. {
  900. struct trace_event_call *call = &tu->tp.call;
  901. struct uprobe_trace_entry_head *entry;
  902. struct hlist_head *head;
  903. void *data;
  904. int size, esize;
  905. int rctx;
  906. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  907. size = esize + tu->tp.size + dsize;
  908. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  909. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  910. return;
  911. preempt_disable();
  912. head = this_cpu_ptr(call->perf_events);
  913. if (hlist_empty(head))
  914. goto out;
  915. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  916. if (!entry)
  917. goto out;
  918. if (is_ret_probe(tu)) {
  919. entry->vaddr[0] = func;
  920. entry->vaddr[1] = instruction_pointer(regs);
  921. data = DATAOF_TRACE_ENTRY(entry, true);
  922. } else {
  923. entry->vaddr[0] = instruction_pointer(regs);
  924. data = DATAOF_TRACE_ENTRY(entry, false);
  925. }
  926. memcpy(data, ucb->buf, tu->tp.size + dsize);
  927. if (size - esize > tu->tp.size + dsize) {
  928. int len = tu->tp.size + dsize;
  929. memset(data + len, 0, size - esize - len);
  930. }
  931. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  932. out:
  933. preempt_enable();
  934. }
  935. /* uprobe profile handler */
  936. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
  937. struct uprobe_cpu_buffer *ucb, int dsize)
  938. {
  939. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  940. return UPROBE_HANDLER_REMOVE;
  941. if (!is_ret_probe(tu))
  942. __uprobe_perf_func(tu, 0, regs, ucb, dsize);
  943. return 0;
  944. }
  945. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  946. struct pt_regs *regs,
  947. struct uprobe_cpu_buffer *ucb, int dsize)
  948. {
  949. __uprobe_perf_func(tu, func, regs, ucb, dsize);
  950. }
  951. #endif /* CONFIG_PERF_EVENTS */
  952. static int
  953. trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
  954. void *data)
  955. {
  956. struct trace_uprobe *tu = event->data;
  957. struct trace_event_file *file = data;
  958. switch (type) {
  959. case TRACE_REG_REGISTER:
  960. return probe_event_enable(tu, file, NULL);
  961. case TRACE_REG_UNREGISTER:
  962. probe_event_disable(tu, file);
  963. return 0;
  964. #ifdef CONFIG_PERF_EVENTS
  965. case TRACE_REG_PERF_REGISTER:
  966. return probe_event_enable(tu, NULL, uprobe_perf_filter);
  967. case TRACE_REG_PERF_UNREGISTER:
  968. probe_event_disable(tu, NULL);
  969. return 0;
  970. case TRACE_REG_PERF_OPEN:
  971. return uprobe_perf_open(tu, data);
  972. case TRACE_REG_PERF_CLOSE:
  973. return uprobe_perf_close(tu, data);
  974. #endif
  975. default:
  976. return 0;
  977. }
  978. return 0;
  979. }
  980. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  981. {
  982. struct trace_uprobe *tu;
  983. struct uprobe_dispatch_data udd;
  984. struct uprobe_cpu_buffer *ucb;
  985. int dsize, esize;
  986. int ret = 0;
  987. tu = container_of(con, struct trace_uprobe, consumer);
  988. tu->nhit++;
  989. udd.tu = tu;
  990. udd.bp_addr = instruction_pointer(regs);
  991. current->utask->vaddr = (unsigned long) &udd;
  992. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  993. return 0;
  994. dsize = __get_data_size(&tu->tp, regs);
  995. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  996. ucb = uprobe_buffer_get();
  997. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  998. if (tu->tp.flags & TP_FLAG_TRACE)
  999. ret |= uprobe_trace_func(tu, regs, ucb, dsize);
  1000. #ifdef CONFIG_PERF_EVENTS
  1001. if (tu->tp.flags & TP_FLAG_PROFILE)
  1002. ret |= uprobe_perf_func(tu, regs, ucb, dsize);
  1003. #endif
  1004. uprobe_buffer_put(ucb);
  1005. return ret;
  1006. }
  1007. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  1008. unsigned long func, struct pt_regs *regs)
  1009. {
  1010. struct trace_uprobe *tu;
  1011. struct uprobe_dispatch_data udd;
  1012. struct uprobe_cpu_buffer *ucb;
  1013. int dsize, esize;
  1014. tu = container_of(con, struct trace_uprobe, consumer);
  1015. udd.tu = tu;
  1016. udd.bp_addr = func;
  1017. current->utask->vaddr = (unsigned long) &udd;
  1018. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1019. return 0;
  1020. dsize = __get_data_size(&tu->tp, regs);
  1021. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1022. ucb = uprobe_buffer_get();
  1023. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  1024. if (tu->tp.flags & TP_FLAG_TRACE)
  1025. uretprobe_trace_func(tu, func, regs, ucb, dsize);
  1026. #ifdef CONFIG_PERF_EVENTS
  1027. if (tu->tp.flags & TP_FLAG_PROFILE)
  1028. uretprobe_perf_func(tu, func, regs, ucb, dsize);
  1029. #endif
  1030. uprobe_buffer_put(ucb);
  1031. return 0;
  1032. }
  1033. static struct trace_event_functions uprobe_funcs = {
  1034. .trace = print_uprobe_event
  1035. };
  1036. static int register_uprobe_event(struct trace_uprobe *tu)
  1037. {
  1038. struct trace_event_call *call = &tu->tp.call;
  1039. int ret;
  1040. /* Initialize trace_event_call */
  1041. INIT_LIST_HEAD(&call->class->fields);
  1042. call->event.funcs = &uprobe_funcs;
  1043. call->class->define_fields = uprobe_event_define_fields;
  1044. if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
  1045. return -ENOMEM;
  1046. ret = register_trace_event(&call->event);
  1047. if (!ret) {
  1048. kfree(call->print_fmt);
  1049. return -ENODEV;
  1050. }
  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. trace_event_name(call));
  1057. kfree(call->print_fmt);
  1058. unregister_trace_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 (IS_ERR(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);