trace_uprobe.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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 __kprobes 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 __kprobes 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 __kprobes 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 __kprobes 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 __kprobes 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_open(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. /*
  836. * event->parent != NULL means copy_process(), we can avoid
  837. * uprobe_apply(). current->mm must be probed and we can rely
  838. * on dup_mmap() which preserves the already installed bp's.
  839. *
  840. * attr.enable_on_exec means that exec/mmap will install the
  841. * breakpoints we need.
  842. */
  843. done = tu->filter.nr_systemwide ||
  844. event->parent || event->attr.enable_on_exec ||
  845. uprobe_filter_event(tu, event);
  846. list_add(&event->hw.tp_list, &tu->filter.perf_events);
  847. } else {
  848. done = tu->filter.nr_systemwide;
  849. tu->filter.nr_systemwide++;
  850. }
  851. write_unlock(&tu->filter.rwlock);
  852. if (!done)
  853. uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  854. return 0;
  855. }
  856. static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  857. {
  858. bool done;
  859. write_lock(&tu->filter.rwlock);
  860. if (event->hw.tp_target) {
  861. list_del(&event->hw.tp_list);
  862. done = tu->filter.nr_systemwide ||
  863. (event->hw.tp_target->flags & PF_EXITING) ||
  864. uprobe_filter_event(tu, event);
  865. } else {
  866. tu->filter.nr_systemwide--;
  867. done = tu->filter.nr_systemwide;
  868. }
  869. write_unlock(&tu->filter.rwlock);
  870. if (!done)
  871. uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  872. return 0;
  873. }
  874. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  875. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  876. {
  877. struct trace_uprobe *tu;
  878. int ret;
  879. tu = container_of(uc, struct trace_uprobe, consumer);
  880. read_lock(&tu->filter.rwlock);
  881. ret = __uprobe_perf_filter(&tu->filter, mm);
  882. read_unlock(&tu->filter.rwlock);
  883. return ret;
  884. }
  885. static void __uprobe_perf_func(struct trace_uprobe *tu,
  886. unsigned long func, struct pt_regs *regs,
  887. struct uprobe_cpu_buffer *ucb, int dsize)
  888. {
  889. struct ftrace_event_call *call = &tu->tp.call;
  890. struct uprobe_trace_entry_head *entry;
  891. struct hlist_head *head;
  892. void *data;
  893. int size, esize;
  894. int rctx;
  895. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  896. size = esize + tu->tp.size + dsize;
  897. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  898. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  899. return;
  900. preempt_disable();
  901. head = this_cpu_ptr(call->perf_events);
  902. if (hlist_empty(head))
  903. goto out;
  904. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  905. if (!entry)
  906. goto out;
  907. if (is_ret_probe(tu)) {
  908. entry->vaddr[0] = func;
  909. entry->vaddr[1] = instruction_pointer(regs);
  910. data = DATAOF_TRACE_ENTRY(entry, true);
  911. } else {
  912. entry->vaddr[0] = instruction_pointer(regs);
  913. data = DATAOF_TRACE_ENTRY(entry, false);
  914. }
  915. memcpy(data, ucb->buf, tu->tp.size + dsize);
  916. if (size - esize > tu->tp.size + dsize) {
  917. int len = tu->tp.size + dsize;
  918. memset(data + len, 0, size - esize - len);
  919. }
  920. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  921. out:
  922. preempt_enable();
  923. }
  924. /* uprobe profile handler */
  925. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
  926. struct uprobe_cpu_buffer *ucb, int dsize)
  927. {
  928. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  929. return UPROBE_HANDLER_REMOVE;
  930. if (!is_ret_probe(tu))
  931. __uprobe_perf_func(tu, 0, regs, ucb, dsize);
  932. return 0;
  933. }
  934. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  935. struct pt_regs *regs,
  936. struct uprobe_cpu_buffer *ucb, int dsize)
  937. {
  938. __uprobe_perf_func(tu, func, regs, ucb, dsize);
  939. }
  940. #endif /* CONFIG_PERF_EVENTS */
  941. static int
  942. trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type,
  943. void *data)
  944. {
  945. struct trace_uprobe *tu = event->data;
  946. struct ftrace_event_file *file = data;
  947. switch (type) {
  948. case TRACE_REG_REGISTER:
  949. return probe_event_enable(tu, file, NULL);
  950. case TRACE_REG_UNREGISTER:
  951. probe_event_disable(tu, file);
  952. return 0;
  953. #ifdef CONFIG_PERF_EVENTS
  954. case TRACE_REG_PERF_REGISTER:
  955. return probe_event_enable(tu, NULL, uprobe_perf_filter);
  956. case TRACE_REG_PERF_UNREGISTER:
  957. probe_event_disable(tu, NULL);
  958. return 0;
  959. case TRACE_REG_PERF_OPEN:
  960. return uprobe_perf_open(tu, data);
  961. case TRACE_REG_PERF_CLOSE:
  962. return uprobe_perf_close(tu, data);
  963. #endif
  964. default:
  965. return 0;
  966. }
  967. return 0;
  968. }
  969. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  970. {
  971. struct trace_uprobe *tu;
  972. struct uprobe_dispatch_data udd;
  973. struct uprobe_cpu_buffer *ucb;
  974. int dsize, esize;
  975. int ret = 0;
  976. tu = container_of(con, struct trace_uprobe, consumer);
  977. tu->nhit++;
  978. udd.tu = tu;
  979. udd.bp_addr = instruction_pointer(regs);
  980. current->utask->vaddr = (unsigned long) &udd;
  981. #ifdef CONFIG_PERF_EVENTS
  982. if ((tu->tp.flags & TP_FLAG_TRACE) == 0 &&
  983. !uprobe_perf_filter(&tu->consumer, 0, current->mm))
  984. return UPROBE_HANDLER_REMOVE;
  985. #endif
  986. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  987. return 0;
  988. dsize = __get_data_size(&tu->tp, regs);
  989. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  990. ucb = uprobe_buffer_get();
  991. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  992. if (tu->tp.flags & TP_FLAG_TRACE)
  993. ret |= uprobe_trace_func(tu, regs, ucb, dsize);
  994. #ifdef CONFIG_PERF_EVENTS
  995. if (tu->tp.flags & TP_FLAG_PROFILE)
  996. ret |= uprobe_perf_func(tu, regs, ucb, dsize);
  997. #endif
  998. uprobe_buffer_put(ucb);
  999. return ret;
  1000. }
  1001. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  1002. unsigned long func, struct pt_regs *regs)
  1003. {
  1004. struct trace_uprobe *tu;
  1005. struct uprobe_dispatch_data udd;
  1006. struct uprobe_cpu_buffer *ucb;
  1007. int dsize, esize;
  1008. tu = container_of(con, struct trace_uprobe, consumer);
  1009. udd.tu = tu;
  1010. udd.bp_addr = func;
  1011. current->utask->vaddr = (unsigned long) &udd;
  1012. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1013. return 0;
  1014. dsize = __get_data_size(&tu->tp, regs);
  1015. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1016. ucb = uprobe_buffer_get();
  1017. store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
  1018. if (tu->tp.flags & TP_FLAG_TRACE)
  1019. uretprobe_trace_func(tu, func, regs, ucb, dsize);
  1020. #ifdef CONFIG_PERF_EVENTS
  1021. if (tu->tp.flags & TP_FLAG_PROFILE)
  1022. uretprobe_perf_func(tu, func, regs, ucb, dsize);
  1023. #endif
  1024. uprobe_buffer_put(ucb);
  1025. return 0;
  1026. }
  1027. static struct trace_event_functions uprobe_funcs = {
  1028. .trace = print_uprobe_event
  1029. };
  1030. static int register_uprobe_event(struct trace_uprobe *tu)
  1031. {
  1032. struct ftrace_event_call *call = &tu->tp.call;
  1033. int ret;
  1034. /* Initialize ftrace_event_call */
  1035. INIT_LIST_HEAD(&call->class->fields);
  1036. call->event.funcs = &uprobe_funcs;
  1037. call->class->define_fields = uprobe_event_define_fields;
  1038. if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
  1039. return -ENOMEM;
  1040. ret = register_ftrace_event(&call->event);
  1041. if (!ret) {
  1042. kfree(call->print_fmt);
  1043. return -ENODEV;
  1044. }
  1045. call->flags = 0;
  1046. call->class->reg = trace_uprobe_register;
  1047. call->data = tu;
  1048. ret = trace_add_event_call(call);
  1049. if (ret) {
  1050. pr_info("Failed to register uprobe event: %s\n",
  1051. ftrace_event_name(call));
  1052. kfree(call->print_fmt);
  1053. unregister_ftrace_event(&call->event);
  1054. }
  1055. return ret;
  1056. }
  1057. static int unregister_uprobe_event(struct trace_uprobe *tu)
  1058. {
  1059. int ret;
  1060. /* tu->event is unregistered in trace_remove_event_call() */
  1061. ret = trace_remove_event_call(&tu->tp.call);
  1062. if (ret)
  1063. return ret;
  1064. kfree(tu->tp.call.print_fmt);
  1065. tu->tp.call.print_fmt = NULL;
  1066. return 0;
  1067. }
  1068. /* Make a trace interface for controling probe points */
  1069. static __init int init_uprobe_trace(void)
  1070. {
  1071. struct dentry *d_tracer;
  1072. d_tracer = tracing_init_dentry();
  1073. if (!d_tracer)
  1074. return 0;
  1075. trace_create_file("uprobe_events", 0644, d_tracer,
  1076. NULL, &uprobe_events_ops);
  1077. /* Profile interface */
  1078. trace_create_file("uprobe_profile", 0444, d_tracer,
  1079. NULL, &uprobe_profile_ops);
  1080. return 0;
  1081. }
  1082. fs_initcall(init_uprobe_trace);