trace_kprobe.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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 "trace_probe.h"
  22. #define KPROBE_EVENT_SYSTEM "kprobes"
  23. /**
  24. * Kprobe event core functions
  25. */
  26. struct trace_kprobe {
  27. struct list_head list;
  28. struct kretprobe rp; /* Use rp.kp for kprobe use */
  29. unsigned long nhit;
  30. const char *symbol; /* symbol name */
  31. struct trace_probe tp;
  32. };
  33. #define SIZEOF_TRACE_KPROBE(n) \
  34. (offsetof(struct trace_kprobe, tp.args) + \
  35. (sizeof(struct probe_arg) * (n)))
  36. static __kprobes bool trace_kprobe_is_return(struct trace_kprobe *tk)
  37. {
  38. return tk->rp.handler != NULL;
  39. }
  40. static __kprobes const char *trace_kprobe_symbol(struct trace_kprobe *tk)
  41. {
  42. return tk->symbol ? tk->symbol : "unknown";
  43. }
  44. static __kprobes unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
  45. {
  46. return tk->rp.kp.offset;
  47. }
  48. static __kprobes bool trace_kprobe_has_gone(struct trace_kprobe *tk)
  49. {
  50. return !!(kprobe_gone(&tk->rp.kp));
  51. }
  52. static __kprobes bool trace_kprobe_within_module(struct trace_kprobe *tk,
  53. struct module *mod)
  54. {
  55. int len = strlen(mod->name);
  56. const char *name = trace_kprobe_symbol(tk);
  57. return strncmp(mod->name, name, len) == 0 && name[len] == ':';
  58. }
  59. static __kprobes bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
  60. {
  61. return !!strchr(trace_kprobe_symbol(tk), ':');
  62. }
  63. static int register_kprobe_event(struct trace_kprobe *tk);
  64. static int unregister_kprobe_event(struct trace_kprobe *tk);
  65. static DEFINE_MUTEX(probe_lock);
  66. static LIST_HEAD(probe_list);
  67. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  68. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  69. struct pt_regs *regs);
  70. /* Memory fetching by symbol */
  71. struct symbol_cache {
  72. char *symbol;
  73. long offset;
  74. unsigned long addr;
  75. };
  76. unsigned long update_symbol_cache(struct symbol_cache *sc)
  77. {
  78. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  79. if (sc->addr)
  80. sc->addr += sc->offset;
  81. return sc->addr;
  82. }
  83. void free_symbol_cache(struct symbol_cache *sc)
  84. {
  85. kfree(sc->symbol);
  86. kfree(sc);
  87. }
  88. struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  89. {
  90. struct symbol_cache *sc;
  91. if (!sym || strlen(sym) == 0)
  92. return NULL;
  93. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  94. if (!sc)
  95. return NULL;
  96. sc->symbol = kstrdup(sym, GFP_KERNEL);
  97. if (!sc->symbol) {
  98. kfree(sc);
  99. return NULL;
  100. }
  101. sc->offset = offset;
  102. update_symbol_cache(sc);
  103. return sc;
  104. }
  105. /*
  106. * Kprobes-specific fetch functions
  107. */
  108. #define DEFINE_FETCH_stack(type) \
  109. static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
  110. void *offset, void *dest) \
  111. { \
  112. *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
  113. (unsigned int)((unsigned long)offset)); \
  114. }
  115. DEFINE_BASIC_FETCH_FUNCS(stack)
  116. /* No string on the stack entry */
  117. #define fetch_stack_string NULL
  118. #define fetch_stack_string_size NULL
  119. #define DEFINE_FETCH_memory(type) \
  120. static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
  121. void *addr, void *dest) \
  122. { \
  123. type retval; \
  124. if (probe_kernel_address(addr, retval)) \
  125. *(type *)dest = 0; \
  126. else \
  127. *(type *)dest = retval; \
  128. }
  129. DEFINE_BASIC_FETCH_FUNCS(memory)
  130. /*
  131. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  132. * length and relative data location.
  133. */
  134. static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  135. void *addr, void *dest)
  136. {
  137. long ret;
  138. int maxlen = get_rloc_len(*(u32 *)dest);
  139. u8 *dst = get_rloc_data(dest);
  140. u8 *src = addr;
  141. mm_segment_t old_fs = get_fs();
  142. if (!maxlen)
  143. return;
  144. /*
  145. * Try to get string again, since the string can be changed while
  146. * probing.
  147. */
  148. set_fs(KERNEL_DS);
  149. pagefault_disable();
  150. do
  151. ret = __copy_from_user_inatomic(dst++, src++, 1);
  152. while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
  153. dst[-1] = '\0';
  154. pagefault_enable();
  155. set_fs(old_fs);
  156. if (ret < 0) { /* Failed to fetch string */
  157. ((u8 *)get_rloc_data(dest))[0] = '\0';
  158. *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
  159. } else {
  160. *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
  161. get_rloc_offs(*(u32 *)dest));
  162. }
  163. }
  164. /* Return the length of string -- including null terminal byte */
  165. static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
  166. void *addr, void *dest)
  167. {
  168. mm_segment_t old_fs;
  169. int ret, len = 0;
  170. u8 c;
  171. old_fs = get_fs();
  172. set_fs(KERNEL_DS);
  173. pagefault_disable();
  174. do {
  175. ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
  176. len++;
  177. } while (c && ret == 0 && len < MAX_STRING_SIZE);
  178. pagefault_enable();
  179. set_fs(old_fs);
  180. if (ret < 0) /* Failed to check the length */
  181. *(u32 *)dest = 0;
  182. else
  183. *(u32 *)dest = len;
  184. }
  185. #define DEFINE_FETCH_symbol(type) \
  186. __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, \
  187. void *data, void *dest) \
  188. { \
  189. struct symbol_cache *sc = data; \
  190. if (sc->addr) \
  191. fetch_memory_##type(regs, (void *)sc->addr, dest); \
  192. else \
  193. *(type *)dest = 0; \
  194. }
  195. DEFINE_BASIC_FETCH_FUNCS(symbol)
  196. DEFINE_FETCH_symbol(string)
  197. DEFINE_FETCH_symbol(string_size)
  198. /* kprobes don't support file_offset fetch methods */
  199. #define fetch_file_offset_u8 NULL
  200. #define fetch_file_offset_u16 NULL
  201. #define fetch_file_offset_u32 NULL
  202. #define fetch_file_offset_u64 NULL
  203. #define fetch_file_offset_string NULL
  204. #define fetch_file_offset_string_size NULL
  205. /* Fetch type information table */
  206. const struct fetch_type kprobes_fetch_type_table[] = {
  207. /* Special types */
  208. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  209. sizeof(u32), 1, "__data_loc char[]"),
  210. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  211. string_size, sizeof(u32), 0, "u32"),
  212. /* Basic types */
  213. ASSIGN_FETCH_TYPE(u8, u8, 0),
  214. ASSIGN_FETCH_TYPE(u16, u16, 0),
  215. ASSIGN_FETCH_TYPE(u32, u32, 0),
  216. ASSIGN_FETCH_TYPE(u64, u64, 0),
  217. ASSIGN_FETCH_TYPE(s8, u8, 1),
  218. ASSIGN_FETCH_TYPE(s16, u16, 1),
  219. ASSIGN_FETCH_TYPE(s32, u32, 1),
  220. ASSIGN_FETCH_TYPE(s64, u64, 1),
  221. ASSIGN_FETCH_TYPE_END
  222. };
  223. /*
  224. * Allocate new trace_probe and initialize it (including kprobes).
  225. */
  226. static struct trace_kprobe *alloc_trace_kprobe(const char *group,
  227. const char *event,
  228. void *addr,
  229. const char *symbol,
  230. unsigned long offs,
  231. int nargs, bool is_return)
  232. {
  233. struct trace_kprobe *tk;
  234. int ret = -ENOMEM;
  235. tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
  236. if (!tk)
  237. return ERR_PTR(ret);
  238. if (symbol) {
  239. tk->symbol = kstrdup(symbol, GFP_KERNEL);
  240. if (!tk->symbol)
  241. goto error;
  242. tk->rp.kp.symbol_name = tk->symbol;
  243. tk->rp.kp.offset = offs;
  244. } else
  245. tk->rp.kp.addr = addr;
  246. if (is_return)
  247. tk->rp.handler = kretprobe_dispatcher;
  248. else
  249. tk->rp.kp.pre_handler = kprobe_dispatcher;
  250. if (!event || !is_good_name(event)) {
  251. ret = -EINVAL;
  252. goto error;
  253. }
  254. tk->tp.call.class = &tk->tp.class;
  255. tk->tp.call.name = kstrdup(event, GFP_KERNEL);
  256. if (!tk->tp.call.name)
  257. goto error;
  258. if (!group || !is_good_name(group)) {
  259. ret = -EINVAL;
  260. goto error;
  261. }
  262. tk->tp.class.system = kstrdup(group, GFP_KERNEL);
  263. if (!tk->tp.class.system)
  264. goto error;
  265. INIT_LIST_HEAD(&tk->list);
  266. INIT_LIST_HEAD(&tk->tp.files);
  267. return tk;
  268. error:
  269. kfree(tk->tp.call.name);
  270. kfree(tk->symbol);
  271. kfree(tk);
  272. return ERR_PTR(ret);
  273. }
  274. static void free_trace_kprobe(struct trace_kprobe *tk)
  275. {
  276. int i;
  277. for (i = 0; i < tk->tp.nr_args; i++)
  278. traceprobe_free_probe_arg(&tk->tp.args[i]);
  279. kfree(tk->tp.call.class->system);
  280. kfree(tk->tp.call.name);
  281. kfree(tk->symbol);
  282. kfree(tk);
  283. }
  284. static struct trace_kprobe *find_trace_kprobe(const char *event,
  285. const char *group)
  286. {
  287. struct trace_kprobe *tk;
  288. list_for_each_entry(tk, &probe_list, list)
  289. if (strcmp(ftrace_event_name(&tk->tp.call), event) == 0 &&
  290. strcmp(tk->tp.call.class->system, group) == 0)
  291. return tk;
  292. return NULL;
  293. }
  294. /*
  295. * Enable trace_probe
  296. * if the file is NULL, enable "perf" handler, or enable "trace" handler.
  297. */
  298. static int
  299. enable_trace_kprobe(struct trace_kprobe *tk, struct ftrace_event_file *file)
  300. {
  301. int ret = 0;
  302. if (file) {
  303. struct event_file_link *link;
  304. link = kmalloc(sizeof(*link), GFP_KERNEL);
  305. if (!link) {
  306. ret = -ENOMEM;
  307. goto out;
  308. }
  309. link->file = file;
  310. list_add_tail_rcu(&link->list, &tk->tp.files);
  311. tk->tp.flags |= TP_FLAG_TRACE;
  312. } else
  313. tk->tp.flags |= TP_FLAG_PROFILE;
  314. if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
  315. if (trace_kprobe_is_return(tk))
  316. ret = enable_kretprobe(&tk->rp);
  317. else
  318. ret = enable_kprobe(&tk->rp.kp);
  319. }
  320. out:
  321. return ret;
  322. }
  323. /*
  324. * Disable trace_probe
  325. * if the file is NULL, disable "perf" handler, or disable "trace" handler.
  326. */
  327. static int
  328. disable_trace_kprobe(struct trace_kprobe *tk, struct ftrace_event_file *file)
  329. {
  330. struct event_file_link *link = NULL;
  331. int wait = 0;
  332. int ret = 0;
  333. if (file) {
  334. link = find_event_file_link(&tk->tp, file);
  335. if (!link) {
  336. ret = -EINVAL;
  337. goto out;
  338. }
  339. list_del_rcu(&link->list);
  340. wait = 1;
  341. if (!list_empty(&tk->tp.files))
  342. goto out;
  343. tk->tp.flags &= ~TP_FLAG_TRACE;
  344. } else
  345. tk->tp.flags &= ~TP_FLAG_PROFILE;
  346. if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
  347. if (trace_kprobe_is_return(tk))
  348. disable_kretprobe(&tk->rp);
  349. else
  350. disable_kprobe(&tk->rp.kp);
  351. wait = 1;
  352. }
  353. out:
  354. if (wait) {
  355. /*
  356. * Synchronize with kprobe_trace_func/kretprobe_trace_func
  357. * to ensure disabled (all running handlers are finished).
  358. * This is not only for kfree(), but also the caller,
  359. * trace_remove_event_call() supposes it for releasing
  360. * event_call related objects, which will be accessed in
  361. * the kprobe_trace_func/kretprobe_trace_func.
  362. */
  363. synchronize_sched();
  364. kfree(link); /* Ignored if link == NULL */
  365. }
  366. return ret;
  367. }
  368. /* Internal register function - just handle k*probes and flags */
  369. static int __register_trace_kprobe(struct trace_kprobe *tk)
  370. {
  371. int i, ret;
  372. if (trace_probe_is_registered(&tk->tp))
  373. return -EINVAL;
  374. for (i = 0; i < tk->tp.nr_args; i++)
  375. traceprobe_update_arg(&tk->tp.args[i]);
  376. /* Set/clear disabled flag according to tp->flag */
  377. if (trace_probe_is_enabled(&tk->tp))
  378. tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  379. else
  380. tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  381. if (trace_kprobe_is_return(tk))
  382. ret = register_kretprobe(&tk->rp);
  383. else
  384. ret = register_kprobe(&tk->rp.kp);
  385. if (ret == 0)
  386. tk->tp.flags |= TP_FLAG_REGISTERED;
  387. else {
  388. pr_warning("Could not insert probe at %s+%lu: %d\n",
  389. trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
  390. if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
  391. pr_warning("This probe might be able to register after"
  392. "target module is loaded. Continue.\n");
  393. ret = 0;
  394. } else if (ret == -EILSEQ) {
  395. pr_warning("Probing address(0x%p) is not an "
  396. "instruction boundary.\n",
  397. tk->rp.kp.addr);
  398. ret = -EINVAL;
  399. }
  400. }
  401. return ret;
  402. }
  403. /* Internal unregister function - just handle k*probes and flags */
  404. static void __unregister_trace_kprobe(struct trace_kprobe *tk)
  405. {
  406. if (trace_probe_is_registered(&tk->tp)) {
  407. if (trace_kprobe_is_return(tk))
  408. unregister_kretprobe(&tk->rp);
  409. else
  410. unregister_kprobe(&tk->rp.kp);
  411. tk->tp.flags &= ~TP_FLAG_REGISTERED;
  412. /* Cleanup kprobe for reuse */
  413. if (tk->rp.kp.symbol_name)
  414. tk->rp.kp.addr = NULL;
  415. }
  416. }
  417. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  418. static int unregister_trace_kprobe(struct trace_kprobe *tk)
  419. {
  420. /* Enabled event can not be unregistered */
  421. if (trace_probe_is_enabled(&tk->tp))
  422. return -EBUSY;
  423. /* Will fail if probe is being used by ftrace or perf */
  424. if (unregister_kprobe_event(tk))
  425. return -EBUSY;
  426. __unregister_trace_kprobe(tk);
  427. list_del(&tk->list);
  428. return 0;
  429. }
  430. /* Register a trace_probe and probe_event */
  431. static int register_trace_kprobe(struct trace_kprobe *tk)
  432. {
  433. struct trace_kprobe *old_tk;
  434. int ret;
  435. mutex_lock(&probe_lock);
  436. /* Delete old (same name) event if exist */
  437. old_tk = find_trace_kprobe(ftrace_event_name(&tk->tp.call),
  438. tk->tp.call.class->system);
  439. if (old_tk) {
  440. ret = unregister_trace_kprobe(old_tk);
  441. if (ret < 0)
  442. goto end;
  443. free_trace_kprobe(old_tk);
  444. }
  445. /* Register new event */
  446. ret = register_kprobe_event(tk);
  447. if (ret) {
  448. pr_warning("Failed to register probe event(%d)\n", ret);
  449. goto end;
  450. }
  451. /* Register k*probe */
  452. ret = __register_trace_kprobe(tk);
  453. if (ret < 0)
  454. unregister_kprobe_event(tk);
  455. else
  456. list_add_tail(&tk->list, &probe_list);
  457. end:
  458. mutex_unlock(&probe_lock);
  459. return ret;
  460. }
  461. /* Module notifier call back, checking event on the module */
  462. static int trace_kprobe_module_callback(struct notifier_block *nb,
  463. unsigned long val, void *data)
  464. {
  465. struct module *mod = data;
  466. struct trace_kprobe *tk;
  467. int ret;
  468. if (val != MODULE_STATE_COMING)
  469. return NOTIFY_DONE;
  470. /* Update probes on coming module */
  471. mutex_lock(&probe_lock);
  472. list_for_each_entry(tk, &probe_list, list) {
  473. if (trace_kprobe_within_module(tk, mod)) {
  474. /* Don't need to check busy - this should have gone. */
  475. __unregister_trace_kprobe(tk);
  476. ret = __register_trace_kprobe(tk);
  477. if (ret)
  478. pr_warning("Failed to re-register probe %s on"
  479. "%s: %d\n",
  480. ftrace_event_name(&tk->tp.call),
  481. mod->name, ret);
  482. }
  483. }
  484. mutex_unlock(&probe_lock);
  485. return NOTIFY_DONE;
  486. }
  487. static struct notifier_block trace_kprobe_module_nb = {
  488. .notifier_call = trace_kprobe_module_callback,
  489. .priority = 1 /* Invoked after kprobe module callback */
  490. };
  491. static int create_trace_kprobe(int argc, char **argv)
  492. {
  493. /*
  494. * Argument syntax:
  495. * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  496. * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  497. * Fetch args:
  498. * $retval : fetch return value
  499. * $stack : fetch stack address
  500. * $stackN : fetch Nth of stack (N:0-)
  501. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  502. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  503. * %REG : fetch register REG
  504. * Dereferencing memory fetch:
  505. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  506. * Alias name of args:
  507. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  508. * Type of args:
  509. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  510. */
  511. struct trace_kprobe *tk;
  512. int i, ret = 0;
  513. bool is_return = false, is_delete = false;
  514. char *symbol = NULL, *event = NULL, *group = NULL;
  515. char *arg;
  516. unsigned long offset = 0;
  517. void *addr = NULL;
  518. char buf[MAX_EVENT_NAME_LEN];
  519. /* argc must be >= 1 */
  520. if (argv[0][0] == 'p')
  521. is_return = false;
  522. else if (argv[0][0] == 'r')
  523. is_return = true;
  524. else if (argv[0][0] == '-')
  525. is_delete = true;
  526. else {
  527. pr_info("Probe definition must be started with 'p', 'r' or"
  528. " '-'.\n");
  529. return -EINVAL;
  530. }
  531. if (argv[0][1] == ':') {
  532. event = &argv[0][2];
  533. if (strchr(event, '/')) {
  534. group = event;
  535. event = strchr(group, '/') + 1;
  536. event[-1] = '\0';
  537. if (strlen(group) == 0) {
  538. pr_info("Group name is not specified\n");
  539. return -EINVAL;
  540. }
  541. }
  542. if (strlen(event) == 0) {
  543. pr_info("Event name is not specified\n");
  544. return -EINVAL;
  545. }
  546. }
  547. if (!group)
  548. group = KPROBE_EVENT_SYSTEM;
  549. if (is_delete) {
  550. if (!event) {
  551. pr_info("Delete command needs an event name.\n");
  552. return -EINVAL;
  553. }
  554. mutex_lock(&probe_lock);
  555. tk = find_trace_kprobe(event, group);
  556. if (!tk) {
  557. mutex_unlock(&probe_lock);
  558. pr_info("Event %s/%s doesn't exist.\n", group, event);
  559. return -ENOENT;
  560. }
  561. /* delete an event */
  562. ret = unregister_trace_kprobe(tk);
  563. if (ret == 0)
  564. free_trace_kprobe(tk);
  565. mutex_unlock(&probe_lock);
  566. return ret;
  567. }
  568. if (argc < 2) {
  569. pr_info("Probe point is not specified.\n");
  570. return -EINVAL;
  571. }
  572. if (isdigit(argv[1][0])) {
  573. if (is_return) {
  574. pr_info("Return probe point must be a symbol.\n");
  575. return -EINVAL;
  576. }
  577. /* an address specified */
  578. ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
  579. if (ret) {
  580. pr_info("Failed to parse address.\n");
  581. return ret;
  582. }
  583. } else {
  584. /* a symbol specified */
  585. symbol = argv[1];
  586. /* TODO: support .init module functions */
  587. ret = traceprobe_split_symbol_offset(symbol, &offset);
  588. if (ret) {
  589. pr_info("Failed to parse symbol.\n");
  590. return ret;
  591. }
  592. if (offset && is_return) {
  593. pr_info("Return probe must be used without offset.\n");
  594. return -EINVAL;
  595. }
  596. }
  597. argc -= 2; argv += 2;
  598. /* setup a probe */
  599. if (!event) {
  600. /* Make a new event name */
  601. if (symbol)
  602. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  603. is_return ? 'r' : 'p', symbol, offset);
  604. else
  605. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  606. is_return ? 'r' : 'p', addr);
  607. event = buf;
  608. }
  609. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc,
  610. is_return);
  611. if (IS_ERR(tk)) {
  612. pr_info("Failed to allocate trace_probe.(%d)\n",
  613. (int)PTR_ERR(tk));
  614. return PTR_ERR(tk);
  615. }
  616. /* parse arguments */
  617. ret = 0;
  618. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  619. struct probe_arg *parg = &tk->tp.args[i];
  620. /* Increment count for freeing args in error case */
  621. tk->tp.nr_args++;
  622. /* Parse argument name */
  623. arg = strchr(argv[i], '=');
  624. if (arg) {
  625. *arg++ = '\0';
  626. parg->name = kstrdup(argv[i], GFP_KERNEL);
  627. } else {
  628. arg = argv[i];
  629. /* If argument name is omitted, set "argN" */
  630. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  631. parg->name = kstrdup(buf, GFP_KERNEL);
  632. }
  633. if (!parg->name) {
  634. pr_info("Failed to allocate argument[%d] name.\n", i);
  635. ret = -ENOMEM;
  636. goto error;
  637. }
  638. if (!is_good_name(parg->name)) {
  639. pr_info("Invalid argument[%d] name: %s\n",
  640. i, parg->name);
  641. ret = -EINVAL;
  642. goto error;
  643. }
  644. if (traceprobe_conflict_field_name(parg->name,
  645. tk->tp.args, i)) {
  646. pr_info("Argument[%d] name '%s' conflicts with "
  647. "another field.\n", i, argv[i]);
  648. ret = -EINVAL;
  649. goto error;
  650. }
  651. /* Parse fetch argument */
  652. ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
  653. is_return, true);
  654. if (ret) {
  655. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  656. goto error;
  657. }
  658. }
  659. ret = register_trace_kprobe(tk);
  660. if (ret)
  661. goto error;
  662. return 0;
  663. error:
  664. free_trace_kprobe(tk);
  665. return ret;
  666. }
  667. static int release_all_trace_kprobes(void)
  668. {
  669. struct trace_kprobe *tk;
  670. int ret = 0;
  671. mutex_lock(&probe_lock);
  672. /* Ensure no probe is in use. */
  673. list_for_each_entry(tk, &probe_list, list)
  674. if (trace_probe_is_enabled(&tk->tp)) {
  675. ret = -EBUSY;
  676. goto end;
  677. }
  678. /* TODO: Use batch unregistration */
  679. while (!list_empty(&probe_list)) {
  680. tk = list_entry(probe_list.next, struct trace_kprobe, list);
  681. ret = unregister_trace_kprobe(tk);
  682. if (ret)
  683. goto end;
  684. free_trace_kprobe(tk);
  685. }
  686. end:
  687. mutex_unlock(&probe_lock);
  688. return ret;
  689. }
  690. /* Probes listing interfaces */
  691. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  692. {
  693. mutex_lock(&probe_lock);
  694. return seq_list_start(&probe_list, *pos);
  695. }
  696. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  697. {
  698. return seq_list_next(v, &probe_list, pos);
  699. }
  700. static void probes_seq_stop(struct seq_file *m, void *v)
  701. {
  702. mutex_unlock(&probe_lock);
  703. }
  704. static int probes_seq_show(struct seq_file *m, void *v)
  705. {
  706. struct trace_kprobe *tk = v;
  707. int i;
  708. seq_printf(m, "%c", trace_kprobe_is_return(tk) ? 'r' : 'p');
  709. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  710. ftrace_event_name(&tk->tp.call));
  711. if (!tk->symbol)
  712. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  713. else if (tk->rp.kp.offset)
  714. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  715. tk->rp.kp.offset);
  716. else
  717. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  718. for (i = 0; i < tk->tp.nr_args; i++)
  719. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  720. seq_printf(m, "\n");
  721. return 0;
  722. }
  723. static const struct seq_operations probes_seq_op = {
  724. .start = probes_seq_start,
  725. .next = probes_seq_next,
  726. .stop = probes_seq_stop,
  727. .show = probes_seq_show
  728. };
  729. static int probes_open(struct inode *inode, struct file *file)
  730. {
  731. int ret;
  732. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  733. ret = release_all_trace_kprobes();
  734. if (ret < 0)
  735. return ret;
  736. }
  737. return seq_open(file, &probes_seq_op);
  738. }
  739. static ssize_t probes_write(struct file *file, const char __user *buffer,
  740. size_t count, loff_t *ppos)
  741. {
  742. return traceprobe_probes_write(file, buffer, count, ppos,
  743. create_trace_kprobe);
  744. }
  745. static const struct file_operations kprobe_events_ops = {
  746. .owner = THIS_MODULE,
  747. .open = probes_open,
  748. .read = seq_read,
  749. .llseek = seq_lseek,
  750. .release = seq_release,
  751. .write = probes_write,
  752. };
  753. /* Probes profiling interfaces */
  754. static int probes_profile_seq_show(struct seq_file *m, void *v)
  755. {
  756. struct trace_kprobe *tk = v;
  757. seq_printf(m, " %-44s %15lu %15lu\n",
  758. ftrace_event_name(&tk->tp.call), tk->nhit,
  759. tk->rp.kp.nmissed);
  760. return 0;
  761. }
  762. static const struct seq_operations profile_seq_op = {
  763. .start = probes_seq_start,
  764. .next = probes_seq_next,
  765. .stop = probes_seq_stop,
  766. .show = probes_profile_seq_show
  767. };
  768. static int profile_open(struct inode *inode, struct file *file)
  769. {
  770. return seq_open(file, &profile_seq_op);
  771. }
  772. static const struct file_operations kprobe_profile_ops = {
  773. .owner = THIS_MODULE,
  774. .open = profile_open,
  775. .read = seq_read,
  776. .llseek = seq_lseek,
  777. .release = seq_release,
  778. };
  779. /* Kprobe handler */
  780. static __kprobes void
  781. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  782. struct ftrace_event_file *ftrace_file)
  783. {
  784. struct kprobe_trace_entry_head *entry;
  785. struct ring_buffer_event *event;
  786. struct ring_buffer *buffer;
  787. int size, dsize, pc;
  788. unsigned long irq_flags;
  789. struct ftrace_event_call *call = &tk->tp.call;
  790. WARN_ON(call != ftrace_file->event_call);
  791. if (ftrace_trigger_soft_disabled(ftrace_file))
  792. return;
  793. local_save_flags(irq_flags);
  794. pc = preempt_count();
  795. dsize = __get_data_size(&tk->tp, regs);
  796. size = sizeof(*entry) + tk->tp.size + dsize;
  797. event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
  798. call->event.type,
  799. size, irq_flags, pc);
  800. if (!event)
  801. return;
  802. entry = ring_buffer_event_data(event);
  803. entry->ip = (unsigned long)tk->rp.kp.addr;
  804. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  805. event_trigger_unlock_commit_regs(ftrace_file, buffer, event,
  806. entry, irq_flags, pc, regs);
  807. }
  808. static __kprobes void
  809. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  810. {
  811. struct event_file_link *link;
  812. list_for_each_entry_rcu(link, &tk->tp.files, list)
  813. __kprobe_trace_func(tk, regs, link->file);
  814. }
  815. /* Kretprobe handler */
  816. static __kprobes void
  817. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  818. struct pt_regs *regs,
  819. struct ftrace_event_file *ftrace_file)
  820. {
  821. struct kretprobe_trace_entry_head *entry;
  822. struct ring_buffer_event *event;
  823. struct ring_buffer *buffer;
  824. int size, pc, dsize;
  825. unsigned long irq_flags;
  826. struct ftrace_event_call *call = &tk->tp.call;
  827. WARN_ON(call != ftrace_file->event_call);
  828. if (ftrace_trigger_soft_disabled(ftrace_file))
  829. return;
  830. local_save_flags(irq_flags);
  831. pc = preempt_count();
  832. dsize = __get_data_size(&tk->tp, regs);
  833. size = sizeof(*entry) + tk->tp.size + dsize;
  834. event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
  835. call->event.type,
  836. size, irq_flags, pc);
  837. if (!event)
  838. return;
  839. entry = ring_buffer_event_data(event);
  840. entry->func = (unsigned long)tk->rp.kp.addr;
  841. entry->ret_ip = (unsigned long)ri->ret_addr;
  842. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  843. event_trigger_unlock_commit_regs(ftrace_file, buffer, event,
  844. entry, irq_flags, pc, regs);
  845. }
  846. static __kprobes void
  847. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  848. struct pt_regs *regs)
  849. {
  850. struct event_file_link *link;
  851. list_for_each_entry_rcu(link, &tk->tp.files, list)
  852. __kretprobe_trace_func(tk, ri, regs, link->file);
  853. }
  854. /* Event entry printers */
  855. static enum print_line_t
  856. print_kprobe_event(struct trace_iterator *iter, int flags,
  857. struct trace_event *event)
  858. {
  859. struct kprobe_trace_entry_head *field;
  860. struct trace_seq *s = &iter->seq;
  861. struct trace_probe *tp;
  862. u8 *data;
  863. int i;
  864. field = (struct kprobe_trace_entry_head *)iter->ent;
  865. tp = container_of(event, struct trace_probe, call.event);
  866. if (!trace_seq_printf(s, "%s: (", ftrace_event_name(&tp->call)))
  867. goto partial;
  868. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  869. goto partial;
  870. if (!trace_seq_puts(s, ")"))
  871. goto partial;
  872. data = (u8 *)&field[1];
  873. for (i = 0; i < tp->nr_args; i++)
  874. if (!tp->args[i].type->print(s, tp->args[i].name,
  875. data + tp->args[i].offset, field))
  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. static enum print_line_t
  884. print_kretprobe_event(struct trace_iterator *iter, int flags,
  885. struct trace_event *event)
  886. {
  887. struct kretprobe_trace_entry_head *field;
  888. struct trace_seq *s = &iter->seq;
  889. struct trace_probe *tp;
  890. u8 *data;
  891. int i;
  892. field = (struct kretprobe_trace_entry_head *)iter->ent;
  893. tp = container_of(event, struct trace_probe, call.event);
  894. if (!trace_seq_printf(s, "%s: (", ftrace_event_name(&tp->call)))
  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. data = (u8 *)&field[1];
  905. for (i = 0; i < tp->nr_args; i++)
  906. if (!tp->args[i].type->print(s, tp->args[i].name,
  907. data + tp->args[i].offset, field))
  908. goto partial;
  909. if (!trace_seq_puts(s, "\n"))
  910. goto partial;
  911. return TRACE_TYPE_HANDLED;
  912. partial:
  913. return TRACE_TYPE_PARTIAL_LINE;
  914. }
  915. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  916. {
  917. int ret, i;
  918. struct kprobe_trace_entry_head field;
  919. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  920. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  921. /* Set argument names as fields */
  922. for (i = 0; i < tk->tp.nr_args; i++) {
  923. struct probe_arg *parg = &tk->tp.args[i];
  924. ret = trace_define_field(event_call, parg->type->fmttype,
  925. parg->name,
  926. sizeof(field) + parg->offset,
  927. parg->type->size,
  928. parg->type->is_signed,
  929. FILTER_OTHER);
  930. if (ret)
  931. return ret;
  932. }
  933. return 0;
  934. }
  935. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  936. {
  937. int ret, i;
  938. struct kretprobe_trace_entry_head field;
  939. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  940. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  941. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  942. /* Set argument names as fields */
  943. for (i = 0; i < tk->tp.nr_args; i++) {
  944. struct probe_arg *parg = &tk->tp.args[i];
  945. ret = trace_define_field(event_call, parg->type->fmttype,
  946. parg->name,
  947. sizeof(field) + parg->offset,
  948. parg->type->size,
  949. parg->type->is_signed,
  950. FILTER_OTHER);
  951. if (ret)
  952. return ret;
  953. }
  954. return 0;
  955. }
  956. #ifdef CONFIG_PERF_EVENTS
  957. /* Kprobe profile handler */
  958. static __kprobes void
  959. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  960. {
  961. struct ftrace_event_call *call = &tk->tp.call;
  962. struct kprobe_trace_entry_head *entry;
  963. struct hlist_head *head;
  964. int size, __size, dsize;
  965. int rctx;
  966. head = this_cpu_ptr(call->perf_events);
  967. if (hlist_empty(head))
  968. return;
  969. dsize = __get_data_size(&tk->tp, regs);
  970. __size = sizeof(*entry) + tk->tp.size + dsize;
  971. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  972. size -= sizeof(u32);
  973. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  974. if (!entry)
  975. return;
  976. entry->ip = (unsigned long)tk->rp.kp.addr;
  977. memset(&entry[1], 0, dsize);
  978. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  979. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  980. }
  981. /* Kretprobe profile handler */
  982. static __kprobes void
  983. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  984. struct pt_regs *regs)
  985. {
  986. struct ftrace_event_call *call = &tk->tp.call;
  987. struct kretprobe_trace_entry_head *entry;
  988. struct hlist_head *head;
  989. int size, __size, dsize;
  990. int rctx;
  991. head = this_cpu_ptr(call->perf_events);
  992. if (hlist_empty(head))
  993. return;
  994. dsize = __get_data_size(&tk->tp, regs);
  995. __size = sizeof(*entry) + tk->tp.size + dsize;
  996. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  997. size -= sizeof(u32);
  998. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  999. if (!entry)
  1000. return;
  1001. entry->func = (unsigned long)tk->rp.kp.addr;
  1002. entry->ret_ip = (unsigned long)ri->ret_addr;
  1003. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1004. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  1005. }
  1006. #endif /* CONFIG_PERF_EVENTS */
  1007. /*
  1008. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1009. *
  1010. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1011. * lockless, but we can't race with this __init function.
  1012. */
  1013. static __kprobes
  1014. int kprobe_register(struct ftrace_event_call *event,
  1015. enum trace_reg type, void *data)
  1016. {
  1017. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1018. struct ftrace_event_file *file = data;
  1019. switch (type) {
  1020. case TRACE_REG_REGISTER:
  1021. return enable_trace_kprobe(tk, file);
  1022. case TRACE_REG_UNREGISTER:
  1023. return disable_trace_kprobe(tk, file);
  1024. #ifdef CONFIG_PERF_EVENTS
  1025. case TRACE_REG_PERF_REGISTER:
  1026. return enable_trace_kprobe(tk, NULL);
  1027. case TRACE_REG_PERF_UNREGISTER:
  1028. return disable_trace_kprobe(tk, NULL);
  1029. case TRACE_REG_PERF_OPEN:
  1030. case TRACE_REG_PERF_CLOSE:
  1031. case TRACE_REG_PERF_ADD:
  1032. case TRACE_REG_PERF_DEL:
  1033. return 0;
  1034. #endif
  1035. }
  1036. return 0;
  1037. }
  1038. static __kprobes
  1039. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1040. {
  1041. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1042. tk->nhit++;
  1043. if (tk->tp.flags & TP_FLAG_TRACE)
  1044. kprobe_trace_func(tk, regs);
  1045. #ifdef CONFIG_PERF_EVENTS
  1046. if (tk->tp.flags & TP_FLAG_PROFILE)
  1047. kprobe_perf_func(tk, regs);
  1048. #endif
  1049. return 0; /* We don't tweek kernel, so just return 0 */
  1050. }
  1051. static __kprobes
  1052. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1053. {
  1054. struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
  1055. tk->nhit++;
  1056. if (tk->tp.flags & TP_FLAG_TRACE)
  1057. kretprobe_trace_func(tk, ri, regs);
  1058. #ifdef CONFIG_PERF_EVENTS
  1059. if (tk->tp.flags & TP_FLAG_PROFILE)
  1060. kretprobe_perf_func(tk, ri, regs);
  1061. #endif
  1062. return 0; /* We don't tweek kernel, so just return 0 */
  1063. }
  1064. static struct trace_event_functions kretprobe_funcs = {
  1065. .trace = print_kretprobe_event
  1066. };
  1067. static struct trace_event_functions kprobe_funcs = {
  1068. .trace = print_kprobe_event
  1069. };
  1070. static int register_kprobe_event(struct trace_kprobe *tk)
  1071. {
  1072. struct ftrace_event_call *call = &tk->tp.call;
  1073. int ret;
  1074. /* Initialize ftrace_event_call */
  1075. INIT_LIST_HEAD(&call->class->fields);
  1076. if (trace_kprobe_is_return(tk)) {
  1077. call->event.funcs = &kretprobe_funcs;
  1078. call->class->define_fields = kretprobe_event_define_fields;
  1079. } else {
  1080. call->event.funcs = &kprobe_funcs;
  1081. call->class->define_fields = kprobe_event_define_fields;
  1082. }
  1083. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1084. return -ENOMEM;
  1085. ret = register_ftrace_event(&call->event);
  1086. if (!ret) {
  1087. kfree(call->print_fmt);
  1088. return -ENODEV;
  1089. }
  1090. call->flags = 0;
  1091. call->class->reg = kprobe_register;
  1092. call->data = tk;
  1093. ret = trace_add_event_call(call);
  1094. if (ret) {
  1095. pr_info("Failed to register kprobe event: %s\n",
  1096. ftrace_event_name(call));
  1097. kfree(call->print_fmt);
  1098. unregister_ftrace_event(&call->event);
  1099. }
  1100. return ret;
  1101. }
  1102. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1103. {
  1104. int ret;
  1105. /* tp->event is unregistered in trace_remove_event_call() */
  1106. ret = trace_remove_event_call(&tk->tp.call);
  1107. if (!ret)
  1108. kfree(tk->tp.call.print_fmt);
  1109. return ret;
  1110. }
  1111. /* Make a debugfs interface for controlling probe points */
  1112. static __init int init_kprobe_trace(void)
  1113. {
  1114. struct dentry *d_tracer;
  1115. struct dentry *entry;
  1116. if (register_module_notifier(&trace_kprobe_module_nb))
  1117. return -EINVAL;
  1118. d_tracer = tracing_init_dentry();
  1119. if (!d_tracer)
  1120. return 0;
  1121. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1122. NULL, &kprobe_events_ops);
  1123. /* Event list interface */
  1124. if (!entry)
  1125. pr_warning("Could not create debugfs "
  1126. "'kprobe_events' entry\n");
  1127. /* Profile interface */
  1128. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1129. NULL, &kprobe_profile_ops);
  1130. if (!entry)
  1131. pr_warning("Could not create debugfs "
  1132. "'kprobe_profile' entry\n");
  1133. return 0;
  1134. }
  1135. fs_initcall(init_kprobe_trace);
  1136. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1137. /*
  1138. * The "__used" keeps gcc from removing the function symbol
  1139. * from the kallsyms table.
  1140. */
  1141. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1142. int a4, int a5, int a6)
  1143. {
  1144. return a1 + a2 + a3 + a4 + a5 + a6;
  1145. }
  1146. static struct ftrace_event_file *
  1147. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1148. {
  1149. struct ftrace_event_file *file;
  1150. list_for_each_entry(file, &tr->events, list)
  1151. if (file->event_call == &tk->tp.call)
  1152. return file;
  1153. return NULL;
  1154. }
  1155. /*
  1156. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1157. * stage, we can do this lockless.
  1158. */
  1159. static __init int kprobe_trace_self_tests_init(void)
  1160. {
  1161. int ret, warn = 0;
  1162. int (*target)(int, int, int, int, int, int);
  1163. struct trace_kprobe *tk;
  1164. struct ftrace_event_file *file;
  1165. target = kprobe_trace_selftest_target;
  1166. pr_info("Testing kprobe tracing: ");
  1167. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1168. "$stack $stack0 +0($stack)",
  1169. create_trace_kprobe);
  1170. if (WARN_ON_ONCE(ret)) {
  1171. pr_warn("error on probing function entry.\n");
  1172. warn++;
  1173. } else {
  1174. /* Enable trace point */
  1175. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1176. if (WARN_ON_ONCE(tk == NULL)) {
  1177. pr_warn("error on getting new probe.\n");
  1178. warn++;
  1179. } else {
  1180. file = find_trace_probe_file(tk, top_trace_array());
  1181. if (WARN_ON_ONCE(file == NULL)) {
  1182. pr_warn("error on getting probe file.\n");
  1183. warn++;
  1184. } else
  1185. enable_trace_kprobe(tk, file);
  1186. }
  1187. }
  1188. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1189. "$retval", create_trace_kprobe);
  1190. if (WARN_ON_ONCE(ret)) {
  1191. pr_warn("error on probing function return.\n");
  1192. warn++;
  1193. } else {
  1194. /* Enable trace point */
  1195. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1196. if (WARN_ON_ONCE(tk == NULL)) {
  1197. pr_warn("error on getting 2nd new probe.\n");
  1198. warn++;
  1199. } else {
  1200. file = find_trace_probe_file(tk, top_trace_array());
  1201. if (WARN_ON_ONCE(file == NULL)) {
  1202. pr_warn("error on getting probe file.\n");
  1203. warn++;
  1204. } else
  1205. enable_trace_kprobe(tk, file);
  1206. }
  1207. }
  1208. if (warn)
  1209. goto end;
  1210. ret = target(1, 2, 3, 4, 5, 6);
  1211. /* Disable trace points before removing it */
  1212. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1213. if (WARN_ON_ONCE(tk == NULL)) {
  1214. pr_warn("error on getting test probe.\n");
  1215. warn++;
  1216. } else {
  1217. file = find_trace_probe_file(tk, top_trace_array());
  1218. if (WARN_ON_ONCE(file == NULL)) {
  1219. pr_warn("error on getting probe file.\n");
  1220. warn++;
  1221. } else
  1222. disable_trace_kprobe(tk, file);
  1223. }
  1224. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1225. if (WARN_ON_ONCE(tk == NULL)) {
  1226. pr_warn("error on getting 2nd test probe.\n");
  1227. warn++;
  1228. } else {
  1229. file = find_trace_probe_file(tk, top_trace_array());
  1230. if (WARN_ON_ONCE(file == NULL)) {
  1231. pr_warn("error on getting probe file.\n");
  1232. warn++;
  1233. } else
  1234. disable_trace_kprobe(tk, file);
  1235. }
  1236. ret = traceprobe_command("-:testprobe", create_trace_kprobe);
  1237. if (WARN_ON_ONCE(ret)) {
  1238. pr_warn("error on deleting a probe.\n");
  1239. warn++;
  1240. }
  1241. ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
  1242. if (WARN_ON_ONCE(ret)) {
  1243. pr_warn("error on deleting a probe.\n");
  1244. warn++;
  1245. }
  1246. end:
  1247. release_all_trace_kprobes();
  1248. if (warn)
  1249. pr_cont("NG: Some tests are failed. Please check them.\n");
  1250. else
  1251. pr_cont("OK\n");
  1252. return 0;
  1253. }
  1254. late_initcall(kprobe_trace_self_tests_init);
  1255. #endif