trace_kprobe.c 34 KB

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