trace_kprobe.c 35 KB

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