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