trace_kprobe.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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. /* Recursive argument parser */
  422. static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  423. {
  424. int ret = 0;
  425. unsigned long param;
  426. long offset;
  427. char *tmp;
  428. switch (arg[0]) {
  429. case '$':
  430. ret = parse_probe_vars(arg + 1, ff, is_return);
  431. break;
  432. case '%': /* named register */
  433. ret = regs_query_register_offset(arg + 1);
  434. if (ret >= 0) {
  435. ff->func = fetch_register;
  436. ff->data = (void *)(unsigned long)ret;
  437. ret = 0;
  438. }
  439. break;
  440. case '@': /* memory or symbol */
  441. if (isdigit(arg[1])) {
  442. ret = strict_strtoul(arg + 1, 0, &param);
  443. if (ret)
  444. break;
  445. ff->func = fetch_memory;
  446. ff->data = (void *)param;
  447. } else {
  448. ret = split_symbol_offset(arg + 1, &offset);
  449. if (ret)
  450. break;
  451. ff->data = alloc_symbol_cache(arg + 1, offset);
  452. if (ff->data)
  453. ff->func = fetch_symbol;
  454. else
  455. ret = -EINVAL;
  456. }
  457. break;
  458. case '+': /* indirect memory */
  459. case '-':
  460. tmp = strchr(arg, '(');
  461. if (!tmp) {
  462. ret = -EINVAL;
  463. break;
  464. }
  465. *tmp = '\0';
  466. ret = strict_strtol(arg + 1, 0, &offset);
  467. if (ret)
  468. break;
  469. if (arg[0] == '-')
  470. offset = -offset;
  471. arg = tmp + 1;
  472. tmp = strrchr(arg, ')');
  473. if (tmp) {
  474. struct indirect_fetch_data *id;
  475. *tmp = '\0';
  476. id = kzalloc(sizeof(struct indirect_fetch_data),
  477. GFP_KERNEL);
  478. if (!id)
  479. return -ENOMEM;
  480. id->offset = offset;
  481. ret = __parse_probe_arg(arg, &id->orig, is_return);
  482. if (ret)
  483. kfree(id);
  484. else {
  485. ff->func = fetch_indirect;
  486. ff->data = (void *)id;
  487. }
  488. } else
  489. ret = -EINVAL;
  490. break;
  491. default:
  492. /* TODO: support custom handler */
  493. ret = -EINVAL;
  494. }
  495. return ret;
  496. }
  497. /* String length checking wrapper */
  498. static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  499. {
  500. if (strlen(arg) > MAX_ARGSTR_LEN) {
  501. pr_info("Argument is too long.: %s\n", arg);
  502. return -ENOSPC;
  503. }
  504. return __parse_probe_arg(arg, ff, is_return);
  505. }
  506. /* Return 1 if name is reserved or already used by another argument */
  507. static int conflict_field_name(const char *name,
  508. struct probe_arg *args, int narg)
  509. {
  510. int i;
  511. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  512. if (strcmp(reserved_field_names[i], name) == 0)
  513. return 1;
  514. for (i = 0; i < narg; i++)
  515. if (strcmp(args[i].name, name) == 0)
  516. return 1;
  517. return 0;
  518. }
  519. static int create_trace_probe(int argc, char **argv)
  520. {
  521. /*
  522. * Argument syntax:
  523. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  524. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  525. * Fetch args:
  526. * $argN : fetch Nth of function argument. (N:0-)
  527. * $retval : fetch return value
  528. * $stack : fetch stack address
  529. * $stackN : fetch Nth of stack (N:0-)
  530. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  531. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  532. * %REG : fetch register REG
  533. * Indirect memory fetch:
  534. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  535. * Alias name of args:
  536. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  537. */
  538. struct trace_probe *tp;
  539. int i, ret = 0;
  540. int is_return = 0, is_delete = 0;
  541. char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
  542. unsigned long offset = 0;
  543. void *addr = NULL;
  544. char buf[MAX_EVENT_NAME_LEN];
  545. /* argc must be >= 1 */
  546. if (argv[0][0] == 'p')
  547. is_return = 0;
  548. else if (argv[0][0] == 'r')
  549. is_return = 1;
  550. else if (argv[0][0] == '-')
  551. is_delete = 1;
  552. else {
  553. pr_info("Probe definition must be started with 'p', 'r' or"
  554. " '-'.\n");
  555. return -EINVAL;
  556. }
  557. if (argv[0][1] == ':') {
  558. event = &argv[0][2];
  559. if (strchr(event, '/')) {
  560. group = event;
  561. event = strchr(group, '/') + 1;
  562. event[-1] = '\0';
  563. if (strlen(group) == 0) {
  564. pr_info("Group name is not specifiled\n");
  565. return -EINVAL;
  566. }
  567. }
  568. if (strlen(event) == 0) {
  569. pr_info("Event name is not specifiled\n");
  570. return -EINVAL;
  571. }
  572. }
  573. if (!group)
  574. group = KPROBE_EVENT_SYSTEM;
  575. if (is_delete) {
  576. if (!event) {
  577. pr_info("Delete command needs an event name.\n");
  578. return -EINVAL;
  579. }
  580. tp = find_probe_event(event, group);
  581. if (!tp) {
  582. pr_info("Event %s/%s doesn't exist.\n", group, event);
  583. return -ENOENT;
  584. }
  585. /* delete an event */
  586. unregister_trace_probe(tp);
  587. free_trace_probe(tp);
  588. return 0;
  589. }
  590. if (argc < 2) {
  591. pr_info("Probe point is not specified.\n");
  592. return -EINVAL;
  593. }
  594. if (isdigit(argv[1][0])) {
  595. if (is_return) {
  596. pr_info("Return probe point must be a symbol.\n");
  597. return -EINVAL;
  598. }
  599. /* an address specified */
  600. ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
  601. if (ret) {
  602. pr_info("Failed to parse address.\n");
  603. return ret;
  604. }
  605. } else {
  606. /* a symbol specified */
  607. symbol = argv[1];
  608. /* TODO: support .init module functions */
  609. ret = split_symbol_offset(symbol, &offset);
  610. if (ret) {
  611. pr_info("Failed to parse symbol.\n");
  612. return ret;
  613. }
  614. if (offset && is_return) {
  615. pr_info("Return probe must be used without offset.\n");
  616. return -EINVAL;
  617. }
  618. }
  619. argc -= 2; argv += 2;
  620. /* setup a probe */
  621. if (!event) {
  622. /* Make a new event name */
  623. if (symbol)
  624. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
  625. is_return ? 'r' : 'p', symbol, offset);
  626. else
  627. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
  628. is_return ? 'r' : 'p', addr);
  629. event = buf;
  630. }
  631. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  632. is_return);
  633. if (IS_ERR(tp)) {
  634. pr_info("Failed to allocate trace_probe.(%d)\n",
  635. (int)PTR_ERR(tp));
  636. return PTR_ERR(tp);
  637. }
  638. /* parse arguments */
  639. ret = 0;
  640. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  641. /* Parse argument name */
  642. arg = strchr(argv[i], '=');
  643. if (arg)
  644. *arg++ = '\0';
  645. else
  646. arg = argv[i];
  647. if (conflict_field_name(argv[i], tp->args, i)) {
  648. pr_info("Argument%d name '%s' conflicts with "
  649. "another field.\n", i, argv[i]);
  650. ret = -EINVAL;
  651. goto error;
  652. }
  653. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  654. if (!tp->args[i].name) {
  655. pr_info("Failed to allocate argument%d name '%s'.\n",
  656. i, argv[i]);
  657. ret = -ENOMEM;
  658. goto error;
  659. }
  660. /* Parse fetch argument */
  661. ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
  662. if (ret) {
  663. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  664. kfree(tp->args[i].name);
  665. goto error;
  666. }
  667. tp->nr_args++;
  668. }
  669. ret = register_trace_probe(tp);
  670. if (ret)
  671. goto error;
  672. return 0;
  673. error:
  674. free_trace_probe(tp);
  675. return ret;
  676. }
  677. static void cleanup_all_probes(void)
  678. {
  679. struct trace_probe *tp;
  680. mutex_lock(&probe_lock);
  681. /* TODO: Use batch unregistration */
  682. while (!list_empty(&probe_list)) {
  683. tp = list_entry(probe_list.next, struct trace_probe, list);
  684. unregister_trace_probe(tp);
  685. free_trace_probe(tp);
  686. }
  687. mutex_unlock(&probe_lock);
  688. }
  689. /* Probes listing interfaces */
  690. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  691. {
  692. mutex_lock(&probe_lock);
  693. return seq_list_start(&probe_list, *pos);
  694. }
  695. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  696. {
  697. return seq_list_next(v, &probe_list, pos);
  698. }
  699. static void probes_seq_stop(struct seq_file *m, void *v)
  700. {
  701. mutex_unlock(&probe_lock);
  702. }
  703. static int probes_seq_show(struct seq_file *m, void *v)
  704. {
  705. struct trace_probe *tp = v;
  706. int i, ret;
  707. char buf[MAX_ARGSTR_LEN + 1];
  708. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  709. seq_printf(m, ":%s/%s", tp->call.system, tp->call.name);
  710. if (!tp->symbol)
  711. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  712. else if (tp->rp.kp.offset)
  713. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  714. else
  715. seq_printf(m, " %s", probe_symbol(tp));
  716. for (i = 0; i < tp->nr_args; i++) {
  717. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
  718. if (ret < 0) {
  719. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  720. return ret;
  721. }
  722. seq_printf(m, " %s=%s", tp->args[i].name, buf);
  723. }
  724. seq_printf(m, "\n");
  725. return 0;
  726. }
  727. static const struct seq_operations probes_seq_op = {
  728. .start = probes_seq_start,
  729. .next = probes_seq_next,
  730. .stop = probes_seq_stop,
  731. .show = probes_seq_show
  732. };
  733. static int probes_open(struct inode *inode, struct file *file)
  734. {
  735. if ((file->f_mode & FMODE_WRITE) &&
  736. (file->f_flags & O_TRUNC))
  737. cleanup_all_probes();
  738. return seq_open(file, &probes_seq_op);
  739. }
  740. static int command_trace_probe(const char *buf)
  741. {
  742. char **argv;
  743. int argc = 0, ret = 0;
  744. argv = argv_split(GFP_KERNEL, buf, &argc);
  745. if (!argv)
  746. return -ENOMEM;
  747. if (argc)
  748. ret = create_trace_probe(argc, argv);
  749. argv_free(argv);
  750. return ret;
  751. }
  752. #define WRITE_BUFSIZE 128
  753. static ssize_t probes_write(struct file *file, const char __user *buffer,
  754. size_t count, loff_t *ppos)
  755. {
  756. char *kbuf, *tmp;
  757. int ret;
  758. size_t done;
  759. size_t size;
  760. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  761. if (!kbuf)
  762. return -ENOMEM;
  763. ret = done = 0;
  764. while (done < count) {
  765. size = count - done;
  766. if (size >= WRITE_BUFSIZE)
  767. size = WRITE_BUFSIZE - 1;
  768. if (copy_from_user(kbuf, buffer + done, size)) {
  769. ret = -EFAULT;
  770. goto out;
  771. }
  772. kbuf[size] = '\0';
  773. tmp = strchr(kbuf, '\n');
  774. if (tmp) {
  775. *tmp = '\0';
  776. size = tmp - kbuf + 1;
  777. } else if (done + size < count) {
  778. pr_warning("Line length is too long: "
  779. "Should be less than %d.", WRITE_BUFSIZE);
  780. ret = -EINVAL;
  781. goto out;
  782. }
  783. done += size;
  784. /* Remove comments */
  785. tmp = strchr(kbuf, '#');
  786. if (tmp)
  787. *tmp = '\0';
  788. ret = command_trace_probe(kbuf);
  789. if (ret)
  790. goto out;
  791. }
  792. ret = done;
  793. out:
  794. kfree(kbuf);
  795. return ret;
  796. }
  797. static const struct file_operations kprobe_events_ops = {
  798. .owner = THIS_MODULE,
  799. .open = probes_open,
  800. .read = seq_read,
  801. .llseek = seq_lseek,
  802. .release = seq_release,
  803. .write = probes_write,
  804. };
  805. /* Probes profiling interfaces */
  806. static int probes_profile_seq_show(struct seq_file *m, void *v)
  807. {
  808. struct trace_probe *tp = v;
  809. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  810. tp->rp.kp.nmissed);
  811. return 0;
  812. }
  813. static const struct seq_operations profile_seq_op = {
  814. .start = probes_seq_start,
  815. .next = probes_seq_next,
  816. .stop = probes_seq_stop,
  817. .show = probes_profile_seq_show
  818. };
  819. static int profile_open(struct inode *inode, struct file *file)
  820. {
  821. return seq_open(file, &profile_seq_op);
  822. }
  823. static const struct file_operations kprobe_profile_ops = {
  824. .owner = THIS_MODULE,
  825. .open = profile_open,
  826. .read = seq_read,
  827. .llseek = seq_lseek,
  828. .release = seq_release,
  829. };
  830. /* Kprobe handler */
  831. static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  832. {
  833. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  834. struct kprobe_trace_entry *entry;
  835. struct ring_buffer_event *event;
  836. struct ring_buffer *buffer;
  837. int size, i, pc;
  838. unsigned long irq_flags;
  839. struct ftrace_event_call *call = &tp->call;
  840. tp->nhit++;
  841. local_save_flags(irq_flags);
  842. pc = preempt_count();
  843. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  844. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  845. irq_flags, pc);
  846. if (!event)
  847. return 0;
  848. entry = ring_buffer_event_data(event);
  849. entry->nargs = tp->nr_args;
  850. entry->ip = (unsigned long)kp->addr;
  851. for (i = 0; i < tp->nr_args; i++)
  852. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  853. if (!filter_current_check_discard(buffer, call, entry, event))
  854. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  855. return 0;
  856. }
  857. /* Kretprobe handler */
  858. static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
  859. struct pt_regs *regs)
  860. {
  861. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  862. struct kretprobe_trace_entry *entry;
  863. struct ring_buffer_event *event;
  864. struct ring_buffer *buffer;
  865. int size, i, pc;
  866. unsigned long irq_flags;
  867. struct ftrace_event_call *call = &tp->call;
  868. local_save_flags(irq_flags);
  869. pc = preempt_count();
  870. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  871. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  872. irq_flags, pc);
  873. if (!event)
  874. return 0;
  875. entry = ring_buffer_event_data(event);
  876. entry->nargs = tp->nr_args;
  877. entry->func = (unsigned long)tp->rp.kp.addr;
  878. entry->ret_ip = (unsigned long)ri->ret_addr;
  879. for (i = 0; i < tp->nr_args; i++)
  880. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  881. if (!filter_current_check_discard(buffer, call, entry, event))
  882. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  883. return 0;
  884. }
  885. /* Event entry printers */
  886. enum print_line_t
  887. print_kprobe_event(struct trace_iterator *iter, int flags)
  888. {
  889. struct kprobe_trace_entry *field;
  890. struct trace_seq *s = &iter->seq;
  891. struct trace_event *event;
  892. struct trace_probe *tp;
  893. int i;
  894. field = (struct kprobe_trace_entry *)iter->ent;
  895. event = ftrace_find_event(field->ent.type);
  896. tp = container_of(event, struct trace_probe, event);
  897. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  898. goto partial;
  899. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  900. goto partial;
  901. if (!trace_seq_puts(s, ")"))
  902. goto partial;
  903. for (i = 0; i < field->nargs; i++)
  904. if (!trace_seq_printf(s, " %s=%lx",
  905. tp->args[i].name, field->args[i]))
  906. goto partial;
  907. if (!trace_seq_puts(s, "\n"))
  908. goto partial;
  909. return TRACE_TYPE_HANDLED;
  910. partial:
  911. return TRACE_TYPE_PARTIAL_LINE;
  912. }
  913. enum print_line_t
  914. print_kretprobe_event(struct trace_iterator *iter, int flags)
  915. {
  916. struct kretprobe_trace_entry *field;
  917. struct trace_seq *s = &iter->seq;
  918. struct trace_event *event;
  919. struct trace_probe *tp;
  920. int i;
  921. field = (struct kretprobe_trace_entry *)iter->ent;
  922. event = ftrace_find_event(field->ent.type);
  923. tp = container_of(event, struct trace_probe, event);
  924. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  925. goto partial;
  926. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  927. goto partial;
  928. if (!trace_seq_puts(s, " <- "))
  929. goto partial;
  930. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  931. goto partial;
  932. if (!trace_seq_puts(s, ")"))
  933. goto partial;
  934. for (i = 0; i < field->nargs; i++)
  935. if (!trace_seq_printf(s, " %s=%lx",
  936. tp->args[i].name, field->args[i]))
  937. goto partial;
  938. if (!trace_seq_puts(s, "\n"))
  939. goto partial;
  940. return TRACE_TYPE_HANDLED;
  941. partial:
  942. return TRACE_TYPE_PARTIAL_LINE;
  943. }
  944. static int probe_event_enable(struct ftrace_event_call *call)
  945. {
  946. struct trace_probe *tp = (struct trace_probe *)call->data;
  947. tp->flags |= TP_FLAG_TRACE;
  948. if (probe_is_return(tp))
  949. return enable_kretprobe(&tp->rp);
  950. else
  951. return enable_kprobe(&tp->rp.kp);
  952. }
  953. static void probe_event_disable(struct ftrace_event_call *call)
  954. {
  955. struct trace_probe *tp = (struct trace_probe *)call->data;
  956. tp->flags &= ~TP_FLAG_TRACE;
  957. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  958. if (probe_is_return(tp))
  959. disable_kretprobe(&tp->rp);
  960. else
  961. disable_kprobe(&tp->rp.kp);
  962. }
  963. }
  964. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  965. {
  966. INIT_LIST_HEAD(&event_call->fields);
  967. return 0;
  968. }
  969. #undef DEFINE_FIELD
  970. #define DEFINE_FIELD(type, item, name, is_signed) \
  971. do { \
  972. ret = trace_define_field(event_call, #type, name, \
  973. offsetof(typeof(field), item), \
  974. sizeof(field.item), is_signed, \
  975. FILTER_OTHER); \
  976. if (ret) \
  977. return ret; \
  978. } while (0)
  979. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  980. {
  981. int ret, i;
  982. struct kprobe_trace_entry field;
  983. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  984. ret = trace_define_common_fields(event_call);
  985. if (ret)
  986. return ret;
  987. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  988. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  989. /* Set argument names as fields */
  990. for (i = 0; i < tp->nr_args; i++)
  991. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  992. return 0;
  993. }
  994. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  995. {
  996. int ret, i;
  997. struct kretprobe_trace_entry field;
  998. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  999. ret = trace_define_common_fields(event_call);
  1000. if (ret)
  1001. return ret;
  1002. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1003. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1004. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  1005. /* Set argument names as fields */
  1006. for (i = 0; i < tp->nr_args; i++)
  1007. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  1008. return 0;
  1009. }
  1010. static int __probe_event_show_format(struct trace_seq *s,
  1011. struct trace_probe *tp, const char *fmt,
  1012. const char *arg)
  1013. {
  1014. int i;
  1015. /* Show format */
  1016. if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
  1017. return 0;
  1018. for (i = 0; i < tp->nr_args; i++)
  1019. if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
  1020. return 0;
  1021. if (!trace_seq_printf(s, "\", %s", arg))
  1022. return 0;
  1023. for (i = 0; i < tp->nr_args; i++)
  1024. if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
  1025. return 0;
  1026. return trace_seq_puts(s, "\n");
  1027. }
  1028. #undef SHOW_FIELD
  1029. #define SHOW_FIELD(type, item, name) \
  1030. do { \
  1031. ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
  1032. "offset:%u;\tsize:%u;\n", name, \
  1033. (unsigned int)offsetof(typeof(field), item),\
  1034. (unsigned int)sizeof(type)); \
  1035. if (!ret) \
  1036. return 0; \
  1037. } while (0)
  1038. static int kprobe_event_show_format(struct ftrace_event_call *call,
  1039. struct trace_seq *s)
  1040. {
  1041. struct kprobe_trace_entry field __attribute__((unused));
  1042. int ret, i;
  1043. struct trace_probe *tp = (struct trace_probe *)call->data;
  1044. SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
  1045. SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
  1046. /* Show fields */
  1047. for (i = 0; i < tp->nr_args; i++)
  1048. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  1049. trace_seq_puts(s, "\n");
  1050. return __probe_event_show_format(s, tp, "(%lx)",
  1051. "REC->" FIELD_STRING_IP);
  1052. }
  1053. static int kretprobe_event_show_format(struct ftrace_event_call *call,
  1054. struct trace_seq *s)
  1055. {
  1056. struct kretprobe_trace_entry field __attribute__((unused));
  1057. int ret, i;
  1058. struct trace_probe *tp = (struct trace_probe *)call->data;
  1059. SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
  1060. SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
  1061. SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
  1062. /* Show fields */
  1063. for (i = 0; i < tp->nr_args; i++)
  1064. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  1065. trace_seq_puts(s, "\n");
  1066. return __probe_event_show_format(s, tp, "(%lx <- %lx)",
  1067. "REC->" FIELD_STRING_FUNC
  1068. ", REC->" FIELD_STRING_RETIP);
  1069. }
  1070. #ifdef CONFIG_EVENT_PROFILE
  1071. /* Kprobe profile handler */
  1072. static __kprobes int kprobe_profile_func(struct kprobe *kp,
  1073. struct pt_regs *regs)
  1074. {
  1075. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1076. struct ftrace_event_call *call = &tp->call;
  1077. struct kprobe_trace_entry *entry;
  1078. struct trace_entry *ent;
  1079. int size, __size, i, pc, __cpu;
  1080. unsigned long irq_flags;
  1081. char *trace_buf;
  1082. char *raw_data;
  1083. int rctx;
  1084. pc = preempt_count();
  1085. __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  1086. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1087. size -= sizeof(u32);
  1088. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1089. "profile buffer not large enough"))
  1090. return 0;
  1091. /*
  1092. * Protect the non nmi buffer
  1093. * This also protects the rcu read side
  1094. */
  1095. local_irq_save(irq_flags);
  1096. rctx = perf_swevent_get_recursion_context();
  1097. if (rctx < 0)
  1098. goto end_recursion;
  1099. __cpu = smp_processor_id();
  1100. if (in_nmi())
  1101. trace_buf = rcu_dereference(perf_trace_buf_nmi);
  1102. else
  1103. trace_buf = rcu_dereference(perf_trace_buf);
  1104. if (!trace_buf)
  1105. goto end;
  1106. raw_data = per_cpu_ptr(trace_buf, __cpu);
  1107. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1108. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1109. entry = (struct kprobe_trace_entry *)raw_data;
  1110. ent = &entry->ent;
  1111. tracing_generic_entry_update(ent, irq_flags, pc);
  1112. ent->type = call->id;
  1113. entry->nargs = tp->nr_args;
  1114. entry->ip = (unsigned long)kp->addr;
  1115. for (i = 0; i < tp->nr_args; i++)
  1116. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1117. perf_tp_event(call->id, entry->ip, 1, entry, size);
  1118. end:
  1119. perf_swevent_put_recursion_context(rctx);
  1120. end_recursion:
  1121. local_irq_restore(irq_flags);
  1122. return 0;
  1123. }
  1124. /* Kretprobe profile handler */
  1125. static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
  1126. struct pt_regs *regs)
  1127. {
  1128. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1129. struct ftrace_event_call *call = &tp->call;
  1130. struct kretprobe_trace_entry *entry;
  1131. struct trace_entry *ent;
  1132. int size, __size, i, pc, __cpu;
  1133. unsigned long irq_flags;
  1134. char *trace_buf;
  1135. char *raw_data;
  1136. int rctx;
  1137. pc = preempt_count();
  1138. __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  1139. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1140. size -= sizeof(u32);
  1141. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1142. "profile buffer not large enough"))
  1143. return 0;
  1144. /*
  1145. * Protect the non nmi buffer
  1146. * This also protects the rcu read side
  1147. */
  1148. local_irq_save(irq_flags);
  1149. rctx = perf_swevent_get_recursion_context();
  1150. if (rctx < 0)
  1151. goto end_recursion;
  1152. __cpu = smp_processor_id();
  1153. if (in_nmi())
  1154. trace_buf = rcu_dereference(perf_trace_buf_nmi);
  1155. else
  1156. trace_buf = rcu_dereference(perf_trace_buf);
  1157. if (!trace_buf)
  1158. goto end;
  1159. raw_data = per_cpu_ptr(trace_buf, __cpu);
  1160. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1161. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1162. entry = (struct kretprobe_trace_entry *)raw_data;
  1163. ent = &entry->ent;
  1164. tracing_generic_entry_update(ent, irq_flags, pc);
  1165. ent->type = call->id;
  1166. entry->nargs = tp->nr_args;
  1167. entry->func = (unsigned long)tp->rp.kp.addr;
  1168. entry->ret_ip = (unsigned long)ri->ret_addr;
  1169. for (i = 0; i < tp->nr_args; i++)
  1170. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1171. perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
  1172. end:
  1173. perf_swevent_put_recursion_context(rctx);
  1174. end_recursion:
  1175. local_irq_restore(irq_flags);
  1176. return 0;
  1177. }
  1178. static int probe_profile_enable(struct ftrace_event_call *call)
  1179. {
  1180. struct trace_probe *tp = (struct trace_probe *)call->data;
  1181. tp->flags |= TP_FLAG_PROFILE;
  1182. if (probe_is_return(tp))
  1183. return enable_kretprobe(&tp->rp);
  1184. else
  1185. return enable_kprobe(&tp->rp.kp);
  1186. }
  1187. static void probe_profile_disable(struct ftrace_event_call *call)
  1188. {
  1189. struct trace_probe *tp = (struct trace_probe *)call->data;
  1190. tp->flags &= ~TP_FLAG_PROFILE;
  1191. if (!(tp->flags & TP_FLAG_TRACE)) {
  1192. if (probe_is_return(tp))
  1193. disable_kretprobe(&tp->rp);
  1194. else
  1195. disable_kprobe(&tp->rp.kp);
  1196. }
  1197. }
  1198. #endif /* CONFIG_EVENT_PROFILE */
  1199. static __kprobes
  1200. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1201. {
  1202. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1203. if (tp->flags & TP_FLAG_TRACE)
  1204. kprobe_trace_func(kp, regs);
  1205. #ifdef CONFIG_EVENT_PROFILE
  1206. if (tp->flags & TP_FLAG_PROFILE)
  1207. kprobe_profile_func(kp, regs);
  1208. #endif /* CONFIG_EVENT_PROFILE */
  1209. return 0; /* We don't tweek kernel, so just return 0 */
  1210. }
  1211. static __kprobes
  1212. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1213. {
  1214. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1215. if (tp->flags & TP_FLAG_TRACE)
  1216. kretprobe_trace_func(ri, regs);
  1217. #ifdef CONFIG_EVENT_PROFILE
  1218. if (tp->flags & TP_FLAG_PROFILE)
  1219. kretprobe_profile_func(ri, regs);
  1220. #endif /* CONFIG_EVENT_PROFILE */
  1221. return 0; /* We don't tweek kernel, so just return 0 */
  1222. }
  1223. static int register_probe_event(struct trace_probe *tp)
  1224. {
  1225. struct ftrace_event_call *call = &tp->call;
  1226. int ret;
  1227. /* Initialize ftrace_event_call */
  1228. if (probe_is_return(tp)) {
  1229. tp->event.trace = print_kretprobe_event;
  1230. call->raw_init = probe_event_raw_init;
  1231. call->show_format = kretprobe_event_show_format;
  1232. call->define_fields = kretprobe_event_define_fields;
  1233. } else {
  1234. tp->event.trace = print_kprobe_event;
  1235. call->raw_init = probe_event_raw_init;
  1236. call->show_format = kprobe_event_show_format;
  1237. call->define_fields = kprobe_event_define_fields;
  1238. }
  1239. call->event = &tp->event;
  1240. call->id = register_ftrace_event(&tp->event);
  1241. if (!call->id)
  1242. return -ENODEV;
  1243. call->enabled = 0;
  1244. call->regfunc = probe_event_enable;
  1245. call->unregfunc = probe_event_disable;
  1246. #ifdef CONFIG_EVENT_PROFILE
  1247. atomic_set(&call->profile_count, -1);
  1248. call->profile_enable = probe_profile_enable;
  1249. call->profile_disable = probe_profile_disable;
  1250. #endif
  1251. call->data = tp;
  1252. ret = trace_add_event_call(call);
  1253. if (ret) {
  1254. pr_info("Failed to register kprobe event: %s\n", call->name);
  1255. unregister_ftrace_event(&tp->event);
  1256. }
  1257. return ret;
  1258. }
  1259. static void unregister_probe_event(struct trace_probe *tp)
  1260. {
  1261. /* tp->event is unregistered in trace_remove_event_call() */
  1262. trace_remove_event_call(&tp->call);
  1263. }
  1264. /* Make a debugfs interface for controling probe points */
  1265. static __init int init_kprobe_trace(void)
  1266. {
  1267. struct dentry *d_tracer;
  1268. struct dentry *entry;
  1269. d_tracer = tracing_init_dentry();
  1270. if (!d_tracer)
  1271. return 0;
  1272. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1273. NULL, &kprobe_events_ops);
  1274. /* Event list interface */
  1275. if (!entry)
  1276. pr_warning("Could not create debugfs "
  1277. "'kprobe_events' entry\n");
  1278. /* Profile interface */
  1279. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1280. NULL, &kprobe_profile_ops);
  1281. if (!entry)
  1282. pr_warning("Could not create debugfs "
  1283. "'kprobe_profile' entry\n");
  1284. return 0;
  1285. }
  1286. fs_initcall(init_kprobe_trace);
  1287. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1288. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1289. int a4, int a5, int a6)
  1290. {
  1291. return a1 + a2 + a3 + a4 + a5 + a6;
  1292. }
  1293. static __init int kprobe_trace_self_tests_init(void)
  1294. {
  1295. int ret;
  1296. int (*target)(int, int, int, int, int, int);
  1297. target = kprobe_trace_selftest_target;
  1298. pr_info("Testing kprobe tracing: ");
  1299. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1300. "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
  1301. if (WARN_ON_ONCE(ret))
  1302. pr_warning("error enabling function entry\n");
  1303. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1304. "$retval");
  1305. if (WARN_ON_ONCE(ret))
  1306. pr_warning("error enabling function return\n");
  1307. ret = target(1, 2, 3, 4, 5, 6);
  1308. cleanup_all_probes();
  1309. pr_cont("OK\n");
  1310. return 0;
  1311. }
  1312. late_initcall(kprobe_trace_self_tests_init);
  1313. #endif