trace_kprobe.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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_class class;
  176. struct ftrace_event_call call;
  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.class = &tp->class;
  282. tp->call.name = kstrdup(event, GFP_KERNEL);
  283. if (!tp->call.name)
  284. goto error;
  285. if (!group || !check_event_name(group)) {
  286. ret = -EINVAL;
  287. goto error;
  288. }
  289. tp->class.system = kstrdup(group, GFP_KERNEL);
  290. if (!tp->class.system)
  291. goto error;
  292. INIT_LIST_HEAD(&tp->list);
  293. return tp;
  294. error:
  295. kfree(tp->call.name);
  296. kfree(tp->symbol);
  297. kfree(tp);
  298. return ERR_PTR(ret);
  299. }
  300. static void free_probe_arg(struct probe_arg *arg)
  301. {
  302. if (arg->fetch.func == fetch_symbol)
  303. free_symbol_cache(arg->fetch.data);
  304. else if (arg->fetch.func == fetch_indirect)
  305. free_indirect_fetch_data(arg->fetch.data);
  306. kfree(arg->name);
  307. }
  308. static void free_trace_probe(struct trace_probe *tp)
  309. {
  310. int i;
  311. for (i = 0; i < tp->nr_args; i++)
  312. free_probe_arg(&tp->args[i]);
  313. kfree(tp->call.class->system);
  314. kfree(tp->call.name);
  315. kfree(tp->symbol);
  316. kfree(tp);
  317. }
  318. static struct trace_probe *find_probe_event(const char *event,
  319. const char *group)
  320. {
  321. struct trace_probe *tp;
  322. list_for_each_entry(tp, &probe_list, list)
  323. if (strcmp(tp->call.name, event) == 0 &&
  324. strcmp(tp->call.class->system, group) == 0)
  325. return tp;
  326. return NULL;
  327. }
  328. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  329. static void unregister_trace_probe(struct trace_probe *tp)
  330. {
  331. if (probe_is_return(tp))
  332. unregister_kretprobe(&tp->rp);
  333. else
  334. unregister_kprobe(&tp->rp.kp);
  335. list_del(&tp->list);
  336. unregister_probe_event(tp);
  337. }
  338. /* Register a trace_probe and probe_event */
  339. static int register_trace_probe(struct trace_probe *tp)
  340. {
  341. struct trace_probe *old_tp;
  342. int ret;
  343. mutex_lock(&probe_lock);
  344. /* register as an event */
  345. old_tp = find_probe_event(tp->call.name, tp->call.class->system);
  346. if (old_tp) {
  347. /* delete old event */
  348. unregister_trace_probe(old_tp);
  349. free_trace_probe(old_tp);
  350. }
  351. ret = register_probe_event(tp);
  352. if (ret) {
  353. pr_warning("Faild to register probe event(%d)\n", ret);
  354. goto end;
  355. }
  356. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  357. if (probe_is_return(tp))
  358. ret = register_kretprobe(&tp->rp);
  359. else
  360. ret = register_kprobe(&tp->rp.kp);
  361. if (ret) {
  362. pr_warning("Could not insert probe(%d)\n", ret);
  363. if (ret == -EILSEQ) {
  364. pr_warning("Probing address(0x%p) is not an "
  365. "instruction boundary.\n",
  366. tp->rp.kp.addr);
  367. ret = -EINVAL;
  368. }
  369. unregister_probe_event(tp);
  370. } else
  371. list_add_tail(&tp->list, &probe_list);
  372. end:
  373. mutex_unlock(&probe_lock);
  374. return ret;
  375. }
  376. /* Split symbol and offset. */
  377. static int split_symbol_offset(char *symbol, unsigned long *offset)
  378. {
  379. char *tmp;
  380. int ret;
  381. if (!offset)
  382. return -EINVAL;
  383. tmp = strchr(symbol, '+');
  384. if (tmp) {
  385. /* skip sign because strict_strtol doesn't accept '+' */
  386. ret = strict_strtoul(tmp + 1, 0, offset);
  387. if (ret)
  388. return ret;
  389. *tmp = '\0';
  390. } else
  391. *offset = 0;
  392. return 0;
  393. }
  394. #define PARAM_MAX_ARGS 16
  395. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  396. static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
  397. {
  398. int ret = 0;
  399. unsigned long param;
  400. if (strcmp(arg, "retval") == 0) {
  401. if (is_return) {
  402. ff->func = fetch_retvalue;
  403. ff->data = NULL;
  404. } else
  405. ret = -EINVAL;
  406. } else if (strncmp(arg, "stack", 5) == 0) {
  407. if (arg[5] == '\0') {
  408. ff->func = fetch_stack_address;
  409. ff->data = NULL;
  410. } else if (isdigit(arg[5])) {
  411. ret = strict_strtoul(arg + 5, 10, &param);
  412. if (ret || param > PARAM_MAX_STACK)
  413. ret = -EINVAL;
  414. else {
  415. ff->func = fetch_stack;
  416. ff->data = (void *)param;
  417. }
  418. } else
  419. ret = -EINVAL;
  420. } else
  421. ret = -EINVAL;
  422. return ret;
  423. }
  424. /* Recursive argument parser */
  425. static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  426. {
  427. int ret = 0;
  428. unsigned long param;
  429. long offset;
  430. char *tmp;
  431. switch (arg[0]) {
  432. case '$':
  433. ret = parse_probe_vars(arg + 1, ff, is_return);
  434. break;
  435. case '%': /* named register */
  436. ret = regs_query_register_offset(arg + 1);
  437. if (ret >= 0) {
  438. ff->func = fetch_register;
  439. ff->data = (void *)(unsigned long)ret;
  440. ret = 0;
  441. }
  442. break;
  443. case '@': /* memory or symbol */
  444. if (isdigit(arg[1])) {
  445. ret = strict_strtoul(arg + 1, 0, &param);
  446. if (ret)
  447. break;
  448. ff->func = fetch_memory;
  449. ff->data = (void *)param;
  450. } else {
  451. ret = split_symbol_offset(arg + 1, &offset);
  452. if (ret)
  453. break;
  454. ff->data = alloc_symbol_cache(arg + 1, offset);
  455. if (ff->data)
  456. ff->func = fetch_symbol;
  457. else
  458. ret = -EINVAL;
  459. }
  460. break;
  461. case '+': /* indirect memory */
  462. case '-':
  463. tmp = strchr(arg, '(');
  464. if (!tmp) {
  465. ret = -EINVAL;
  466. break;
  467. }
  468. *tmp = '\0';
  469. ret = strict_strtol(arg + 1, 0, &offset);
  470. if (ret)
  471. break;
  472. if (arg[0] == '-')
  473. offset = -offset;
  474. arg = tmp + 1;
  475. tmp = strrchr(arg, ')');
  476. if (tmp) {
  477. struct indirect_fetch_data *id;
  478. *tmp = '\0';
  479. id = kzalloc(sizeof(struct indirect_fetch_data),
  480. GFP_KERNEL);
  481. if (!id)
  482. return -ENOMEM;
  483. id->offset = offset;
  484. ret = __parse_probe_arg(arg, &id->orig, is_return);
  485. if (ret)
  486. kfree(id);
  487. else {
  488. ff->func = fetch_indirect;
  489. ff->data = (void *)id;
  490. }
  491. } else
  492. ret = -EINVAL;
  493. break;
  494. default:
  495. /* TODO: support custom handler */
  496. ret = -EINVAL;
  497. }
  498. return ret;
  499. }
  500. /* String length checking wrapper */
  501. static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  502. {
  503. if (strlen(arg) > MAX_ARGSTR_LEN) {
  504. pr_info("Argument is too long.: %s\n", arg);
  505. return -ENOSPC;
  506. }
  507. return __parse_probe_arg(arg, ff, is_return);
  508. }
  509. /* Return 1 if name is reserved or already used by another argument */
  510. static int conflict_field_name(const char *name,
  511. struct probe_arg *args, int narg)
  512. {
  513. int i;
  514. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  515. if (strcmp(reserved_field_names[i], name) == 0)
  516. return 1;
  517. for (i = 0; i < narg; i++)
  518. if (strcmp(args[i].name, name) == 0)
  519. return 1;
  520. return 0;
  521. }
  522. static int create_trace_probe(int argc, char **argv)
  523. {
  524. /*
  525. * Argument syntax:
  526. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  527. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  528. * Fetch args:
  529. * $retval : fetch return value
  530. * $stack : fetch stack address
  531. * $stackN : fetch Nth of stack (N:0-)
  532. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  533. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  534. * %REG : fetch register REG
  535. * Indirect memory fetch:
  536. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  537. * Alias name of args:
  538. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  539. */
  540. struct trace_probe *tp;
  541. int i, ret = 0;
  542. int is_return = 0, is_delete = 0;
  543. char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
  544. unsigned long offset = 0;
  545. void *addr = NULL;
  546. char buf[MAX_EVENT_NAME_LEN];
  547. /* argc must be >= 1 */
  548. if (argv[0][0] == 'p')
  549. is_return = 0;
  550. else if (argv[0][0] == 'r')
  551. is_return = 1;
  552. else if (argv[0][0] == '-')
  553. is_delete = 1;
  554. else {
  555. pr_info("Probe definition must be started with 'p', 'r' or"
  556. " '-'.\n");
  557. return -EINVAL;
  558. }
  559. if (argv[0][1] == ':') {
  560. event = &argv[0][2];
  561. if (strchr(event, '/')) {
  562. group = event;
  563. event = strchr(group, '/') + 1;
  564. event[-1] = '\0';
  565. if (strlen(group) == 0) {
  566. pr_info("Group name is not specified\n");
  567. return -EINVAL;
  568. }
  569. }
  570. if (strlen(event) == 0) {
  571. pr_info("Event name is not specified\n");
  572. return -EINVAL;
  573. }
  574. }
  575. if (!group)
  576. group = KPROBE_EVENT_SYSTEM;
  577. if (is_delete) {
  578. if (!event) {
  579. pr_info("Delete command needs an event name.\n");
  580. return -EINVAL;
  581. }
  582. tp = find_probe_event(event, group);
  583. if (!tp) {
  584. pr_info("Event %s/%s doesn't exist.\n", group, event);
  585. return -ENOENT;
  586. }
  587. /* delete an event */
  588. unregister_trace_probe(tp);
  589. free_trace_probe(tp);
  590. return 0;
  591. }
  592. if (argc < 2) {
  593. pr_info("Probe point is not specified.\n");
  594. return -EINVAL;
  595. }
  596. if (isdigit(argv[1][0])) {
  597. if (is_return) {
  598. pr_info("Return probe point must be a symbol.\n");
  599. return -EINVAL;
  600. }
  601. /* an address specified */
  602. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  603. if (ret) {
  604. pr_info("Failed to parse address.\n");
  605. return ret;
  606. }
  607. } else {
  608. /* a symbol specified */
  609. symbol = argv[1];
  610. /* TODO: support .init module functions */
  611. ret = split_symbol_offset(symbol, &offset);
  612. if (ret) {
  613. pr_info("Failed to parse symbol.\n");
  614. return ret;
  615. }
  616. if (offset && is_return) {
  617. pr_info("Return probe must be used without offset.\n");
  618. return -EINVAL;
  619. }
  620. }
  621. argc -= 2; argv += 2;
  622. /* setup a probe */
  623. if (!event) {
  624. /* Make a new event name */
  625. if (symbol)
  626. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  627. is_return ? 'r' : 'p', symbol, offset);
  628. else
  629. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  630. is_return ? 'r' : 'p', addr);
  631. event = buf;
  632. }
  633. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  634. is_return);
  635. if (IS_ERR(tp)) {
  636. pr_info("Failed to allocate trace_probe.(%d)\n",
  637. (int)PTR_ERR(tp));
  638. return PTR_ERR(tp);
  639. }
  640. /* parse arguments */
  641. ret = 0;
  642. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  643. /* Parse argument name */
  644. arg = strchr(argv[i], '=');
  645. if (arg)
  646. *arg++ = '\0';
  647. else
  648. arg = argv[i];
  649. if (conflict_field_name(argv[i], tp->args, i)) {
  650. pr_info("Argument%d name '%s' conflicts with "
  651. "another field.\n", i, argv[i]);
  652. ret = -EINVAL;
  653. goto error;
  654. }
  655. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  656. if (!tp->args[i].name) {
  657. pr_info("Failed to allocate argument%d name '%s'.\n",
  658. i, argv[i]);
  659. ret = -ENOMEM;
  660. goto error;
  661. }
  662. /* Parse fetch argument */
  663. ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
  664. if (ret) {
  665. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  666. kfree(tp->args[i].name);
  667. goto error;
  668. }
  669. tp->nr_args++;
  670. }
  671. ret = register_trace_probe(tp);
  672. if (ret)
  673. goto error;
  674. return 0;
  675. error:
  676. free_trace_probe(tp);
  677. return ret;
  678. }
  679. static void cleanup_all_probes(void)
  680. {
  681. struct trace_probe *tp;
  682. mutex_lock(&probe_lock);
  683. /* TODO: Use batch unregistration */
  684. while (!list_empty(&probe_list)) {
  685. tp = list_entry(probe_list.next, struct trace_probe, list);
  686. unregister_trace_probe(tp);
  687. free_trace_probe(tp);
  688. }
  689. mutex_unlock(&probe_lock);
  690. }
  691. /* Probes listing interfaces */
  692. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  693. {
  694. mutex_lock(&probe_lock);
  695. return seq_list_start(&probe_list, *pos);
  696. }
  697. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  698. {
  699. return seq_list_next(v, &probe_list, pos);
  700. }
  701. static void probes_seq_stop(struct seq_file *m, void *v)
  702. {
  703. mutex_unlock(&probe_lock);
  704. }
  705. static int probes_seq_show(struct seq_file *m, void *v)
  706. {
  707. struct trace_probe *tp = v;
  708. int i, ret;
  709. char buf[MAX_ARGSTR_LEN + 1];
  710. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  711. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  712. if (!tp->symbol)
  713. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  714. else if (tp->rp.kp.offset)
  715. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  716. else
  717. seq_printf(m, " %s", probe_symbol(tp));
  718. for (i = 0; i < tp->nr_args; i++) {
  719. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
  720. if (ret < 0) {
  721. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  722. return ret;
  723. }
  724. seq_printf(m, " %s=%s", tp->args[i].name, buf);
  725. }
  726. seq_printf(m, "\n");
  727. return 0;
  728. }
  729. static const struct seq_operations probes_seq_op = {
  730. .start = probes_seq_start,
  731. .next = probes_seq_next,
  732. .stop = probes_seq_stop,
  733. .show = probes_seq_show
  734. };
  735. static int probes_open(struct inode *inode, struct file *file)
  736. {
  737. if ((file->f_mode & FMODE_WRITE) &&
  738. (file->f_flags & O_TRUNC))
  739. cleanup_all_probes();
  740. return seq_open(file, &probes_seq_op);
  741. }
  742. static int command_trace_probe(const char *buf)
  743. {
  744. char **argv;
  745. int argc = 0, ret = 0;
  746. argv = argv_split(GFP_KERNEL, buf, &argc);
  747. if (!argv)
  748. return -ENOMEM;
  749. if (argc)
  750. ret = create_trace_probe(argc, argv);
  751. argv_free(argv);
  752. return ret;
  753. }
  754. #define WRITE_BUFSIZE 128
  755. static ssize_t probes_write(struct file *file, const char __user *buffer,
  756. size_t count, loff_t *ppos)
  757. {
  758. char *kbuf, *tmp;
  759. int ret;
  760. size_t done;
  761. size_t size;
  762. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  763. if (!kbuf)
  764. return -ENOMEM;
  765. ret = done = 0;
  766. while (done < count) {
  767. size = count - done;
  768. if (size >= WRITE_BUFSIZE)
  769. size = WRITE_BUFSIZE - 1;
  770. if (copy_from_user(kbuf, buffer + done, size)) {
  771. ret = -EFAULT;
  772. goto out;
  773. }
  774. kbuf[size] = '\0';
  775. tmp = strchr(kbuf, '\n');
  776. if (tmp) {
  777. *tmp = '\0';
  778. size = tmp - kbuf + 1;
  779. } else if (done + size < count) {
  780. pr_warning("Line length is too long: "
  781. "Should be less than %d.", WRITE_BUFSIZE);
  782. ret = -EINVAL;
  783. goto out;
  784. }
  785. done += size;
  786. /* Remove comments */
  787. tmp = strchr(kbuf, '#');
  788. if (tmp)
  789. *tmp = '\0';
  790. ret = command_trace_probe(kbuf);
  791. if (ret)
  792. goto out;
  793. }
  794. ret = done;
  795. out:
  796. kfree(kbuf);
  797. return ret;
  798. }
  799. static const struct file_operations kprobe_events_ops = {
  800. .owner = THIS_MODULE,
  801. .open = probes_open,
  802. .read = seq_read,
  803. .llseek = seq_lseek,
  804. .release = seq_release,
  805. .write = probes_write,
  806. };
  807. /* Probes profiling interfaces */
  808. static int probes_profile_seq_show(struct seq_file *m, void *v)
  809. {
  810. struct trace_probe *tp = v;
  811. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  812. tp->rp.kp.nmissed);
  813. return 0;
  814. }
  815. static const struct seq_operations profile_seq_op = {
  816. .start = probes_seq_start,
  817. .next = probes_seq_next,
  818. .stop = probes_seq_stop,
  819. .show = probes_profile_seq_show
  820. };
  821. static int profile_open(struct inode *inode, struct file *file)
  822. {
  823. return seq_open(file, &profile_seq_op);
  824. }
  825. static const struct file_operations kprobe_profile_ops = {
  826. .owner = THIS_MODULE,
  827. .open = profile_open,
  828. .read = seq_read,
  829. .llseek = seq_lseek,
  830. .release = seq_release,
  831. };
  832. /* Kprobe handler */
  833. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  834. {
  835. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  836. struct kprobe_trace_entry *entry;
  837. struct ring_buffer_event *event;
  838. struct ring_buffer *buffer;
  839. int size, i, pc;
  840. unsigned long irq_flags;
  841. struct ftrace_event_call *call = &tp->call;
  842. tp->nhit++;
  843. local_save_flags(irq_flags);
  844. pc = preempt_count();
  845. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  846. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  847. size, irq_flags, pc);
  848. if (!event)
  849. return;
  850. entry = ring_buffer_event_data(event);
  851. entry->nargs = tp->nr_args;
  852. entry->ip = (unsigned long)kp->addr;
  853. for (i = 0; i < tp->nr_args; i++)
  854. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  855. if (!filter_current_check_discard(buffer, call, entry, event))
  856. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  857. }
  858. /* Kretprobe handler */
  859. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  860. struct pt_regs *regs)
  861. {
  862. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  863. struct kretprobe_trace_entry *entry;
  864. struct ring_buffer_event *event;
  865. struct ring_buffer *buffer;
  866. int size, i, pc;
  867. unsigned long irq_flags;
  868. struct ftrace_event_call *call = &tp->call;
  869. local_save_flags(irq_flags);
  870. pc = preempt_count();
  871. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  872. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  873. size, irq_flags, pc);
  874. if (!event)
  875. return;
  876. entry = ring_buffer_event_data(event);
  877. entry->nargs = tp->nr_args;
  878. entry->func = (unsigned long)tp->rp.kp.addr;
  879. entry->ret_ip = (unsigned long)ri->ret_addr;
  880. for (i = 0; i < tp->nr_args; i++)
  881. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  882. if (!filter_current_check_discard(buffer, call, entry, event))
  883. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  884. }
  885. /* Event entry printers */
  886. enum print_line_t
  887. print_kprobe_event(struct trace_iterator *iter, int flags,
  888. struct trace_event *event)
  889. {
  890. struct kprobe_trace_entry *field;
  891. struct trace_seq *s = &iter->seq;
  892. struct trace_probe *tp;
  893. int i;
  894. field = (struct kprobe_trace_entry *)iter->ent;
  895. tp = container_of(event, struct trace_probe, call.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. struct trace_event *event)
  915. {
  916. struct kretprobe_trace_entry *field;
  917. struct trace_seq *s = &iter->seq;
  918. struct trace_probe *tp;
  919. int i;
  920. field = (struct kretprobe_trace_entry *)iter->ent;
  921. tp = container_of(event, struct trace_probe, call.event);
  922. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  923. goto partial;
  924. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  925. goto partial;
  926. if (!trace_seq_puts(s, " <- "))
  927. goto partial;
  928. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  929. goto partial;
  930. if (!trace_seq_puts(s, ")"))
  931. goto partial;
  932. for (i = 0; i < field->nargs; i++)
  933. if (!trace_seq_printf(s, " %s=%lx",
  934. tp->args[i].name, field->args[i]))
  935. goto partial;
  936. if (!trace_seq_puts(s, "\n"))
  937. goto partial;
  938. return TRACE_TYPE_HANDLED;
  939. partial:
  940. return TRACE_TYPE_PARTIAL_LINE;
  941. }
  942. static int probe_event_enable(struct ftrace_event_call *call)
  943. {
  944. struct trace_probe *tp = (struct trace_probe *)call->data;
  945. tp->flags |= TP_FLAG_TRACE;
  946. if (probe_is_return(tp))
  947. return enable_kretprobe(&tp->rp);
  948. else
  949. return enable_kprobe(&tp->rp.kp);
  950. }
  951. static void probe_event_disable(struct ftrace_event_call *call)
  952. {
  953. struct trace_probe *tp = (struct trace_probe *)call->data;
  954. tp->flags &= ~TP_FLAG_TRACE;
  955. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  956. if (probe_is_return(tp))
  957. disable_kretprobe(&tp->rp);
  958. else
  959. disable_kprobe(&tp->rp.kp);
  960. }
  961. }
  962. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  963. {
  964. return 0;
  965. }
  966. #undef DEFINE_FIELD
  967. #define DEFINE_FIELD(type, item, name, is_signed) \
  968. do { \
  969. ret = trace_define_field(event_call, #type, name, \
  970. offsetof(typeof(field), item), \
  971. sizeof(field.item), is_signed, \
  972. FILTER_OTHER); \
  973. if (ret) \
  974. return ret; \
  975. } while (0)
  976. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  977. {
  978. int ret, i;
  979. struct kprobe_trace_entry field;
  980. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  981. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  982. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  983. /* Set argument names as fields */
  984. for (i = 0; i < tp->nr_args; i++)
  985. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  986. return 0;
  987. }
  988. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  989. {
  990. int ret, i;
  991. struct kretprobe_trace_entry field;
  992. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  993. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  994. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  995. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  996. /* Set argument names as fields */
  997. for (i = 0; i < tp->nr_args; i++)
  998. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  999. return 0;
  1000. }
  1001. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1002. {
  1003. int i;
  1004. int pos = 0;
  1005. const char *fmt, *arg;
  1006. if (!probe_is_return(tp)) {
  1007. fmt = "(%lx)";
  1008. arg = "REC->" FIELD_STRING_IP;
  1009. } else {
  1010. fmt = "(%lx <- %lx)";
  1011. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1012. }
  1013. /* When len=0, we just calculate the needed length */
  1014. #define LEN_OR_ZERO (len ? len - pos : 0)
  1015. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1016. for (i = 0; i < tp->nr_args; i++) {
  1017. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%%lx",
  1018. tp->args[i].name);
  1019. }
  1020. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1021. for (i = 0; i < tp->nr_args; i++) {
  1022. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1023. tp->args[i].name);
  1024. }
  1025. #undef LEN_OR_ZERO
  1026. /* return the length of print_fmt */
  1027. return pos;
  1028. }
  1029. static int set_print_fmt(struct trace_probe *tp)
  1030. {
  1031. int len;
  1032. char *print_fmt;
  1033. /* First: called with 0 length to calculate the needed length */
  1034. len = __set_print_fmt(tp, NULL, 0);
  1035. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1036. if (!print_fmt)
  1037. return -ENOMEM;
  1038. /* Second: actually write the @print_fmt */
  1039. __set_print_fmt(tp, print_fmt, len + 1);
  1040. tp->call.print_fmt = print_fmt;
  1041. return 0;
  1042. }
  1043. #ifdef CONFIG_PERF_EVENTS
  1044. /* Kprobe profile handler */
  1045. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1046. struct pt_regs *regs)
  1047. {
  1048. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1049. struct ftrace_event_call *call = &tp->call;
  1050. struct kprobe_trace_entry *entry;
  1051. int size, __size, i;
  1052. unsigned long irq_flags;
  1053. int rctx;
  1054. __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  1055. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1056. size -= sizeof(u32);
  1057. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1058. "profile buffer not large enough"))
  1059. return;
  1060. entry = perf_trace_buf_prepare(size, call->event.type,
  1061. &rctx, &irq_flags);
  1062. if (!entry)
  1063. return;
  1064. entry->nargs = tp->nr_args;
  1065. entry->ip = (unsigned long)kp->addr;
  1066. for (i = 0; i < tp->nr_args; i++)
  1067. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1068. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags, regs);
  1069. }
  1070. /* Kretprobe profile handler */
  1071. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1072. struct pt_regs *regs)
  1073. {
  1074. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1075. struct ftrace_event_call *call = &tp->call;
  1076. struct kretprobe_trace_entry *entry;
  1077. int size, __size, i;
  1078. unsigned long irq_flags;
  1079. int rctx;
  1080. __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  1081. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1082. size -= sizeof(u32);
  1083. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1084. "profile buffer not large enough"))
  1085. return;
  1086. entry = perf_trace_buf_prepare(size, call->event.type,
  1087. &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_register(struct ftrace_event_call *event, enum trace_reg type)
  1121. {
  1122. switch (type) {
  1123. case TRACE_REG_REGISTER:
  1124. return probe_event_enable(event);
  1125. case TRACE_REG_UNREGISTER:
  1126. probe_event_disable(event);
  1127. return 0;
  1128. #ifdef CONFIG_PERF_EVENTS
  1129. case TRACE_REG_PERF_REGISTER:
  1130. return probe_perf_enable(event);
  1131. case TRACE_REG_PERF_UNREGISTER:
  1132. probe_perf_disable(event);
  1133. return 0;
  1134. #endif
  1135. }
  1136. return 0;
  1137. }
  1138. static __kprobes
  1139. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1140. {
  1141. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1142. if (tp->flags & TP_FLAG_TRACE)
  1143. kprobe_trace_func(kp, regs);
  1144. #ifdef CONFIG_PERF_EVENTS
  1145. if (tp->flags & TP_FLAG_PROFILE)
  1146. kprobe_perf_func(kp, regs);
  1147. #endif
  1148. return 0; /* We don't tweek kernel, so just return 0 */
  1149. }
  1150. static __kprobes
  1151. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1152. {
  1153. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1154. if (tp->flags & TP_FLAG_TRACE)
  1155. kretprobe_trace_func(ri, regs);
  1156. #ifdef CONFIG_PERF_EVENTS
  1157. if (tp->flags & TP_FLAG_PROFILE)
  1158. kretprobe_perf_func(ri, regs);
  1159. #endif
  1160. return 0; /* We don't tweek kernel, so just return 0 */
  1161. }
  1162. static struct trace_event_functions kretprobe_funcs = {
  1163. .trace = print_kretprobe_event
  1164. };
  1165. static struct trace_event_functions kprobe_funcs = {
  1166. .trace = print_kprobe_event
  1167. };
  1168. static int register_probe_event(struct trace_probe *tp)
  1169. {
  1170. struct ftrace_event_call *call = &tp->call;
  1171. int ret;
  1172. /* Initialize ftrace_event_call */
  1173. if (probe_is_return(tp)) {
  1174. INIT_LIST_HEAD(&call->class->fields);
  1175. call->event.funcs = &kretprobe_funcs;
  1176. call->class->raw_init = probe_event_raw_init;
  1177. call->class->define_fields = kretprobe_event_define_fields;
  1178. } else {
  1179. INIT_LIST_HEAD(&call->class->fields);
  1180. call->event.funcs = &kprobe_funcs;
  1181. call->class->raw_init = probe_event_raw_init;
  1182. call->class->define_fields = kprobe_event_define_fields;
  1183. }
  1184. if (set_print_fmt(tp) < 0)
  1185. return -ENOMEM;
  1186. ret = register_ftrace_event(&call->event);
  1187. if (!ret) {
  1188. kfree(call->print_fmt);
  1189. return -ENODEV;
  1190. }
  1191. call->enabled = 0;
  1192. call->class->reg = kprobe_register;
  1193. call->data = tp;
  1194. ret = trace_add_event_call(call);
  1195. if (ret) {
  1196. pr_info("Failed to register kprobe event: %s\n", call->name);
  1197. kfree(call->print_fmt);
  1198. unregister_ftrace_event(&call->event);
  1199. }
  1200. return ret;
  1201. }
  1202. static void unregister_probe_event(struct trace_probe *tp)
  1203. {
  1204. /* tp->event is unregistered in trace_remove_event_call() */
  1205. trace_remove_event_call(&tp->call);
  1206. kfree(tp->call.print_fmt);
  1207. }
  1208. /* Make a debugfs interface for controling probe points */
  1209. static __init int init_kprobe_trace(void)
  1210. {
  1211. struct dentry *d_tracer;
  1212. struct dentry *entry;
  1213. d_tracer = tracing_init_dentry();
  1214. if (!d_tracer)
  1215. return 0;
  1216. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1217. NULL, &kprobe_events_ops);
  1218. /* Event list interface */
  1219. if (!entry)
  1220. pr_warning("Could not create debugfs "
  1221. "'kprobe_events' entry\n");
  1222. /* Profile interface */
  1223. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1224. NULL, &kprobe_profile_ops);
  1225. if (!entry)
  1226. pr_warning("Could not create debugfs "
  1227. "'kprobe_profile' entry\n");
  1228. return 0;
  1229. }
  1230. fs_initcall(init_kprobe_trace);
  1231. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1232. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1233. int a4, int a5, int a6)
  1234. {
  1235. return a1 + a2 + a3 + a4 + a5 + a6;
  1236. }
  1237. static __init int kprobe_trace_self_tests_init(void)
  1238. {
  1239. int ret, warn = 0;
  1240. int (*target)(int, int, int, int, int, int);
  1241. struct trace_probe *tp;
  1242. target = kprobe_trace_selftest_target;
  1243. pr_info("Testing kprobe tracing: ");
  1244. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1245. "$stack $stack0 +0($stack)");
  1246. if (WARN_ON_ONCE(ret)) {
  1247. pr_warning("error on probing function entry.\n");
  1248. warn++;
  1249. } else {
  1250. /* Enable trace point */
  1251. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1252. if (WARN_ON_ONCE(tp == NULL)) {
  1253. pr_warning("error on getting new probe.\n");
  1254. warn++;
  1255. } else
  1256. probe_event_enable(&tp->call);
  1257. }
  1258. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1259. "$retval");
  1260. if (WARN_ON_ONCE(ret)) {
  1261. pr_warning("error on probing function return.\n");
  1262. warn++;
  1263. } else {
  1264. /* Enable trace point */
  1265. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1266. if (WARN_ON_ONCE(tp == NULL)) {
  1267. pr_warning("error on getting new probe.\n");
  1268. warn++;
  1269. } else
  1270. probe_event_enable(&tp->call);
  1271. }
  1272. if (warn)
  1273. goto end;
  1274. ret = target(1, 2, 3, 4, 5, 6);
  1275. ret = command_trace_probe("-:testprobe");
  1276. if (WARN_ON_ONCE(ret)) {
  1277. pr_warning("error on deleting a probe.\n");
  1278. warn++;
  1279. }
  1280. ret = command_trace_probe("-:testprobe2");
  1281. if (WARN_ON_ONCE(ret)) {
  1282. pr_warning("error on deleting a probe.\n");
  1283. warn++;
  1284. }
  1285. end:
  1286. cleanup_all_probes();
  1287. if (warn)
  1288. pr_cont("NG: Some tests are failed. Please check them.\n");
  1289. else
  1290. pr_cont("OK\n");
  1291. return 0;
  1292. }
  1293. late_initcall(kprobe_trace_self_tests_init);
  1294. #endif