trace_kprobe.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /*
  2. * kprobe based kernel tracer
  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 <linux/kprobes.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/perf_event.h>
  31. #include "trace.h"
  32. #include "trace_output.h"
  33. #define MAX_TRACE_ARGS 128
  34. #define MAX_ARGSTR_LEN 63
  35. #define MAX_EVENT_NAME_LEN 64
  36. #define KPROBE_EVENT_SYSTEM "kprobes"
  37. /* currently, trace_kprobe only supports X86. */
  38. struct fetch_func {
  39. unsigned long (*func)(struct pt_regs *, void *);
  40. void *data;
  41. };
  42. static __kprobes unsigned long call_fetch(struct fetch_func *f,
  43. struct pt_regs *regs)
  44. {
  45. return f->func(regs, f->data);
  46. }
  47. /* fetch handlers */
  48. static __kprobes unsigned long fetch_register(struct pt_regs *regs,
  49. void *offset)
  50. {
  51. return regs_get_register(regs, (unsigned int)((unsigned long)offset));
  52. }
  53. static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
  54. void *num)
  55. {
  56. return regs_get_kernel_stack_nth(regs,
  57. (unsigned int)((unsigned long)num));
  58. }
  59. static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
  60. {
  61. unsigned long retval;
  62. if (probe_kernel_address(addr, retval))
  63. return 0;
  64. return retval;
  65. }
  66. static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
  67. {
  68. return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
  69. }
  70. static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
  71. void *dummy)
  72. {
  73. return regs_return_value(regs);
  74. }
  75. static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
  76. void *dummy)
  77. {
  78. return kernel_stack_pointer(regs);
  79. }
  80. /* Memory fetching by symbol */
  81. struct symbol_cache {
  82. char *symbol;
  83. long offset;
  84. unsigned long addr;
  85. };
  86. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  87. {
  88. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  89. if (sc->addr)
  90. sc->addr += sc->offset;
  91. return sc->addr;
  92. }
  93. static void free_symbol_cache(struct symbol_cache *sc)
  94. {
  95. kfree(sc->symbol);
  96. kfree(sc);
  97. }
  98. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  99. {
  100. struct symbol_cache *sc;
  101. if (!sym || strlen(sym) == 0)
  102. return NULL;
  103. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  104. if (!sc)
  105. return NULL;
  106. sc->symbol = kstrdup(sym, GFP_KERNEL);
  107. if (!sc->symbol) {
  108. kfree(sc);
  109. return NULL;
  110. }
  111. sc->offset = offset;
  112. update_symbol_cache(sc);
  113. return sc;
  114. }
  115. static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
  116. {
  117. struct symbol_cache *sc = data;
  118. if (sc->addr)
  119. return fetch_memory(regs, (void *)sc->addr);
  120. else
  121. return 0;
  122. }
  123. /* Special indirect memory access interface */
  124. struct indirect_fetch_data {
  125. struct fetch_func orig;
  126. long offset;
  127. };
  128. static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
  129. {
  130. struct indirect_fetch_data *ind = data;
  131. unsigned long addr;
  132. addr = call_fetch(&ind->orig, regs);
  133. if (addr) {
  134. addr += ind->offset;
  135. return fetch_memory(regs, (void *)addr);
  136. } else
  137. return 0;
  138. }
  139. static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
  140. {
  141. if (data->orig.func == fetch_indirect)
  142. free_indirect_fetch_data(data->orig.data);
  143. else if (data->orig.func == fetch_symbol)
  144. free_symbol_cache(data->orig.data);
  145. kfree(data);
  146. }
  147. /**
  148. * Kprobe tracer core functions
  149. */
  150. struct probe_arg {
  151. struct fetch_func fetch;
  152. const char *name;
  153. };
  154. /* Flags for trace_probe */
  155. #define TP_FLAG_TRACE 1
  156. #define TP_FLAG_PROFILE 2
  157. struct trace_probe {
  158. struct list_head list;
  159. struct kretprobe rp; /* Use rp.kp for kprobe use */
  160. unsigned long nhit;
  161. unsigned int flags; /* For TP_FLAG_* */
  162. const char *symbol; /* symbol name */
  163. struct ftrace_event_call call;
  164. struct trace_event event;
  165. unsigned int nr_args;
  166. struct probe_arg args[];
  167. };
  168. #define SIZEOF_TRACE_PROBE(n) \
  169. (offsetof(struct trace_probe, args) + \
  170. (sizeof(struct probe_arg) * (n)))
  171. static __kprobes int probe_is_return(struct trace_probe *tp)
  172. {
  173. return tp->rp.handler != NULL;
  174. }
  175. static __kprobes const char *probe_symbol(struct trace_probe *tp)
  176. {
  177. return tp->symbol ? tp->symbol : "unknown";
  178. }
  179. static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
  180. {
  181. int ret = -EINVAL;
  182. if (ff->func == fetch_argument)
  183. ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
  184. else if (ff->func == fetch_register) {
  185. const char *name;
  186. name = regs_query_register_name((unsigned int)((long)ff->data));
  187. ret = snprintf(buf, n, "%%%s", name);
  188. } else if (ff->func == fetch_stack)
  189. ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
  190. else if (ff->func == fetch_memory)
  191. ret = snprintf(buf, n, "@0x%p", ff->data);
  192. else if (ff->func == fetch_symbol) {
  193. struct symbol_cache *sc = ff->data;
  194. ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
  195. } else if (ff->func == fetch_retvalue)
  196. ret = snprintf(buf, n, "$retval");
  197. else if (ff->func == fetch_stack_address)
  198. ret = snprintf(buf, n, "$stack");
  199. else if (ff->func == fetch_indirect) {
  200. struct indirect_fetch_data *id = ff->data;
  201. size_t l = 0;
  202. ret = snprintf(buf, n, "%+ld(", id->offset);
  203. if (ret >= n)
  204. goto end;
  205. l += ret;
  206. ret = probe_arg_string(buf + l, n - l, &id->orig);
  207. if (ret < 0)
  208. goto end;
  209. l += ret;
  210. ret = snprintf(buf + l, n - l, ")");
  211. ret += l;
  212. }
  213. end:
  214. if (ret >= n)
  215. return -ENOSPC;
  216. return ret;
  217. }
  218. static int register_probe_event(struct trace_probe *tp);
  219. static void unregister_probe_event(struct trace_probe *tp);
  220. static DEFINE_MUTEX(probe_lock);
  221. static LIST_HEAD(probe_list);
  222. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  223. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  224. struct pt_regs *regs);
  225. /*
  226. * Allocate new trace_probe and initialize it (including kprobes).
  227. */
  228. static struct trace_probe *alloc_trace_probe(const char *group,
  229. const char *event,
  230. void *addr,
  231. const char *symbol,
  232. unsigned long offs,
  233. int nargs, int is_return)
  234. {
  235. struct trace_probe *tp;
  236. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  237. if (!tp)
  238. return ERR_PTR(-ENOMEM);
  239. if (symbol) {
  240. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  241. if (!tp->symbol)
  242. goto error;
  243. tp->rp.kp.symbol_name = tp->symbol;
  244. tp->rp.kp.offset = offs;
  245. } else
  246. tp->rp.kp.addr = addr;
  247. if (is_return)
  248. tp->rp.handler = kretprobe_dispatcher;
  249. else
  250. tp->rp.kp.pre_handler = kprobe_dispatcher;
  251. if (!event)
  252. goto error;
  253. tp->call.name = kstrdup(event, GFP_KERNEL);
  254. if (!tp->call.name)
  255. goto error;
  256. if (!group)
  257. goto error;
  258. tp->call.system = kstrdup(group, GFP_KERNEL);
  259. if (!tp->call.system)
  260. goto error;
  261. INIT_LIST_HEAD(&tp->list);
  262. return tp;
  263. error:
  264. kfree(tp->call.name);
  265. kfree(tp->symbol);
  266. kfree(tp);
  267. return ERR_PTR(-ENOMEM);
  268. }
  269. static void free_probe_arg(struct probe_arg *arg)
  270. {
  271. if (arg->fetch.func == fetch_symbol)
  272. free_symbol_cache(arg->fetch.data);
  273. else if (arg->fetch.func == fetch_indirect)
  274. free_indirect_fetch_data(arg->fetch.data);
  275. kfree(arg->name);
  276. }
  277. static void free_trace_probe(struct trace_probe *tp)
  278. {
  279. int i;
  280. for (i = 0; i < tp->nr_args; i++)
  281. free_probe_arg(&tp->args[i]);
  282. kfree(tp->call.system);
  283. kfree(tp->call.name);
  284. kfree(tp->symbol);
  285. kfree(tp);
  286. }
  287. static struct trace_probe *find_probe_event(const char *event)
  288. {
  289. struct trace_probe *tp;
  290. list_for_each_entry(tp, &probe_list, list)
  291. if (!strcmp(tp->call.name, event))
  292. return tp;
  293. return NULL;
  294. }
  295. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  296. static void unregister_trace_probe(struct trace_probe *tp)
  297. {
  298. if (probe_is_return(tp))
  299. unregister_kretprobe(&tp->rp);
  300. else
  301. unregister_kprobe(&tp->rp.kp);
  302. list_del(&tp->list);
  303. unregister_probe_event(tp);
  304. }
  305. /* Register a trace_probe and probe_event */
  306. static int register_trace_probe(struct trace_probe *tp)
  307. {
  308. struct trace_probe *old_tp;
  309. int ret;
  310. mutex_lock(&probe_lock);
  311. /* register as an event */
  312. old_tp = find_probe_event(tp->call.name);
  313. if (old_tp) {
  314. /* delete old event */
  315. unregister_trace_probe(old_tp);
  316. free_trace_probe(old_tp);
  317. }
  318. ret = register_probe_event(tp);
  319. if (ret) {
  320. pr_warning("Faild to register probe event(%d)\n", ret);
  321. goto end;
  322. }
  323. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  324. if (probe_is_return(tp))
  325. ret = register_kretprobe(&tp->rp);
  326. else
  327. ret = register_kprobe(&tp->rp.kp);
  328. if (ret) {
  329. pr_warning("Could not insert probe(%d)\n", ret);
  330. if (ret == -EILSEQ) {
  331. pr_warning("Probing address(0x%p) is not an "
  332. "instruction boundary.\n",
  333. tp->rp.kp.addr);
  334. ret = -EINVAL;
  335. }
  336. unregister_probe_event(tp);
  337. } else
  338. list_add_tail(&tp->list, &probe_list);
  339. end:
  340. mutex_unlock(&probe_lock);
  341. return ret;
  342. }
  343. /* Split symbol and offset. */
  344. static int split_symbol_offset(char *symbol, unsigned long *offset)
  345. {
  346. char *tmp;
  347. int ret;
  348. if (!offset)
  349. return -EINVAL;
  350. tmp = strchr(symbol, '+');
  351. if (tmp) {
  352. /* skip sign because strict_strtol doesn't accept '+' */
  353. ret = strict_strtoul(tmp + 1, 0, offset);
  354. if (ret)
  355. return ret;
  356. *tmp = '\0';
  357. } else
  358. *offset = 0;
  359. return 0;
  360. }
  361. #define PARAM_MAX_ARGS 16
  362. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  363. static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
  364. {
  365. int ret = 0;
  366. unsigned long param;
  367. if (strcmp(arg, "retval") == 0) {
  368. if (is_return) {
  369. ff->func = fetch_retvalue;
  370. ff->data = NULL;
  371. } else
  372. ret = -EINVAL;
  373. } else if (strncmp(arg, "stack", 5) == 0) {
  374. if (arg[5] == '\0') {
  375. ff->func = fetch_stack_address;
  376. ff->data = NULL;
  377. } else if (isdigit(arg[5])) {
  378. ret = strict_strtoul(arg + 5, 10, &param);
  379. if (ret || param > PARAM_MAX_STACK)
  380. ret = -EINVAL;
  381. else {
  382. ff->func = fetch_stack;
  383. ff->data = (void *)param;
  384. }
  385. } else
  386. ret = -EINVAL;
  387. } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
  388. ret = strict_strtoul(arg + 3, 10, &param);
  389. if (ret || param > PARAM_MAX_ARGS)
  390. ret = -EINVAL;
  391. else {
  392. ff->func = fetch_argument;
  393. ff->data = (void *)param;
  394. }
  395. } else
  396. ret = -EINVAL;
  397. return ret;
  398. }
  399. static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  400. {
  401. int ret = 0;
  402. unsigned long param;
  403. long offset;
  404. char *tmp;
  405. switch (arg[0]) {
  406. case '$':
  407. ret = parse_probe_vars(arg + 1, ff, is_return);
  408. break;
  409. case '%': /* named register */
  410. ret = regs_query_register_offset(arg + 1);
  411. if (ret >= 0) {
  412. ff->func = fetch_register;
  413. ff->data = (void *)(unsigned long)ret;
  414. ret = 0;
  415. }
  416. break;
  417. case '@': /* memory or symbol */
  418. if (isdigit(arg[1])) {
  419. ret = strict_strtoul(arg + 1, 0, &param);
  420. if (ret)
  421. break;
  422. ff->func = fetch_memory;
  423. ff->data = (void *)param;
  424. } else {
  425. ret = split_symbol_offset(arg + 1, &offset);
  426. if (ret)
  427. break;
  428. ff->data = alloc_symbol_cache(arg + 1, offset);
  429. if (ff->data)
  430. ff->func = fetch_symbol;
  431. else
  432. ret = -EINVAL;
  433. }
  434. break;
  435. case '+': /* indirect memory */
  436. case '-':
  437. tmp = strchr(arg, '(');
  438. if (!tmp) {
  439. ret = -EINVAL;
  440. break;
  441. }
  442. *tmp = '\0';
  443. ret = strict_strtol(arg + 1, 0, &offset);
  444. if (ret)
  445. break;
  446. if (arg[0] == '-')
  447. offset = -offset;
  448. arg = tmp + 1;
  449. tmp = strrchr(arg, ')');
  450. if (tmp) {
  451. struct indirect_fetch_data *id;
  452. *tmp = '\0';
  453. id = kzalloc(sizeof(struct indirect_fetch_data),
  454. GFP_KERNEL);
  455. if (!id)
  456. return -ENOMEM;
  457. id->offset = offset;
  458. ret = parse_probe_arg(arg, &id->orig, is_return);
  459. if (ret)
  460. kfree(id);
  461. else {
  462. ff->func = fetch_indirect;
  463. ff->data = (void *)id;
  464. }
  465. } else
  466. ret = -EINVAL;
  467. break;
  468. default:
  469. /* TODO: support custom handler */
  470. ret = -EINVAL;
  471. }
  472. return ret;
  473. }
  474. static int create_trace_probe(int argc, char **argv)
  475. {
  476. /*
  477. * Argument syntax:
  478. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  479. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  480. * Fetch args:
  481. * $argN : fetch Nth of function argument. (N:0-)
  482. * $retval : fetch return value
  483. * $stack : fetch stack address
  484. * $stackN : fetch Nth of stack (N:0-)
  485. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  486. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  487. * %REG : fetch register REG
  488. * Indirect memory fetch:
  489. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  490. * Alias name of args:
  491. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  492. */
  493. struct trace_probe *tp;
  494. int i, ret = 0;
  495. int is_return = 0;
  496. char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
  497. unsigned long offset = 0;
  498. void *addr = NULL;
  499. char buf[MAX_EVENT_NAME_LEN];
  500. if (argc < 2)
  501. return -EINVAL;
  502. if (argv[0][0] == 'p')
  503. is_return = 0;
  504. else if (argv[0][0] == 'r')
  505. is_return = 1;
  506. else
  507. return -EINVAL;
  508. if (argv[0][1] == ':') {
  509. event = &argv[0][2];
  510. if (strchr(event, '/')) {
  511. group = event;
  512. event = strchr(group, '/') + 1;
  513. event[-1] = '\0';
  514. if (strlen(group) == 0) {
  515. pr_info("Group name is not specifiled\n");
  516. return -EINVAL;
  517. }
  518. }
  519. if (strlen(event) == 0) {
  520. pr_info("Event name is not specifiled\n");
  521. return -EINVAL;
  522. }
  523. }
  524. if (isdigit(argv[1][0])) {
  525. if (is_return)
  526. return -EINVAL;
  527. /* an address specified */
  528. ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
  529. if (ret)
  530. return ret;
  531. } else {
  532. /* a symbol specified */
  533. symbol = argv[1];
  534. /* TODO: support .init module functions */
  535. ret = split_symbol_offset(symbol, &offset);
  536. if (ret)
  537. return ret;
  538. if (offset && is_return)
  539. return -EINVAL;
  540. }
  541. argc -= 2; argv += 2;
  542. /* setup a probe */
  543. if (!group)
  544. group = KPROBE_EVENT_SYSTEM;
  545. if (!event) {
  546. /* Make a new event name */
  547. if (symbol)
  548. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
  549. is_return ? 'r' : 'p', symbol, offset);
  550. else
  551. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
  552. is_return ? 'r' : 'p', addr);
  553. event = buf;
  554. }
  555. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  556. is_return);
  557. if (IS_ERR(tp))
  558. return PTR_ERR(tp);
  559. /* parse arguments */
  560. ret = 0;
  561. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  562. /* Parse argument name */
  563. arg = strchr(argv[i], '=');
  564. if (arg)
  565. *arg++ = '\0';
  566. else
  567. arg = argv[i];
  568. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  569. /* Parse fetch argument */
  570. if (strlen(arg) > MAX_ARGSTR_LEN) {
  571. pr_info("Argument%d(%s) is too long.\n", i, arg);
  572. ret = -ENOSPC;
  573. goto error;
  574. }
  575. ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
  576. if (ret)
  577. goto error;
  578. }
  579. tp->nr_args = i;
  580. ret = register_trace_probe(tp);
  581. if (ret)
  582. goto error;
  583. return 0;
  584. error:
  585. free_trace_probe(tp);
  586. return ret;
  587. }
  588. static void cleanup_all_probes(void)
  589. {
  590. struct trace_probe *tp;
  591. mutex_lock(&probe_lock);
  592. /* TODO: Use batch unregistration */
  593. while (!list_empty(&probe_list)) {
  594. tp = list_entry(probe_list.next, struct trace_probe, list);
  595. unregister_trace_probe(tp);
  596. free_trace_probe(tp);
  597. }
  598. mutex_unlock(&probe_lock);
  599. }
  600. /* Probes listing interfaces */
  601. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  602. {
  603. mutex_lock(&probe_lock);
  604. return seq_list_start(&probe_list, *pos);
  605. }
  606. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  607. {
  608. return seq_list_next(v, &probe_list, pos);
  609. }
  610. static void probes_seq_stop(struct seq_file *m, void *v)
  611. {
  612. mutex_unlock(&probe_lock);
  613. }
  614. static int probes_seq_show(struct seq_file *m, void *v)
  615. {
  616. struct trace_probe *tp = v;
  617. int i, ret;
  618. char buf[MAX_ARGSTR_LEN + 1];
  619. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  620. seq_printf(m, ":%s", tp->call.name);
  621. if (tp->symbol)
  622. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  623. else
  624. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  625. for (i = 0; i < tp->nr_args; i++) {
  626. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
  627. if (ret < 0) {
  628. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  629. return ret;
  630. }
  631. seq_printf(m, " %s=%s", tp->args[i].name, buf);
  632. }
  633. seq_printf(m, "\n");
  634. return 0;
  635. }
  636. static const struct seq_operations probes_seq_op = {
  637. .start = probes_seq_start,
  638. .next = probes_seq_next,
  639. .stop = probes_seq_stop,
  640. .show = probes_seq_show
  641. };
  642. static int probes_open(struct inode *inode, struct file *file)
  643. {
  644. if ((file->f_mode & FMODE_WRITE) &&
  645. (file->f_flags & O_TRUNC))
  646. cleanup_all_probes();
  647. return seq_open(file, &probes_seq_op);
  648. }
  649. static int command_trace_probe(const char *buf)
  650. {
  651. char **argv;
  652. int argc = 0, ret = 0;
  653. argv = argv_split(GFP_KERNEL, buf, &argc);
  654. if (!argv)
  655. return -ENOMEM;
  656. if (argc)
  657. ret = create_trace_probe(argc, argv);
  658. argv_free(argv);
  659. return ret;
  660. }
  661. #define WRITE_BUFSIZE 128
  662. static ssize_t probes_write(struct file *file, const char __user *buffer,
  663. size_t count, loff_t *ppos)
  664. {
  665. char *kbuf, *tmp;
  666. int ret;
  667. size_t done;
  668. size_t size;
  669. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  670. if (!kbuf)
  671. return -ENOMEM;
  672. ret = done = 0;
  673. while (done < count) {
  674. size = count - done;
  675. if (size >= WRITE_BUFSIZE)
  676. size = WRITE_BUFSIZE - 1;
  677. if (copy_from_user(kbuf, buffer + done, size)) {
  678. ret = -EFAULT;
  679. goto out;
  680. }
  681. kbuf[size] = '\0';
  682. tmp = strchr(kbuf, '\n');
  683. if (tmp) {
  684. *tmp = '\0';
  685. size = tmp - kbuf + 1;
  686. } else if (done + size < count) {
  687. pr_warning("Line length is too long: "
  688. "Should be less than %d.", WRITE_BUFSIZE);
  689. ret = -EINVAL;
  690. goto out;
  691. }
  692. done += size;
  693. /* Remove comments */
  694. tmp = strchr(kbuf, '#');
  695. if (tmp)
  696. *tmp = '\0';
  697. ret = command_trace_probe(kbuf);
  698. if (ret)
  699. goto out;
  700. }
  701. ret = done;
  702. out:
  703. kfree(kbuf);
  704. return ret;
  705. }
  706. static const struct file_operations kprobe_events_ops = {
  707. .owner = THIS_MODULE,
  708. .open = probes_open,
  709. .read = seq_read,
  710. .llseek = seq_lseek,
  711. .release = seq_release,
  712. .write = probes_write,
  713. };
  714. /* Probes profiling interfaces */
  715. static int probes_profile_seq_show(struct seq_file *m, void *v)
  716. {
  717. struct trace_probe *tp = v;
  718. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  719. tp->rp.kp.nmissed);
  720. return 0;
  721. }
  722. static const struct seq_operations profile_seq_op = {
  723. .start = probes_seq_start,
  724. .next = probes_seq_next,
  725. .stop = probes_seq_stop,
  726. .show = probes_profile_seq_show
  727. };
  728. static int profile_open(struct inode *inode, struct file *file)
  729. {
  730. return seq_open(file, &profile_seq_op);
  731. }
  732. static const struct file_operations kprobe_profile_ops = {
  733. .owner = THIS_MODULE,
  734. .open = profile_open,
  735. .read = seq_read,
  736. .llseek = seq_lseek,
  737. .release = seq_release,
  738. };
  739. /* Kprobe handler */
  740. static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  741. {
  742. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  743. struct kprobe_trace_entry *entry;
  744. struct ring_buffer_event *event;
  745. struct ring_buffer *buffer;
  746. int size, i, pc;
  747. unsigned long irq_flags;
  748. struct ftrace_event_call *call = &tp->call;
  749. tp->nhit++;
  750. local_save_flags(irq_flags);
  751. pc = preempt_count();
  752. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  753. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  754. irq_flags, pc);
  755. if (!event)
  756. return 0;
  757. entry = ring_buffer_event_data(event);
  758. entry->nargs = tp->nr_args;
  759. entry->ip = (unsigned long)kp->addr;
  760. for (i = 0; i < tp->nr_args; i++)
  761. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  762. if (!filter_current_check_discard(buffer, call, entry, event))
  763. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  764. return 0;
  765. }
  766. /* Kretprobe handler */
  767. static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
  768. struct pt_regs *regs)
  769. {
  770. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  771. struct kretprobe_trace_entry *entry;
  772. struct ring_buffer_event *event;
  773. struct ring_buffer *buffer;
  774. int size, i, pc;
  775. unsigned long irq_flags;
  776. struct ftrace_event_call *call = &tp->call;
  777. local_save_flags(irq_flags);
  778. pc = preempt_count();
  779. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  780. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  781. irq_flags, pc);
  782. if (!event)
  783. return 0;
  784. entry = ring_buffer_event_data(event);
  785. entry->nargs = tp->nr_args;
  786. entry->func = (unsigned long)tp->rp.kp.addr;
  787. entry->ret_ip = (unsigned long)ri->ret_addr;
  788. for (i = 0; i < tp->nr_args; i++)
  789. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  790. if (!filter_current_check_discard(buffer, call, entry, event))
  791. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  792. return 0;
  793. }
  794. /* Event entry printers */
  795. enum print_line_t
  796. print_kprobe_event(struct trace_iterator *iter, int flags)
  797. {
  798. struct kprobe_trace_entry *field;
  799. struct trace_seq *s = &iter->seq;
  800. struct trace_event *event;
  801. struct trace_probe *tp;
  802. int i;
  803. field = (struct kprobe_trace_entry *)iter->ent;
  804. event = ftrace_find_event(field->ent.type);
  805. tp = container_of(event, struct trace_probe, event);
  806. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  807. goto partial;
  808. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  809. goto partial;
  810. if (!trace_seq_puts(s, ")"))
  811. goto partial;
  812. for (i = 0; i < field->nargs; i++)
  813. if (!trace_seq_printf(s, " %s=%lx",
  814. tp->args[i].name, field->args[i]))
  815. goto partial;
  816. if (!trace_seq_puts(s, "\n"))
  817. goto partial;
  818. return TRACE_TYPE_HANDLED;
  819. partial:
  820. return TRACE_TYPE_PARTIAL_LINE;
  821. }
  822. enum print_line_t
  823. print_kretprobe_event(struct trace_iterator *iter, int flags)
  824. {
  825. struct kretprobe_trace_entry *field;
  826. struct trace_seq *s = &iter->seq;
  827. struct trace_event *event;
  828. struct trace_probe *tp;
  829. int i;
  830. field = (struct kretprobe_trace_entry *)iter->ent;
  831. event = ftrace_find_event(field->ent.type);
  832. tp = container_of(event, struct trace_probe, event);
  833. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  834. goto partial;
  835. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  836. goto partial;
  837. if (!trace_seq_puts(s, " <- "))
  838. goto partial;
  839. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  840. goto partial;
  841. if (!trace_seq_puts(s, ")"))
  842. goto partial;
  843. for (i = 0; i < field->nargs; i++)
  844. if (!trace_seq_printf(s, " %s=%lx",
  845. tp->args[i].name, field->args[i]))
  846. goto partial;
  847. if (!trace_seq_puts(s, "\n"))
  848. goto partial;
  849. return TRACE_TYPE_HANDLED;
  850. partial:
  851. return TRACE_TYPE_PARTIAL_LINE;
  852. }
  853. static int probe_event_enable(struct ftrace_event_call *call)
  854. {
  855. struct trace_probe *tp = (struct trace_probe *)call->data;
  856. tp->flags |= TP_FLAG_TRACE;
  857. if (probe_is_return(tp))
  858. return enable_kretprobe(&tp->rp);
  859. else
  860. return enable_kprobe(&tp->rp.kp);
  861. }
  862. static void probe_event_disable(struct ftrace_event_call *call)
  863. {
  864. struct trace_probe *tp = (struct trace_probe *)call->data;
  865. tp->flags &= ~TP_FLAG_TRACE;
  866. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  867. if (probe_is_return(tp))
  868. disable_kretprobe(&tp->rp);
  869. else
  870. disable_kprobe(&tp->rp.kp);
  871. }
  872. }
  873. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  874. {
  875. INIT_LIST_HEAD(&event_call->fields);
  876. return 0;
  877. }
  878. #undef DEFINE_FIELD
  879. #define DEFINE_FIELD(type, item, name, is_signed) \
  880. do { \
  881. ret = trace_define_field(event_call, #type, name, \
  882. offsetof(typeof(field), item), \
  883. sizeof(field.item), is_signed, \
  884. FILTER_OTHER); \
  885. if (ret) \
  886. return ret; \
  887. } while (0)
  888. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  889. {
  890. int ret, i;
  891. struct kprobe_trace_entry field;
  892. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  893. ret = trace_define_common_fields(event_call);
  894. if (!ret)
  895. return ret;
  896. DEFINE_FIELD(unsigned long, ip, "ip", 0);
  897. DEFINE_FIELD(int, nargs, "nargs", 1);
  898. /* Set argument names as fields */
  899. for (i = 0; i < tp->nr_args; i++)
  900. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  901. return 0;
  902. }
  903. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  904. {
  905. int ret, i;
  906. struct kretprobe_trace_entry field;
  907. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  908. ret = trace_define_common_fields(event_call);
  909. if (!ret)
  910. return ret;
  911. DEFINE_FIELD(unsigned long, func, "func", 0);
  912. DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
  913. DEFINE_FIELD(int, nargs, "nargs", 1);
  914. /* Set argument names as fields */
  915. for (i = 0; i < tp->nr_args; i++)
  916. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  917. return 0;
  918. }
  919. static int __probe_event_show_format(struct trace_seq *s,
  920. struct trace_probe *tp, const char *fmt,
  921. const char *arg)
  922. {
  923. int i;
  924. /* Show format */
  925. if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
  926. return 0;
  927. for (i = 0; i < tp->nr_args; i++)
  928. if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
  929. return 0;
  930. if (!trace_seq_printf(s, "\", %s", arg))
  931. return 0;
  932. for (i = 0; i < tp->nr_args; i++)
  933. if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
  934. return 0;
  935. return trace_seq_puts(s, "\n");
  936. }
  937. #undef SHOW_FIELD
  938. #define SHOW_FIELD(type, item, name) \
  939. do { \
  940. ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
  941. "offset:%u;\tsize:%u;\n", name, \
  942. (unsigned int)offsetof(typeof(field), item),\
  943. (unsigned int)sizeof(type)); \
  944. if (!ret) \
  945. return 0; \
  946. } while (0)
  947. static int kprobe_event_show_format(struct ftrace_event_call *call,
  948. struct trace_seq *s)
  949. {
  950. struct kprobe_trace_entry field __attribute__((unused));
  951. int ret, i;
  952. struct trace_probe *tp = (struct trace_probe *)call->data;
  953. SHOW_FIELD(unsigned long, ip, "ip");
  954. SHOW_FIELD(int, nargs, "nargs");
  955. /* Show fields */
  956. for (i = 0; i < tp->nr_args; i++)
  957. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  958. trace_seq_puts(s, "\n");
  959. return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
  960. }
  961. static int kretprobe_event_show_format(struct ftrace_event_call *call,
  962. struct trace_seq *s)
  963. {
  964. struct kretprobe_trace_entry field __attribute__((unused));
  965. int ret, i;
  966. struct trace_probe *tp = (struct trace_probe *)call->data;
  967. SHOW_FIELD(unsigned long, func, "func");
  968. SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
  969. SHOW_FIELD(int, nargs, "nargs");
  970. /* Show fields */
  971. for (i = 0; i < tp->nr_args; i++)
  972. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  973. trace_seq_puts(s, "\n");
  974. return __probe_event_show_format(s, tp, "(%lx <- %lx)",
  975. "REC->func, REC->ret_ip");
  976. }
  977. #ifdef CONFIG_EVENT_PROFILE
  978. /* Kprobe profile handler */
  979. static __kprobes int kprobe_profile_func(struct kprobe *kp,
  980. struct pt_regs *regs)
  981. {
  982. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  983. struct ftrace_event_call *call = &tp->call;
  984. struct kprobe_trace_entry *entry;
  985. struct trace_entry *ent;
  986. int size, __size, i, pc, __cpu;
  987. unsigned long irq_flags;
  988. char *raw_data;
  989. pc = preempt_count();
  990. __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  991. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  992. size -= sizeof(u32);
  993. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  994. "profile buffer not large enough"))
  995. return 0;
  996. /*
  997. * Protect the non nmi buffer
  998. * This also protects the rcu read side
  999. */
  1000. local_irq_save(irq_flags);
  1001. __cpu = smp_processor_id();
  1002. if (in_nmi())
  1003. raw_data = rcu_dereference(trace_profile_buf_nmi);
  1004. else
  1005. raw_data = rcu_dereference(trace_profile_buf);
  1006. if (!raw_data)
  1007. goto end;
  1008. raw_data = per_cpu_ptr(raw_data, __cpu);
  1009. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1010. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1011. entry = (struct kprobe_trace_entry *)raw_data;
  1012. ent = &entry->ent;
  1013. tracing_generic_entry_update(ent, irq_flags, pc);
  1014. ent->type = call->id;
  1015. entry->nargs = tp->nr_args;
  1016. entry->ip = (unsigned long)kp->addr;
  1017. for (i = 0; i < tp->nr_args; i++)
  1018. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1019. perf_tp_event(call->id, entry->ip, 1, entry, size);
  1020. end:
  1021. local_irq_restore(irq_flags);
  1022. return 0;
  1023. }
  1024. /* Kretprobe profile handler */
  1025. static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
  1026. struct pt_regs *regs)
  1027. {
  1028. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1029. struct ftrace_event_call *call = &tp->call;
  1030. struct kretprobe_trace_entry *entry;
  1031. struct trace_entry *ent;
  1032. int size, __size, i, pc, __cpu;
  1033. unsigned long irq_flags;
  1034. char *raw_data;
  1035. pc = preempt_count();
  1036. __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  1037. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1038. size -= sizeof(u32);
  1039. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1040. "profile buffer not large enough"))
  1041. return 0;
  1042. /*
  1043. * Protect the non nmi buffer
  1044. * This also protects the rcu read side
  1045. */
  1046. local_irq_save(irq_flags);
  1047. __cpu = smp_processor_id();
  1048. if (in_nmi())
  1049. raw_data = rcu_dereference(trace_profile_buf_nmi);
  1050. else
  1051. raw_data = rcu_dereference(trace_profile_buf);
  1052. if (!raw_data)
  1053. goto end;
  1054. raw_data = per_cpu_ptr(raw_data, __cpu);
  1055. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1056. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1057. entry = (struct kretprobe_trace_entry *)raw_data;
  1058. ent = &entry->ent;
  1059. tracing_generic_entry_update(ent, irq_flags, pc);
  1060. ent->type = call->id;
  1061. entry->nargs = tp->nr_args;
  1062. entry->func = (unsigned long)tp->rp.kp.addr;
  1063. entry->ret_ip = (unsigned long)ri->ret_addr;
  1064. for (i = 0; i < tp->nr_args; i++)
  1065. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1066. perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
  1067. end:
  1068. local_irq_restore(irq_flags);
  1069. return 0;
  1070. }
  1071. static int probe_profile_enable(struct ftrace_event_call *call)
  1072. {
  1073. struct trace_probe *tp = (struct trace_probe *)call->data;
  1074. tp->flags |= TP_FLAG_PROFILE;
  1075. if (probe_is_return(tp))
  1076. return enable_kretprobe(&tp->rp);
  1077. else
  1078. return enable_kprobe(&tp->rp.kp);
  1079. }
  1080. static void probe_profile_disable(struct ftrace_event_call *call)
  1081. {
  1082. struct trace_probe *tp = (struct trace_probe *)call->data;
  1083. tp->flags &= ~TP_FLAG_PROFILE;
  1084. if (!(tp->flags & TP_FLAG_TRACE)) {
  1085. if (probe_is_return(tp))
  1086. disable_kretprobe(&tp->rp);
  1087. else
  1088. disable_kprobe(&tp->rp.kp);
  1089. }
  1090. }
  1091. #endif /* CONFIG_EVENT_PROFILE */
  1092. static __kprobes
  1093. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1094. {
  1095. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1096. if (tp->flags & TP_FLAG_TRACE)
  1097. kprobe_trace_func(kp, regs);
  1098. #ifdef CONFIG_EVENT_PROFILE
  1099. if (tp->flags & TP_FLAG_PROFILE)
  1100. kprobe_profile_func(kp, regs);
  1101. #endif /* CONFIG_EVENT_PROFILE */
  1102. return 0; /* We don't tweek kernel, so just return 0 */
  1103. }
  1104. static __kprobes
  1105. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1106. {
  1107. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1108. if (tp->flags & TP_FLAG_TRACE)
  1109. kretprobe_trace_func(ri, regs);
  1110. #ifdef CONFIG_EVENT_PROFILE
  1111. if (tp->flags & TP_FLAG_PROFILE)
  1112. kretprobe_profile_func(ri, regs);
  1113. #endif /* CONFIG_EVENT_PROFILE */
  1114. return 0; /* We don't tweek kernel, so just return 0 */
  1115. }
  1116. static int register_probe_event(struct trace_probe *tp)
  1117. {
  1118. struct ftrace_event_call *call = &tp->call;
  1119. int ret;
  1120. /* Initialize ftrace_event_call */
  1121. if (probe_is_return(tp)) {
  1122. tp->event.trace = print_kretprobe_event;
  1123. call->raw_init = probe_event_raw_init;
  1124. call->show_format = kretprobe_event_show_format;
  1125. call->define_fields = kretprobe_event_define_fields;
  1126. } else {
  1127. tp->event.trace = print_kprobe_event;
  1128. call->raw_init = probe_event_raw_init;
  1129. call->show_format = kprobe_event_show_format;
  1130. call->define_fields = kprobe_event_define_fields;
  1131. }
  1132. call->event = &tp->event;
  1133. call->id = register_ftrace_event(&tp->event);
  1134. if (!call->id)
  1135. return -ENODEV;
  1136. call->enabled = 0;
  1137. call->regfunc = probe_event_enable;
  1138. call->unregfunc = probe_event_disable;
  1139. #ifdef CONFIG_EVENT_PROFILE
  1140. atomic_set(&call->profile_count, -1);
  1141. call->profile_enable = probe_profile_enable;
  1142. call->profile_disable = probe_profile_disable;
  1143. #endif
  1144. call->data = tp;
  1145. ret = trace_add_event_call(call);
  1146. if (ret) {
  1147. pr_info("Failed to register kprobe event: %s\n", call->name);
  1148. unregister_ftrace_event(&tp->event);
  1149. }
  1150. return ret;
  1151. }
  1152. static void unregister_probe_event(struct trace_probe *tp)
  1153. {
  1154. /* tp->event is unregistered in trace_remove_event_call() */
  1155. trace_remove_event_call(&tp->call);
  1156. }
  1157. /* Make a debugfs interface for controling probe points */
  1158. static __init int init_kprobe_trace(void)
  1159. {
  1160. struct dentry *d_tracer;
  1161. struct dentry *entry;
  1162. d_tracer = tracing_init_dentry();
  1163. if (!d_tracer)
  1164. return 0;
  1165. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1166. NULL, &kprobe_events_ops);
  1167. /* Event list interface */
  1168. if (!entry)
  1169. pr_warning("Could not create debugfs "
  1170. "'kprobe_events' entry\n");
  1171. /* Profile interface */
  1172. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1173. NULL, &kprobe_profile_ops);
  1174. if (!entry)
  1175. pr_warning("Could not create debugfs "
  1176. "'kprobe_profile' entry\n");
  1177. return 0;
  1178. }
  1179. fs_initcall(init_kprobe_trace);
  1180. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1181. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1182. int a4, int a5, int a6)
  1183. {
  1184. return a1 + a2 + a3 + a4 + a5 + a6;
  1185. }
  1186. static __init int kprobe_trace_self_tests_init(void)
  1187. {
  1188. int ret;
  1189. int (*target)(int, int, int, int, int, int);
  1190. target = kprobe_trace_selftest_target;
  1191. pr_info("Testing kprobe tracing: ");
  1192. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1193. "a1 a2 a3 a4 a5 a6");
  1194. if (WARN_ON_ONCE(ret))
  1195. pr_warning("error enabling function entry\n");
  1196. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1197. "ra rv");
  1198. if (WARN_ON_ONCE(ret))
  1199. pr_warning("error enabling function return\n");
  1200. ret = target(1, 2, 3, 4, 5, 6);
  1201. cleanup_all_probes();
  1202. pr_cont("OK\n");
  1203. return 0;
  1204. }
  1205. late_initcall(kprobe_trace_self_tests_init);
  1206. #endif