trace_kprobe.c 35 KB

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