trace_kprobe.c 36 KB

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