trace_kprobe.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * Kprobes-based tracing events
  3. *
  4. * Created by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include "trace_probe.h"
  22. #define KPROBE_EVENT_SYSTEM "kprobes"
  23. /**
  24. * Kprobe event core functions
  25. */
  26. struct trace_probe {
  27. struct list_head list;
  28. struct kretprobe rp; /* Use rp.kp for kprobe use */
  29. unsigned long nhit;
  30. unsigned int flags; /* For TP_FLAG_* */
  31. const char *symbol; /* symbol name */
  32. struct ftrace_event_class class;
  33. struct ftrace_event_call call;
  34. ssize_t size; /* trace entry size */
  35. unsigned int nr_args;
  36. struct probe_arg args[];
  37. };
  38. #define SIZEOF_TRACE_PROBE(n) \
  39. (offsetof(struct trace_probe, args) + \
  40. (sizeof(struct probe_arg) * (n)))
  41. static __kprobes int trace_probe_is_return(struct trace_probe *tp)
  42. {
  43. return tp->rp.handler != NULL;
  44. }
  45. static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
  46. {
  47. return tp->symbol ? tp->symbol : "unknown";
  48. }
  49. static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp)
  50. {
  51. return tp->rp.kp.offset;
  52. }
  53. static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp)
  54. {
  55. return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
  56. }
  57. static __kprobes bool trace_probe_is_registered(struct trace_probe *tp)
  58. {
  59. return !!(tp->flags & TP_FLAG_REGISTERED);
  60. }
  61. static __kprobes bool trace_probe_has_gone(struct trace_probe *tp)
  62. {
  63. return !!(kprobe_gone(&tp->rp.kp));
  64. }
  65. static __kprobes bool trace_probe_within_module(struct trace_probe *tp,
  66. struct module *mod)
  67. {
  68. int len = strlen(mod->name);
  69. const char *name = trace_probe_symbol(tp);
  70. return strncmp(mod->name, name, len) == 0 && name[len] == ':';
  71. }
  72. static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp)
  73. {
  74. return !!strchr(trace_probe_symbol(tp), ':');
  75. }
  76. static int register_probe_event(struct trace_probe *tp);
  77. static void unregister_probe_event(struct trace_probe *tp);
  78. static DEFINE_MUTEX(probe_lock);
  79. static LIST_HEAD(probe_list);
  80. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  81. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  82. struct pt_regs *regs);
  83. /*
  84. * Allocate new trace_probe and initialize it (including kprobes).
  85. */
  86. static struct trace_probe *alloc_trace_probe(const char *group,
  87. const char *event,
  88. void *addr,
  89. const char *symbol,
  90. unsigned long offs,
  91. int nargs, bool is_return)
  92. {
  93. struct trace_probe *tp;
  94. int ret = -ENOMEM;
  95. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  96. if (!tp)
  97. return ERR_PTR(ret);
  98. if (symbol) {
  99. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  100. if (!tp->symbol)
  101. goto error;
  102. tp->rp.kp.symbol_name = tp->symbol;
  103. tp->rp.kp.offset = offs;
  104. } else
  105. tp->rp.kp.addr = addr;
  106. if (is_return)
  107. tp->rp.handler = kretprobe_dispatcher;
  108. else
  109. tp->rp.kp.pre_handler = kprobe_dispatcher;
  110. if (!event || !is_good_name(event)) {
  111. ret = -EINVAL;
  112. goto error;
  113. }
  114. tp->call.class = &tp->class;
  115. tp->call.name = kstrdup(event, GFP_KERNEL);
  116. if (!tp->call.name)
  117. goto error;
  118. if (!group || !is_good_name(group)) {
  119. ret = -EINVAL;
  120. goto error;
  121. }
  122. tp->class.system = kstrdup(group, GFP_KERNEL);
  123. if (!tp->class.system)
  124. goto error;
  125. INIT_LIST_HEAD(&tp->list);
  126. return tp;
  127. error:
  128. kfree(tp->call.name);
  129. kfree(tp->symbol);
  130. kfree(tp);
  131. return ERR_PTR(ret);
  132. }
  133. static void free_trace_probe(struct trace_probe *tp)
  134. {
  135. int i;
  136. for (i = 0; i < tp->nr_args; i++)
  137. traceprobe_free_probe_arg(&tp->args[i]);
  138. kfree(tp->call.class->system);
  139. kfree(tp->call.name);
  140. kfree(tp->symbol);
  141. kfree(tp);
  142. }
  143. static struct trace_probe *find_trace_probe(const char *event,
  144. const char *group)
  145. {
  146. struct trace_probe *tp;
  147. list_for_each_entry(tp, &probe_list, list)
  148. if (strcmp(tp->call.name, event) == 0 &&
  149. strcmp(tp->call.class->system, group) == 0)
  150. return tp;
  151. return NULL;
  152. }
  153. /* Enable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  154. static int enable_trace_probe(struct trace_probe *tp, int flag)
  155. {
  156. int ret = 0;
  157. tp->flags |= flag;
  158. if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
  159. !trace_probe_has_gone(tp)) {
  160. if (trace_probe_is_return(tp))
  161. ret = enable_kretprobe(&tp->rp);
  162. else
  163. ret = enable_kprobe(&tp->rp.kp);
  164. }
  165. return ret;
  166. }
  167. /* Disable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  168. static void disable_trace_probe(struct trace_probe *tp, int flag)
  169. {
  170. tp->flags &= ~flag;
  171. if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
  172. if (trace_probe_is_return(tp))
  173. disable_kretprobe(&tp->rp);
  174. else
  175. disable_kprobe(&tp->rp.kp);
  176. }
  177. }
  178. /* Internal register function - just handle k*probes and flags */
  179. static int __register_trace_probe(struct trace_probe *tp)
  180. {
  181. int i, ret;
  182. if (trace_probe_is_registered(tp))
  183. return -EINVAL;
  184. for (i = 0; i < tp->nr_args; i++)
  185. traceprobe_update_arg(&tp->args[i]);
  186. /* Set/clear disabled flag according to tp->flag */
  187. if (trace_probe_is_enabled(tp))
  188. tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  189. else
  190. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  191. if (trace_probe_is_return(tp))
  192. ret = register_kretprobe(&tp->rp);
  193. else
  194. ret = register_kprobe(&tp->rp.kp);
  195. if (ret == 0)
  196. tp->flags |= TP_FLAG_REGISTERED;
  197. else {
  198. pr_warning("Could not insert probe at %s+%lu: %d\n",
  199. trace_probe_symbol(tp), trace_probe_offset(tp), ret);
  200. if (ret == -ENOENT && trace_probe_is_on_module(tp)) {
  201. pr_warning("This probe might be able to register after"
  202. "target module is loaded. Continue.\n");
  203. ret = 0;
  204. } else if (ret == -EILSEQ) {
  205. pr_warning("Probing address(0x%p) is not an "
  206. "instruction boundary.\n",
  207. tp->rp.kp.addr);
  208. ret = -EINVAL;
  209. }
  210. }
  211. return ret;
  212. }
  213. /* Internal unregister function - just handle k*probes and flags */
  214. static void __unregister_trace_probe(struct trace_probe *tp)
  215. {
  216. if (trace_probe_is_registered(tp)) {
  217. if (trace_probe_is_return(tp))
  218. unregister_kretprobe(&tp->rp);
  219. else
  220. unregister_kprobe(&tp->rp.kp);
  221. tp->flags &= ~TP_FLAG_REGISTERED;
  222. /* Cleanup kprobe for reuse */
  223. if (tp->rp.kp.symbol_name)
  224. tp->rp.kp.addr = NULL;
  225. }
  226. }
  227. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  228. static int unregister_trace_probe(struct trace_probe *tp)
  229. {
  230. /* Enabled event can not be unregistered */
  231. if (trace_probe_is_enabled(tp))
  232. return -EBUSY;
  233. __unregister_trace_probe(tp);
  234. list_del(&tp->list);
  235. unregister_probe_event(tp);
  236. return 0;
  237. }
  238. /* Register a trace_probe and probe_event */
  239. static int register_trace_probe(struct trace_probe *tp)
  240. {
  241. struct trace_probe *old_tp;
  242. int ret;
  243. mutex_lock(&probe_lock);
  244. /* Delete old (same name) event if exist */
  245. old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
  246. if (old_tp) {
  247. ret = unregister_trace_probe(old_tp);
  248. if (ret < 0)
  249. goto end;
  250. free_trace_probe(old_tp);
  251. }
  252. /* Register new event */
  253. ret = register_probe_event(tp);
  254. if (ret) {
  255. pr_warning("Failed to register probe event(%d)\n", ret);
  256. goto end;
  257. }
  258. /* Register k*probe */
  259. ret = __register_trace_probe(tp);
  260. if (ret < 0)
  261. unregister_probe_event(tp);
  262. else
  263. list_add_tail(&tp->list, &probe_list);
  264. end:
  265. mutex_unlock(&probe_lock);
  266. return ret;
  267. }
  268. /* Module notifier call back, checking event on the module */
  269. static int trace_probe_module_callback(struct notifier_block *nb,
  270. unsigned long val, void *data)
  271. {
  272. struct module *mod = data;
  273. struct trace_probe *tp;
  274. int ret;
  275. if (val != MODULE_STATE_COMING)
  276. return NOTIFY_DONE;
  277. /* Update probes on coming module */
  278. mutex_lock(&probe_lock);
  279. list_for_each_entry(tp, &probe_list, list) {
  280. if (trace_probe_within_module(tp, mod)) {
  281. /* Don't need to check busy - this should have gone. */
  282. __unregister_trace_probe(tp);
  283. ret = __register_trace_probe(tp);
  284. if (ret)
  285. pr_warning("Failed to re-register probe %s on"
  286. "%s: %d\n",
  287. tp->call.name, mod->name, ret);
  288. }
  289. }
  290. mutex_unlock(&probe_lock);
  291. return NOTIFY_DONE;
  292. }
  293. static struct notifier_block trace_probe_module_nb = {
  294. .notifier_call = trace_probe_module_callback,
  295. .priority = 1 /* Invoked after kprobe module callback */
  296. };
  297. static int create_trace_probe(int argc, char **argv)
  298. {
  299. /*
  300. * Argument syntax:
  301. * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  302. * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  303. * Fetch args:
  304. * $retval : fetch return value
  305. * $stack : fetch stack address
  306. * $stackN : fetch Nth of stack (N:0-)
  307. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  308. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  309. * %REG : fetch register REG
  310. * Dereferencing memory fetch:
  311. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  312. * Alias name of args:
  313. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  314. * Type of args:
  315. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  316. */
  317. struct trace_probe *tp;
  318. int i, ret = 0;
  319. bool is_return = false, is_delete = false;
  320. char *symbol = NULL, *event = NULL, *group = NULL;
  321. char *arg;
  322. unsigned long offset = 0;
  323. void *addr = NULL;
  324. char buf[MAX_EVENT_NAME_LEN];
  325. /* argc must be >= 1 */
  326. if (argv[0][0] == 'p')
  327. is_return = false;
  328. else if (argv[0][0] == 'r')
  329. is_return = true;
  330. else if (argv[0][0] == '-')
  331. is_delete = true;
  332. else {
  333. pr_info("Probe definition must be started with 'p', 'r' or"
  334. " '-'.\n");
  335. return -EINVAL;
  336. }
  337. if (argv[0][1] == ':') {
  338. event = &argv[0][2];
  339. if (strchr(event, '/')) {
  340. group = event;
  341. event = strchr(group, '/') + 1;
  342. event[-1] = '\0';
  343. if (strlen(group) == 0) {
  344. pr_info("Group name is not specified\n");
  345. return -EINVAL;
  346. }
  347. }
  348. if (strlen(event) == 0) {
  349. pr_info("Event name is not specified\n");
  350. return -EINVAL;
  351. }
  352. }
  353. if (!group)
  354. group = KPROBE_EVENT_SYSTEM;
  355. if (is_delete) {
  356. if (!event) {
  357. pr_info("Delete command needs an event name.\n");
  358. return -EINVAL;
  359. }
  360. mutex_lock(&probe_lock);
  361. tp = find_trace_probe(event, group);
  362. if (!tp) {
  363. mutex_unlock(&probe_lock);
  364. pr_info("Event %s/%s doesn't exist.\n", group, event);
  365. return -ENOENT;
  366. }
  367. /* delete an event */
  368. ret = unregister_trace_probe(tp);
  369. if (ret == 0)
  370. free_trace_probe(tp);
  371. mutex_unlock(&probe_lock);
  372. return ret;
  373. }
  374. if (argc < 2) {
  375. pr_info("Probe point is not specified.\n");
  376. return -EINVAL;
  377. }
  378. if (isdigit(argv[1][0])) {
  379. if (is_return) {
  380. pr_info("Return probe point must be a symbol.\n");
  381. return -EINVAL;
  382. }
  383. /* an address specified */
  384. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  385. if (ret) {
  386. pr_info("Failed to parse address.\n");
  387. return ret;
  388. }
  389. } else {
  390. /* a symbol specified */
  391. symbol = argv[1];
  392. /* TODO: support .init module functions */
  393. ret = traceprobe_split_symbol_offset(symbol, &offset);
  394. if (ret) {
  395. pr_info("Failed to parse symbol.\n");
  396. return ret;
  397. }
  398. if (offset && is_return) {
  399. pr_info("Return probe must be used without offset.\n");
  400. return -EINVAL;
  401. }
  402. }
  403. argc -= 2; argv += 2;
  404. /* setup a probe */
  405. if (!event) {
  406. /* Make a new event name */
  407. if (symbol)
  408. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  409. is_return ? 'r' : 'p', symbol, offset);
  410. else
  411. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  412. is_return ? 'r' : 'p', addr);
  413. event = buf;
  414. }
  415. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  416. is_return);
  417. if (IS_ERR(tp)) {
  418. pr_info("Failed to allocate trace_probe.(%d)\n",
  419. (int)PTR_ERR(tp));
  420. return PTR_ERR(tp);
  421. }
  422. /* parse arguments */
  423. ret = 0;
  424. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  425. /* Increment count for freeing args in error case */
  426. tp->nr_args++;
  427. /* Parse argument name */
  428. arg = strchr(argv[i], '=');
  429. if (arg) {
  430. *arg++ = '\0';
  431. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  432. } else {
  433. arg = argv[i];
  434. /* If argument name is omitted, set "argN" */
  435. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  436. tp->args[i].name = kstrdup(buf, GFP_KERNEL);
  437. }
  438. if (!tp->args[i].name) {
  439. pr_info("Failed to allocate argument[%d] name.\n", i);
  440. ret = -ENOMEM;
  441. goto error;
  442. }
  443. if (!is_good_name(tp->args[i].name)) {
  444. pr_info("Invalid argument[%d] name: %s\n",
  445. i, tp->args[i].name);
  446. ret = -EINVAL;
  447. goto error;
  448. }
  449. if (traceprobe_conflict_field_name(tp->args[i].name,
  450. tp->args, i)) {
  451. pr_info("Argument[%d] name '%s' conflicts with "
  452. "another field.\n", i, argv[i]);
  453. ret = -EINVAL;
  454. goto error;
  455. }
  456. /* Parse fetch argument */
  457. ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i],
  458. is_return, true);
  459. if (ret) {
  460. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  461. goto error;
  462. }
  463. }
  464. ret = register_trace_probe(tp);
  465. if (ret)
  466. goto error;
  467. return 0;
  468. error:
  469. free_trace_probe(tp);
  470. return ret;
  471. }
  472. static int release_all_trace_probes(void)
  473. {
  474. struct trace_probe *tp;
  475. int ret = 0;
  476. mutex_lock(&probe_lock);
  477. /* Ensure no probe is in use. */
  478. list_for_each_entry(tp, &probe_list, list)
  479. if (trace_probe_is_enabled(tp)) {
  480. ret = -EBUSY;
  481. goto end;
  482. }
  483. /* TODO: Use batch unregistration */
  484. while (!list_empty(&probe_list)) {
  485. tp = list_entry(probe_list.next, struct trace_probe, list);
  486. unregister_trace_probe(tp);
  487. free_trace_probe(tp);
  488. }
  489. end:
  490. mutex_unlock(&probe_lock);
  491. return ret;
  492. }
  493. /* Probes listing interfaces */
  494. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  495. {
  496. mutex_lock(&probe_lock);
  497. return seq_list_start(&probe_list, *pos);
  498. }
  499. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  500. {
  501. return seq_list_next(v, &probe_list, pos);
  502. }
  503. static void probes_seq_stop(struct seq_file *m, void *v)
  504. {
  505. mutex_unlock(&probe_lock);
  506. }
  507. static int probes_seq_show(struct seq_file *m, void *v)
  508. {
  509. struct trace_probe *tp = v;
  510. int i;
  511. seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
  512. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  513. if (!tp->symbol)
  514. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  515. else if (tp->rp.kp.offset)
  516. seq_printf(m, " %s+%u", trace_probe_symbol(tp),
  517. tp->rp.kp.offset);
  518. else
  519. seq_printf(m, " %s", trace_probe_symbol(tp));
  520. for (i = 0; i < tp->nr_args; i++)
  521. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  522. seq_printf(m, "\n");
  523. return 0;
  524. }
  525. static const struct seq_operations probes_seq_op = {
  526. .start = probes_seq_start,
  527. .next = probes_seq_next,
  528. .stop = probes_seq_stop,
  529. .show = probes_seq_show
  530. };
  531. static int probes_open(struct inode *inode, struct file *file)
  532. {
  533. int ret;
  534. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  535. ret = release_all_trace_probes();
  536. if (ret < 0)
  537. return ret;
  538. }
  539. return seq_open(file, &probes_seq_op);
  540. }
  541. static ssize_t probes_write(struct file *file, const char __user *buffer,
  542. size_t count, loff_t *ppos)
  543. {
  544. return traceprobe_probes_write(file, buffer, count, ppos,
  545. create_trace_probe);
  546. }
  547. static const struct file_operations kprobe_events_ops = {
  548. .owner = THIS_MODULE,
  549. .open = probes_open,
  550. .read = seq_read,
  551. .llseek = seq_lseek,
  552. .release = seq_release,
  553. .write = probes_write,
  554. };
  555. /* Probes profiling interfaces */
  556. static int probes_profile_seq_show(struct seq_file *m, void *v)
  557. {
  558. struct trace_probe *tp = v;
  559. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  560. tp->rp.kp.nmissed);
  561. return 0;
  562. }
  563. static const struct seq_operations profile_seq_op = {
  564. .start = probes_seq_start,
  565. .next = probes_seq_next,
  566. .stop = probes_seq_stop,
  567. .show = probes_profile_seq_show
  568. };
  569. static int profile_open(struct inode *inode, struct file *file)
  570. {
  571. return seq_open(file, &profile_seq_op);
  572. }
  573. static const struct file_operations kprobe_profile_ops = {
  574. .owner = THIS_MODULE,
  575. .open = profile_open,
  576. .read = seq_read,
  577. .llseek = seq_lseek,
  578. .release = seq_release,
  579. };
  580. /* Sum up total data length for dynamic arraies (strings) */
  581. static __kprobes int __get_data_size(struct trace_probe *tp,
  582. struct pt_regs *regs)
  583. {
  584. int i, ret = 0;
  585. u32 len;
  586. for (i = 0; i < tp->nr_args; i++)
  587. if (unlikely(tp->args[i].fetch_size.fn)) {
  588. call_fetch(&tp->args[i].fetch_size, regs, &len);
  589. ret += len;
  590. }
  591. return ret;
  592. }
  593. /* Store the value of each argument */
  594. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  595. struct pt_regs *regs,
  596. u8 *data, int maxlen)
  597. {
  598. int i;
  599. u32 end = tp->size;
  600. u32 *dl; /* Data (relative) location */
  601. for (i = 0; i < tp->nr_args; i++) {
  602. if (unlikely(tp->args[i].fetch_size.fn)) {
  603. /*
  604. * First, we set the relative location and
  605. * maximum data length to *dl
  606. */
  607. dl = (u32 *)(data + tp->args[i].offset);
  608. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  609. /* Then try to fetch string or dynamic array data */
  610. call_fetch(&tp->args[i].fetch, regs, dl);
  611. /* Reduce maximum length */
  612. end += get_rloc_len(*dl);
  613. maxlen -= get_rloc_len(*dl);
  614. /* Trick here, convert data_rloc to data_loc */
  615. *dl = convert_rloc_to_loc(*dl,
  616. ent_size + tp->args[i].offset);
  617. } else
  618. /* Just fetching data normally */
  619. call_fetch(&tp->args[i].fetch, regs,
  620. data + tp->args[i].offset);
  621. }
  622. }
  623. /* Kprobe handler */
  624. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  625. {
  626. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  627. struct kprobe_trace_entry_head *entry;
  628. struct ring_buffer_event *event;
  629. struct ring_buffer *buffer;
  630. int size, dsize, pc;
  631. unsigned long irq_flags;
  632. struct ftrace_event_call *call = &tp->call;
  633. tp->nhit++;
  634. local_save_flags(irq_flags);
  635. pc = preempt_count();
  636. dsize = __get_data_size(tp, regs);
  637. size = sizeof(*entry) + tp->size + dsize;
  638. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  639. size, irq_flags, pc);
  640. if (!event)
  641. return;
  642. entry = ring_buffer_event_data(event);
  643. entry->ip = (unsigned long)kp->addr;
  644. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  645. if (!filter_current_check_discard(buffer, call, entry, event))
  646. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  647. irq_flags, pc, regs);
  648. }
  649. /* Kretprobe handler */
  650. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  651. struct pt_regs *regs)
  652. {
  653. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  654. struct kretprobe_trace_entry_head *entry;
  655. struct ring_buffer_event *event;
  656. struct ring_buffer *buffer;
  657. int size, pc, dsize;
  658. unsigned long irq_flags;
  659. struct ftrace_event_call *call = &tp->call;
  660. local_save_flags(irq_flags);
  661. pc = preempt_count();
  662. dsize = __get_data_size(tp, regs);
  663. size = sizeof(*entry) + tp->size + dsize;
  664. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  665. size, irq_flags, pc);
  666. if (!event)
  667. return;
  668. entry = ring_buffer_event_data(event);
  669. entry->func = (unsigned long)tp->rp.kp.addr;
  670. entry->ret_ip = (unsigned long)ri->ret_addr;
  671. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  672. if (!filter_current_check_discard(buffer, call, entry, event))
  673. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  674. irq_flags, pc, regs);
  675. }
  676. /* Event entry printers */
  677. enum print_line_t
  678. print_kprobe_event(struct trace_iterator *iter, int flags,
  679. struct trace_event *event)
  680. {
  681. struct kprobe_trace_entry_head *field;
  682. struct trace_seq *s = &iter->seq;
  683. struct trace_probe *tp;
  684. u8 *data;
  685. int i;
  686. field = (struct kprobe_trace_entry_head *)iter->ent;
  687. tp = container_of(event, struct trace_probe, call.event);
  688. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  689. goto partial;
  690. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  691. goto partial;
  692. if (!trace_seq_puts(s, ")"))
  693. goto partial;
  694. data = (u8 *)&field[1];
  695. for (i = 0; i < tp->nr_args; i++)
  696. if (!tp->args[i].type->print(s, tp->args[i].name,
  697. data + tp->args[i].offset, field))
  698. goto partial;
  699. if (!trace_seq_puts(s, "\n"))
  700. goto partial;
  701. return TRACE_TYPE_HANDLED;
  702. partial:
  703. return TRACE_TYPE_PARTIAL_LINE;
  704. }
  705. enum print_line_t
  706. print_kretprobe_event(struct trace_iterator *iter, int flags,
  707. struct trace_event *event)
  708. {
  709. struct kretprobe_trace_entry_head *field;
  710. struct trace_seq *s = &iter->seq;
  711. struct trace_probe *tp;
  712. u8 *data;
  713. int i;
  714. field = (struct kretprobe_trace_entry_head *)iter->ent;
  715. tp = container_of(event, struct trace_probe, call.event);
  716. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  717. goto partial;
  718. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  719. goto partial;
  720. if (!trace_seq_puts(s, " <- "))
  721. goto partial;
  722. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  723. goto partial;
  724. if (!trace_seq_puts(s, ")"))
  725. goto partial;
  726. data = (u8 *)&field[1];
  727. for (i = 0; i < tp->nr_args; i++)
  728. if (!tp->args[i].type->print(s, tp->args[i].name,
  729. data + tp->args[i].offset, field))
  730. goto partial;
  731. if (!trace_seq_puts(s, "\n"))
  732. goto partial;
  733. return TRACE_TYPE_HANDLED;
  734. partial:
  735. return TRACE_TYPE_PARTIAL_LINE;
  736. }
  737. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  738. {
  739. int ret, i;
  740. struct kprobe_trace_entry_head field;
  741. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  742. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  743. /* Set argument names as fields */
  744. for (i = 0; i < tp->nr_args; i++) {
  745. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  746. tp->args[i].name,
  747. sizeof(field) + tp->args[i].offset,
  748. tp->args[i].type->size,
  749. tp->args[i].type->is_signed,
  750. FILTER_OTHER);
  751. if (ret)
  752. return ret;
  753. }
  754. return 0;
  755. }
  756. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  757. {
  758. int ret, i;
  759. struct kretprobe_trace_entry_head field;
  760. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  761. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  762. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  763. /* Set argument names as fields */
  764. for (i = 0; i < tp->nr_args; i++) {
  765. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  766. tp->args[i].name,
  767. sizeof(field) + tp->args[i].offset,
  768. tp->args[i].type->size,
  769. tp->args[i].type->is_signed,
  770. FILTER_OTHER);
  771. if (ret)
  772. return ret;
  773. }
  774. return 0;
  775. }
  776. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  777. {
  778. int i;
  779. int pos = 0;
  780. const char *fmt, *arg;
  781. if (!trace_probe_is_return(tp)) {
  782. fmt = "(%lx)";
  783. arg = "REC->" FIELD_STRING_IP;
  784. } else {
  785. fmt = "(%lx <- %lx)";
  786. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  787. }
  788. /* When len=0, we just calculate the needed length */
  789. #define LEN_OR_ZERO (len ? len - pos : 0)
  790. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  791. for (i = 0; i < tp->nr_args; i++) {
  792. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  793. tp->args[i].name, tp->args[i].type->fmt);
  794. }
  795. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  796. for (i = 0; i < tp->nr_args; i++) {
  797. if (strcmp(tp->args[i].type->name, "string") == 0)
  798. pos += snprintf(buf + pos, LEN_OR_ZERO,
  799. ", __get_str(%s)",
  800. tp->args[i].name);
  801. else
  802. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  803. tp->args[i].name);
  804. }
  805. #undef LEN_OR_ZERO
  806. /* return the length of print_fmt */
  807. return pos;
  808. }
  809. static int set_print_fmt(struct trace_probe *tp)
  810. {
  811. int len;
  812. char *print_fmt;
  813. /* First: called with 0 length to calculate the needed length */
  814. len = __set_print_fmt(tp, NULL, 0);
  815. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  816. if (!print_fmt)
  817. return -ENOMEM;
  818. /* Second: actually write the @print_fmt */
  819. __set_print_fmt(tp, print_fmt, len + 1);
  820. tp->call.print_fmt = print_fmt;
  821. return 0;
  822. }
  823. #ifdef CONFIG_PERF_EVENTS
  824. /* Kprobe profile handler */
  825. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  826. struct pt_regs *regs)
  827. {
  828. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  829. struct ftrace_event_call *call = &tp->call;
  830. struct kprobe_trace_entry_head *entry;
  831. struct hlist_head *head;
  832. int size, __size, dsize;
  833. int rctx;
  834. dsize = __get_data_size(tp, regs);
  835. __size = sizeof(*entry) + tp->size + dsize;
  836. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  837. size -= sizeof(u32);
  838. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  839. "profile buffer not large enough"))
  840. return;
  841. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  842. if (!entry)
  843. return;
  844. entry->ip = (unsigned long)kp->addr;
  845. memset(&entry[1], 0, dsize);
  846. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  847. head = this_cpu_ptr(call->perf_events);
  848. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  849. }
  850. /* Kretprobe profile handler */
  851. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  852. struct pt_regs *regs)
  853. {
  854. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  855. struct ftrace_event_call *call = &tp->call;
  856. struct kretprobe_trace_entry_head *entry;
  857. struct hlist_head *head;
  858. int size, __size, dsize;
  859. int rctx;
  860. dsize = __get_data_size(tp, regs);
  861. __size = sizeof(*entry) + tp->size + dsize;
  862. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  863. size -= sizeof(u32);
  864. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  865. "profile buffer not large enough"))
  866. return;
  867. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  868. if (!entry)
  869. return;
  870. entry->func = (unsigned long)tp->rp.kp.addr;
  871. entry->ret_ip = (unsigned long)ri->ret_addr;
  872. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  873. head = this_cpu_ptr(call->perf_events);
  874. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  875. }
  876. #endif /* CONFIG_PERF_EVENTS */
  877. static __kprobes
  878. int kprobe_register(struct ftrace_event_call *event,
  879. enum trace_reg type, void *data)
  880. {
  881. struct trace_probe *tp = (struct trace_probe *)event->data;
  882. switch (type) {
  883. case TRACE_REG_REGISTER:
  884. return enable_trace_probe(tp, TP_FLAG_TRACE);
  885. case TRACE_REG_UNREGISTER:
  886. disable_trace_probe(tp, TP_FLAG_TRACE);
  887. return 0;
  888. #ifdef CONFIG_PERF_EVENTS
  889. case TRACE_REG_PERF_REGISTER:
  890. return enable_trace_probe(tp, TP_FLAG_PROFILE);
  891. case TRACE_REG_PERF_UNREGISTER:
  892. disable_trace_probe(tp, TP_FLAG_PROFILE);
  893. return 0;
  894. case TRACE_REG_PERF_OPEN:
  895. case TRACE_REG_PERF_CLOSE:
  896. case TRACE_REG_PERF_ADD:
  897. case TRACE_REG_PERF_DEL:
  898. return 0;
  899. #endif
  900. }
  901. return 0;
  902. }
  903. static __kprobes
  904. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  905. {
  906. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  907. if (tp->flags & TP_FLAG_TRACE)
  908. kprobe_trace_func(kp, regs);
  909. #ifdef CONFIG_PERF_EVENTS
  910. if (tp->flags & TP_FLAG_PROFILE)
  911. kprobe_perf_func(kp, regs);
  912. #endif
  913. return 0; /* We don't tweek kernel, so just return 0 */
  914. }
  915. static __kprobes
  916. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  917. {
  918. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  919. if (tp->flags & TP_FLAG_TRACE)
  920. kretprobe_trace_func(ri, regs);
  921. #ifdef CONFIG_PERF_EVENTS
  922. if (tp->flags & TP_FLAG_PROFILE)
  923. kretprobe_perf_func(ri, regs);
  924. #endif
  925. return 0; /* We don't tweek kernel, so just return 0 */
  926. }
  927. static struct trace_event_functions kretprobe_funcs = {
  928. .trace = print_kretprobe_event
  929. };
  930. static struct trace_event_functions kprobe_funcs = {
  931. .trace = print_kprobe_event
  932. };
  933. static int register_probe_event(struct trace_probe *tp)
  934. {
  935. struct ftrace_event_call *call = &tp->call;
  936. int ret;
  937. /* Initialize ftrace_event_call */
  938. INIT_LIST_HEAD(&call->class->fields);
  939. if (trace_probe_is_return(tp)) {
  940. call->event.funcs = &kretprobe_funcs;
  941. call->class->define_fields = kretprobe_event_define_fields;
  942. } else {
  943. call->event.funcs = &kprobe_funcs;
  944. call->class->define_fields = kprobe_event_define_fields;
  945. }
  946. if (set_print_fmt(tp) < 0)
  947. return -ENOMEM;
  948. ret = register_ftrace_event(&call->event);
  949. if (!ret) {
  950. kfree(call->print_fmt);
  951. return -ENODEV;
  952. }
  953. call->flags = 0;
  954. call->class->reg = kprobe_register;
  955. call->data = tp;
  956. ret = trace_add_event_call(call);
  957. if (ret) {
  958. pr_info("Failed to register kprobe event: %s\n", call->name);
  959. kfree(call->print_fmt);
  960. unregister_ftrace_event(&call->event);
  961. }
  962. return ret;
  963. }
  964. static void unregister_probe_event(struct trace_probe *tp)
  965. {
  966. /* tp->event is unregistered in trace_remove_event_call() */
  967. trace_remove_event_call(&tp->call);
  968. kfree(tp->call.print_fmt);
  969. }
  970. /* Make a debugfs interface for controlling probe points */
  971. static __init int init_kprobe_trace(void)
  972. {
  973. struct dentry *d_tracer;
  974. struct dentry *entry;
  975. if (register_module_notifier(&trace_probe_module_nb))
  976. return -EINVAL;
  977. d_tracer = tracing_init_dentry();
  978. if (!d_tracer)
  979. return 0;
  980. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  981. NULL, &kprobe_events_ops);
  982. /* Event list interface */
  983. if (!entry)
  984. pr_warning("Could not create debugfs "
  985. "'kprobe_events' entry\n");
  986. /* Profile interface */
  987. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  988. NULL, &kprobe_profile_ops);
  989. if (!entry)
  990. pr_warning("Could not create debugfs "
  991. "'kprobe_profile' entry\n");
  992. return 0;
  993. }
  994. fs_initcall(init_kprobe_trace);
  995. #ifdef CONFIG_FTRACE_STARTUP_TEST
  996. /*
  997. * The "__used" keeps gcc from removing the function symbol
  998. * from the kallsyms table.
  999. */
  1000. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1001. int a4, int a5, int a6)
  1002. {
  1003. return a1 + a2 + a3 + a4 + a5 + a6;
  1004. }
  1005. static __init int kprobe_trace_self_tests_init(void)
  1006. {
  1007. int ret, warn = 0;
  1008. int (*target)(int, int, int, int, int, int);
  1009. struct trace_probe *tp;
  1010. target = kprobe_trace_selftest_target;
  1011. pr_info("Testing kprobe tracing: ");
  1012. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1013. "$stack $stack0 +0($stack)",
  1014. create_trace_probe);
  1015. if (WARN_ON_ONCE(ret)) {
  1016. pr_warning("error on probing function entry.\n");
  1017. warn++;
  1018. } else {
  1019. /* Enable trace point */
  1020. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1021. if (WARN_ON_ONCE(tp == NULL)) {
  1022. pr_warning("error on getting new probe.\n");
  1023. warn++;
  1024. } else
  1025. enable_trace_probe(tp, TP_FLAG_TRACE);
  1026. }
  1027. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1028. "$retval", create_trace_probe);
  1029. if (WARN_ON_ONCE(ret)) {
  1030. pr_warning("error on probing function return.\n");
  1031. warn++;
  1032. } else {
  1033. /* Enable trace point */
  1034. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1035. if (WARN_ON_ONCE(tp == NULL)) {
  1036. pr_warning("error on getting new probe.\n");
  1037. warn++;
  1038. } else
  1039. enable_trace_probe(tp, TP_FLAG_TRACE);
  1040. }
  1041. if (warn)
  1042. goto end;
  1043. ret = target(1, 2, 3, 4, 5, 6);
  1044. /* Disable trace points before removing it */
  1045. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1046. if (WARN_ON_ONCE(tp == NULL)) {
  1047. pr_warning("error on getting test probe.\n");
  1048. warn++;
  1049. } else
  1050. disable_trace_probe(tp, TP_FLAG_TRACE);
  1051. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1052. if (WARN_ON_ONCE(tp == NULL)) {
  1053. pr_warning("error on getting 2nd test probe.\n");
  1054. warn++;
  1055. } else
  1056. disable_trace_probe(tp, TP_FLAG_TRACE);
  1057. ret = traceprobe_command("-:testprobe", create_trace_probe);
  1058. if (WARN_ON_ONCE(ret)) {
  1059. pr_warning("error on deleting a probe.\n");
  1060. warn++;
  1061. }
  1062. ret = traceprobe_command("-:testprobe2", create_trace_probe);
  1063. if (WARN_ON_ONCE(ret)) {
  1064. pr_warning("error on deleting a probe.\n");
  1065. warn++;
  1066. }
  1067. end:
  1068. release_all_trace_probes();
  1069. if (warn)
  1070. pr_cont("NG: Some tests are failed. Please check them.\n");
  1071. else
  1072. pr_cont("OK\n");
  1073. return 0;
  1074. }
  1075. late_initcall(kprobe_trace_self_tests_init);
  1076. #endif