trace_kprobe.c 36 KB

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