trace_kprobe.c 33 KB

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