trace_kprobe.c 35 KB

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