trace_kprobe.c 36 KB

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