trace_kprobe.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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 nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
  37. {
  38. return tk->rp.handler != NULL;
  39. }
  40. static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
  41. {
  42. return tk->symbol ? tk->symbol : "unknown";
  43. }
  44. static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
  45. {
  46. return tk->rp.kp.offset;
  47. }
  48. static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
  49. {
  50. return !!(kprobe_gone(&tk->rp.kp));
  51. }
  52. static nokprobe_inline 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 nokprobe_inline 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 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. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
  116. DEFINE_BASIC_FETCH_FUNCS(stack)
  117. /* No string on the stack entry */
  118. #define fetch_stack_string NULL
  119. #define fetch_stack_string_size NULL
  120. #define DEFINE_FETCH_memory(type) \
  121. static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
  122. void *addr, void *dest) \
  123. { \
  124. type retval; \
  125. if (probe_kernel_address(addr, retval)) \
  126. *(type *)dest = 0; \
  127. else \
  128. *(type *)dest = retval; \
  129. } \
  130. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
  131. DEFINE_BASIC_FETCH_FUNCS(memory)
  132. /*
  133. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  134. * length and relative data location.
  135. */
  136. static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  137. void *addr, void *dest)
  138. {
  139. int maxlen = get_rloc_len(*(u32 *)dest);
  140. u8 *dst = get_rloc_data(dest);
  141. long ret;
  142. if (!maxlen)
  143. return;
  144. /*
  145. * Try to get string again, since the string can be changed while
  146. * probing.
  147. */
  148. ret = strncpy_from_unsafe(dst, addr, maxlen);
  149. if (ret < 0) { /* Failed to fetch string */
  150. dst[0] = '\0';
  151. *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
  152. } else {
  153. *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
  154. }
  155. }
  156. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
  157. /* Return the length of string -- including null terminal byte */
  158. static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
  159. void *addr, void *dest)
  160. {
  161. mm_segment_t old_fs;
  162. int ret, len = 0;
  163. u8 c;
  164. old_fs = get_fs();
  165. set_fs(KERNEL_DS);
  166. pagefault_disable();
  167. do {
  168. ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
  169. len++;
  170. } while (c && ret == 0 && len < MAX_STRING_SIZE);
  171. pagefault_enable();
  172. set_fs(old_fs);
  173. if (ret < 0) /* Failed to check the length */
  174. *(u32 *)dest = 0;
  175. else
  176. *(u32 *)dest = len;
  177. }
  178. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
  179. #define DEFINE_FETCH_symbol(type) \
  180. void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
  181. { \
  182. struct symbol_cache *sc = data; \
  183. if (sc->addr) \
  184. fetch_memory_##type(regs, (void *)sc->addr, dest); \
  185. else \
  186. *(type *)dest = 0; \
  187. } \
  188. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
  189. DEFINE_BASIC_FETCH_FUNCS(symbol)
  190. DEFINE_FETCH_symbol(string)
  191. DEFINE_FETCH_symbol(string_size)
  192. /* kprobes don't support file_offset fetch methods */
  193. #define fetch_file_offset_u8 NULL
  194. #define fetch_file_offset_u16 NULL
  195. #define fetch_file_offset_u32 NULL
  196. #define fetch_file_offset_u64 NULL
  197. #define fetch_file_offset_string NULL
  198. #define fetch_file_offset_string_size NULL
  199. /* Fetch type information table */
  200. static const struct fetch_type kprobes_fetch_type_table[] = {
  201. /* Special types */
  202. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  203. sizeof(u32), 1, "__data_loc char[]"),
  204. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  205. string_size, sizeof(u32), 0, "u32"),
  206. /* Basic types */
  207. ASSIGN_FETCH_TYPE(u8, u8, 0),
  208. ASSIGN_FETCH_TYPE(u16, u16, 0),
  209. ASSIGN_FETCH_TYPE(u32, u32, 0),
  210. ASSIGN_FETCH_TYPE(u64, u64, 0),
  211. ASSIGN_FETCH_TYPE(s8, u8, 1),
  212. ASSIGN_FETCH_TYPE(s16, u16, 1),
  213. ASSIGN_FETCH_TYPE(s32, u32, 1),
  214. ASSIGN_FETCH_TYPE(s64, u64, 1),
  215. ASSIGN_FETCH_TYPE_END
  216. };
  217. /*
  218. * Allocate new trace_probe and initialize it (including kprobes).
  219. */
  220. static struct trace_kprobe *alloc_trace_kprobe(const char *group,
  221. const char *event,
  222. void *addr,
  223. const char *symbol,
  224. unsigned long offs,
  225. int nargs, bool is_return)
  226. {
  227. struct trace_kprobe *tk;
  228. int ret = -ENOMEM;
  229. tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
  230. if (!tk)
  231. return ERR_PTR(ret);
  232. if (symbol) {
  233. tk->symbol = kstrdup(symbol, GFP_KERNEL);
  234. if (!tk->symbol)
  235. goto error;
  236. tk->rp.kp.symbol_name = tk->symbol;
  237. tk->rp.kp.offset = offs;
  238. } else
  239. tk->rp.kp.addr = addr;
  240. if (is_return)
  241. tk->rp.handler = kretprobe_dispatcher;
  242. else
  243. tk->rp.kp.pre_handler = kprobe_dispatcher;
  244. if (!event || !is_good_name(event)) {
  245. ret = -EINVAL;
  246. goto error;
  247. }
  248. tk->tp.call.class = &tk->tp.class;
  249. tk->tp.call.name = kstrdup(event, GFP_KERNEL);
  250. if (!tk->tp.call.name)
  251. goto error;
  252. if (!group || !is_good_name(group)) {
  253. ret = -EINVAL;
  254. goto error;
  255. }
  256. tk->tp.class.system = kstrdup(group, GFP_KERNEL);
  257. if (!tk->tp.class.system)
  258. goto error;
  259. INIT_LIST_HEAD(&tk->list);
  260. INIT_LIST_HEAD(&tk->tp.files);
  261. return tk;
  262. error:
  263. kfree(tk->tp.call.name);
  264. kfree(tk->symbol);
  265. kfree(tk);
  266. return ERR_PTR(ret);
  267. }
  268. static void free_trace_kprobe(struct trace_kprobe *tk)
  269. {
  270. int i;
  271. for (i = 0; i < tk->tp.nr_args; i++)
  272. traceprobe_free_probe_arg(&tk->tp.args[i]);
  273. kfree(tk->tp.call.class->system);
  274. kfree(tk->tp.call.name);
  275. kfree(tk->symbol);
  276. kfree(tk);
  277. }
  278. static struct trace_kprobe *find_trace_kprobe(const char *event,
  279. const char *group)
  280. {
  281. struct trace_kprobe *tk;
  282. list_for_each_entry(tk, &probe_list, list)
  283. if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
  284. strcmp(tk->tp.call.class->system, group) == 0)
  285. return tk;
  286. return NULL;
  287. }
  288. /*
  289. * Enable trace_probe
  290. * if the file is NULL, enable "perf" handler, or enable "trace" handler.
  291. */
  292. static int
  293. enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
  294. {
  295. struct event_file_link *link = NULL;
  296. int ret = 0;
  297. if (file) {
  298. link = kmalloc(sizeof(*link), GFP_KERNEL);
  299. if (!link) {
  300. ret = -ENOMEM;
  301. goto out;
  302. }
  303. link->file = file;
  304. list_add_tail_rcu(&link->list, &tk->tp.files);
  305. tk->tp.flags |= TP_FLAG_TRACE;
  306. } else
  307. tk->tp.flags |= TP_FLAG_PROFILE;
  308. if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
  309. if (trace_kprobe_is_return(tk))
  310. ret = enable_kretprobe(&tk->rp);
  311. else
  312. ret = enable_kprobe(&tk->rp.kp);
  313. }
  314. if (ret) {
  315. if (file) {
  316. /* Notice the if is true on not WARN() */
  317. if (!WARN_ON_ONCE(!link))
  318. list_del_rcu(&link->list);
  319. kfree(link);
  320. tk->tp.flags &= ~TP_FLAG_TRACE;
  321. } else {
  322. tk->tp.flags &= ~TP_FLAG_PROFILE;
  323. }
  324. }
  325. out:
  326. return ret;
  327. }
  328. /*
  329. * Disable trace_probe
  330. * if the file is NULL, disable "perf" handler, or disable "trace" handler.
  331. */
  332. static int
  333. disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
  334. {
  335. struct event_file_link *link = NULL;
  336. int wait = 0;
  337. int ret = 0;
  338. if (file) {
  339. link = find_event_file_link(&tk->tp, file);
  340. if (!link) {
  341. ret = -EINVAL;
  342. goto out;
  343. }
  344. list_del_rcu(&link->list);
  345. wait = 1;
  346. if (!list_empty(&tk->tp.files))
  347. goto out;
  348. tk->tp.flags &= ~TP_FLAG_TRACE;
  349. } else
  350. tk->tp.flags &= ~TP_FLAG_PROFILE;
  351. if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
  352. if (trace_kprobe_is_return(tk))
  353. disable_kretprobe(&tk->rp);
  354. else
  355. disable_kprobe(&tk->rp.kp);
  356. wait = 1;
  357. }
  358. out:
  359. if (wait) {
  360. /*
  361. * Synchronize with kprobe_trace_func/kretprobe_trace_func
  362. * to ensure disabled (all running handlers are finished).
  363. * This is not only for kfree(), but also the caller,
  364. * trace_remove_event_call() supposes it for releasing
  365. * event_call related objects, which will be accessed in
  366. * the kprobe_trace_func/kretprobe_trace_func.
  367. */
  368. synchronize_sched();
  369. kfree(link); /* Ignored if link == NULL */
  370. }
  371. return ret;
  372. }
  373. /* Internal register function - just handle k*probes and flags */
  374. static int __register_trace_kprobe(struct trace_kprobe *tk)
  375. {
  376. int i, ret;
  377. if (trace_probe_is_registered(&tk->tp))
  378. return -EINVAL;
  379. for (i = 0; i < tk->tp.nr_args; i++)
  380. traceprobe_update_arg(&tk->tp.args[i]);
  381. /* Set/clear disabled flag according to tp->flag */
  382. if (trace_probe_is_enabled(&tk->tp))
  383. tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  384. else
  385. tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  386. if (trace_kprobe_is_return(tk))
  387. ret = register_kretprobe(&tk->rp);
  388. else
  389. ret = register_kprobe(&tk->rp.kp);
  390. if (ret == 0)
  391. tk->tp.flags |= TP_FLAG_REGISTERED;
  392. else {
  393. pr_warning("Could not insert probe at %s+%lu: %d\n",
  394. trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
  395. if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
  396. pr_warning("This probe might be able to register after"
  397. "target module is loaded. Continue.\n");
  398. ret = 0;
  399. } else if (ret == -EILSEQ) {
  400. pr_warning("Probing address(0x%p) is not an "
  401. "instruction boundary.\n",
  402. tk->rp.kp.addr);
  403. ret = -EINVAL;
  404. }
  405. }
  406. return ret;
  407. }
  408. /* Internal unregister function - just handle k*probes and flags */
  409. static void __unregister_trace_kprobe(struct trace_kprobe *tk)
  410. {
  411. if (trace_probe_is_registered(&tk->tp)) {
  412. if (trace_kprobe_is_return(tk))
  413. unregister_kretprobe(&tk->rp);
  414. else
  415. unregister_kprobe(&tk->rp.kp);
  416. tk->tp.flags &= ~TP_FLAG_REGISTERED;
  417. /* Cleanup kprobe for reuse */
  418. if (tk->rp.kp.symbol_name)
  419. tk->rp.kp.addr = NULL;
  420. }
  421. }
  422. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  423. static int unregister_trace_kprobe(struct trace_kprobe *tk)
  424. {
  425. /* Enabled event can not be unregistered */
  426. if (trace_probe_is_enabled(&tk->tp))
  427. return -EBUSY;
  428. /* Will fail if probe is being used by ftrace or perf */
  429. if (unregister_kprobe_event(tk))
  430. return -EBUSY;
  431. __unregister_trace_kprobe(tk);
  432. list_del(&tk->list);
  433. return 0;
  434. }
  435. /* Register a trace_probe and probe_event */
  436. static int register_trace_kprobe(struct trace_kprobe *tk)
  437. {
  438. struct trace_kprobe *old_tk;
  439. int ret;
  440. mutex_lock(&probe_lock);
  441. /* Delete old (same name) event if exist */
  442. old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
  443. tk->tp.call.class->system);
  444. if (old_tk) {
  445. ret = unregister_trace_kprobe(old_tk);
  446. if (ret < 0)
  447. goto end;
  448. free_trace_kprobe(old_tk);
  449. }
  450. /* Register new event */
  451. ret = register_kprobe_event(tk);
  452. if (ret) {
  453. pr_warning("Failed to register probe event(%d)\n", ret);
  454. goto end;
  455. }
  456. /* Register k*probe */
  457. ret = __register_trace_kprobe(tk);
  458. if (ret < 0)
  459. unregister_kprobe_event(tk);
  460. else
  461. list_add_tail(&tk->list, &probe_list);
  462. end:
  463. mutex_unlock(&probe_lock);
  464. return ret;
  465. }
  466. /* Module notifier call back, checking event on the module */
  467. static int trace_kprobe_module_callback(struct notifier_block *nb,
  468. unsigned long val, void *data)
  469. {
  470. struct module *mod = data;
  471. struct trace_kprobe *tk;
  472. int ret;
  473. if (val != MODULE_STATE_COMING)
  474. return NOTIFY_DONE;
  475. /* Update probes on coming module */
  476. mutex_lock(&probe_lock);
  477. list_for_each_entry(tk, &probe_list, list) {
  478. if (trace_kprobe_within_module(tk, mod)) {
  479. /* Don't need to check busy - this should have gone. */
  480. __unregister_trace_kprobe(tk);
  481. ret = __register_trace_kprobe(tk);
  482. if (ret)
  483. pr_warning("Failed to re-register probe %s on"
  484. "%s: %d\n",
  485. trace_event_name(&tk->tp.call),
  486. mod->name, ret);
  487. }
  488. }
  489. mutex_unlock(&probe_lock);
  490. return NOTIFY_DONE;
  491. }
  492. static struct notifier_block trace_kprobe_module_nb = {
  493. .notifier_call = trace_kprobe_module_callback,
  494. .priority = 1 /* Invoked after kprobe module callback */
  495. };
  496. static int create_trace_kprobe(int argc, char **argv)
  497. {
  498. /*
  499. * Argument syntax:
  500. * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  501. * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  502. * Fetch args:
  503. * $retval : fetch return value
  504. * $stack : fetch stack address
  505. * $stackN : fetch Nth of stack (N:0-)
  506. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  507. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  508. * %REG : fetch register REG
  509. * Dereferencing memory fetch:
  510. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  511. * Alias name of args:
  512. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  513. * Type of args:
  514. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  515. */
  516. struct trace_kprobe *tk;
  517. int i, ret = 0;
  518. bool is_return = false, is_delete = false;
  519. char *symbol = NULL, *event = NULL, *group = NULL;
  520. char *arg;
  521. long offset = 0;
  522. void *addr = NULL;
  523. char buf[MAX_EVENT_NAME_LEN];
  524. /* argc must be >= 1 */
  525. if (argv[0][0] == 'p')
  526. is_return = false;
  527. else if (argv[0][0] == 'r')
  528. is_return = true;
  529. else if (argv[0][0] == '-')
  530. is_delete = true;
  531. else {
  532. pr_info("Probe definition must be started with 'p', 'r' or"
  533. " '-'.\n");
  534. return -EINVAL;
  535. }
  536. if (argv[0][1] == ':') {
  537. event = &argv[0][2];
  538. if (strchr(event, '/')) {
  539. group = event;
  540. event = strchr(group, '/') + 1;
  541. event[-1] = '\0';
  542. if (strlen(group) == 0) {
  543. pr_info("Group name is not specified\n");
  544. return -EINVAL;
  545. }
  546. }
  547. if (strlen(event) == 0) {
  548. pr_info("Event name is not specified\n");
  549. return -EINVAL;
  550. }
  551. }
  552. if (!group)
  553. group = KPROBE_EVENT_SYSTEM;
  554. if (is_delete) {
  555. if (!event) {
  556. pr_info("Delete command needs an event name.\n");
  557. return -EINVAL;
  558. }
  559. mutex_lock(&probe_lock);
  560. tk = find_trace_kprobe(event, group);
  561. if (!tk) {
  562. mutex_unlock(&probe_lock);
  563. pr_info("Event %s/%s doesn't exist.\n", group, event);
  564. return -ENOENT;
  565. }
  566. /* delete an event */
  567. ret = unregister_trace_kprobe(tk);
  568. if (ret == 0)
  569. free_trace_kprobe(tk);
  570. mutex_unlock(&probe_lock);
  571. return ret;
  572. }
  573. if (argc < 2) {
  574. pr_info("Probe point is not specified.\n");
  575. return -EINVAL;
  576. }
  577. /* try to parse an address. if that fails, try to read the
  578. * input as a symbol. */
  579. if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
  580. /* a symbol specified */
  581. symbol = argv[1];
  582. /* TODO: support .init module functions */
  583. ret = traceprobe_split_symbol_offset(symbol, &offset);
  584. if (ret || offset < 0 || offset > UINT_MAX) {
  585. pr_info("Failed to parse either an address or a symbol.\n");
  586. return ret;
  587. }
  588. if (offset && is_return) {
  589. pr_info("Return probe must be used without offset.\n");
  590. return -EINVAL;
  591. }
  592. } else if (is_return) {
  593. pr_info("Return probe point must be a symbol.\n");
  594. return -EINVAL;
  595. }
  596. argc -= 2; argv += 2;
  597. /* setup a probe */
  598. if (!event) {
  599. /* Make a new event name */
  600. if (symbol)
  601. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  602. is_return ? 'r' : 'p', symbol, offset);
  603. else
  604. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  605. is_return ? 'r' : 'p', addr);
  606. event = buf;
  607. }
  608. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc,
  609. is_return);
  610. if (IS_ERR(tk)) {
  611. pr_info("Failed to allocate trace_probe.(%d)\n",
  612. (int)PTR_ERR(tk));
  613. return PTR_ERR(tk);
  614. }
  615. /* parse arguments */
  616. ret = 0;
  617. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  618. struct probe_arg *parg = &tk->tp.args[i];
  619. /* Increment count for freeing args in error case */
  620. tk->tp.nr_args++;
  621. /* Parse argument name */
  622. arg = strchr(argv[i], '=');
  623. if (arg) {
  624. *arg++ = '\0';
  625. parg->name = kstrdup(argv[i], GFP_KERNEL);
  626. } else {
  627. arg = argv[i];
  628. /* If argument name is omitted, set "argN" */
  629. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  630. parg->name = kstrdup(buf, GFP_KERNEL);
  631. }
  632. if (!parg->name) {
  633. pr_info("Failed to allocate argument[%d] name.\n", i);
  634. ret = -ENOMEM;
  635. goto error;
  636. }
  637. if (!is_good_name(parg->name)) {
  638. pr_info("Invalid argument[%d] name: %s\n",
  639. i, parg->name);
  640. ret = -EINVAL;
  641. goto error;
  642. }
  643. if (traceprobe_conflict_field_name(parg->name,
  644. tk->tp.args, i)) {
  645. pr_info("Argument[%d] name '%s' conflicts with "
  646. "another field.\n", i, argv[i]);
  647. ret = -EINVAL;
  648. goto error;
  649. }
  650. /* Parse fetch argument */
  651. ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
  652. is_return, true,
  653. kprobes_fetch_type_table);
  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_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  709. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  710. trace_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_putc(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. trace_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 nokprobe_inline void
  781. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  782. struct trace_event_file *trace_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 trace_event_call *call = &tk->tp.call;
  790. WARN_ON(call != trace_file->event_call);
  791. if (trace_trigger_soft_disabled(trace_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, trace_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(trace_file, buffer, event,
  806. entry, irq_flags, pc, regs);
  807. }
  808. static 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. NOKPROBE_SYMBOL(kprobe_trace_func);
  816. /* Kretprobe handler */
  817. static nokprobe_inline void
  818. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  819. struct pt_regs *regs,
  820. struct trace_event_file *trace_file)
  821. {
  822. struct kretprobe_trace_entry_head *entry;
  823. struct ring_buffer_event *event;
  824. struct ring_buffer *buffer;
  825. int size, pc, dsize;
  826. unsigned long irq_flags;
  827. struct trace_event_call *call = &tk->tp.call;
  828. WARN_ON(call != trace_file->event_call);
  829. if (trace_trigger_soft_disabled(trace_file))
  830. return;
  831. local_save_flags(irq_flags);
  832. pc = preempt_count();
  833. dsize = __get_data_size(&tk->tp, regs);
  834. size = sizeof(*entry) + tk->tp.size + dsize;
  835. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  836. call->event.type,
  837. size, irq_flags, pc);
  838. if (!event)
  839. return;
  840. entry = ring_buffer_event_data(event);
  841. entry->func = (unsigned long)tk->rp.kp.addr;
  842. entry->ret_ip = (unsigned long)ri->ret_addr;
  843. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  844. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  845. entry, irq_flags, pc, regs);
  846. }
  847. static void
  848. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  849. struct pt_regs *regs)
  850. {
  851. struct event_file_link *link;
  852. list_for_each_entry_rcu(link, &tk->tp.files, list)
  853. __kretprobe_trace_func(tk, ri, regs, link->file);
  854. }
  855. NOKPROBE_SYMBOL(kretprobe_trace_func);
  856. /* Event entry printers */
  857. static enum print_line_t
  858. print_kprobe_event(struct trace_iterator *iter, int flags,
  859. struct trace_event *event)
  860. {
  861. struct kprobe_trace_entry_head *field;
  862. struct trace_seq *s = &iter->seq;
  863. struct trace_probe *tp;
  864. u8 *data;
  865. int i;
  866. field = (struct kprobe_trace_entry_head *)iter->ent;
  867. tp = container_of(event, struct trace_probe, call.event);
  868. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  869. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  870. goto out;
  871. trace_seq_putc(s, ')');
  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 out;
  877. trace_seq_putc(s, '\n');
  878. out:
  879. return trace_handle_return(s);
  880. }
  881. static enum print_line_t
  882. print_kretprobe_event(struct trace_iterator *iter, int flags,
  883. struct trace_event *event)
  884. {
  885. struct kretprobe_trace_entry_head *field;
  886. struct trace_seq *s = &iter->seq;
  887. struct trace_probe *tp;
  888. u8 *data;
  889. int i;
  890. field = (struct kretprobe_trace_entry_head *)iter->ent;
  891. tp = container_of(event, struct trace_probe, call.event);
  892. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  893. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  894. goto out;
  895. trace_seq_puts(s, " <- ");
  896. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  897. goto out;
  898. trace_seq_putc(s, ')');
  899. data = (u8 *)&field[1];
  900. for (i = 0; i < tp->nr_args; i++)
  901. if (!tp->args[i].type->print(s, tp->args[i].name,
  902. data + tp->args[i].offset, field))
  903. goto out;
  904. trace_seq_putc(s, '\n');
  905. out:
  906. return trace_handle_return(s);
  907. }
  908. static int kprobe_event_define_fields(struct trace_event_call *event_call)
  909. {
  910. int ret, i;
  911. struct kprobe_trace_entry_head field;
  912. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  913. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  914. /* Set argument names as fields */
  915. for (i = 0; i < tk->tp.nr_args; i++) {
  916. struct probe_arg *parg = &tk->tp.args[i];
  917. ret = trace_define_field(event_call, parg->type->fmttype,
  918. parg->name,
  919. sizeof(field) + parg->offset,
  920. parg->type->size,
  921. parg->type->is_signed,
  922. FILTER_OTHER);
  923. if (ret)
  924. return ret;
  925. }
  926. return 0;
  927. }
  928. static int kretprobe_event_define_fields(struct trace_event_call *event_call)
  929. {
  930. int ret, i;
  931. struct kretprobe_trace_entry_head field;
  932. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  933. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  934. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  935. /* Set argument names as fields */
  936. for (i = 0; i < tk->tp.nr_args; i++) {
  937. struct probe_arg *parg = &tk->tp.args[i];
  938. ret = trace_define_field(event_call, parg->type->fmttype,
  939. parg->name,
  940. sizeof(field) + parg->offset,
  941. parg->type->size,
  942. parg->type->is_signed,
  943. FILTER_OTHER);
  944. if (ret)
  945. return ret;
  946. }
  947. return 0;
  948. }
  949. #ifdef CONFIG_PERF_EVENTS
  950. /* Kprobe profile handler */
  951. static void
  952. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  953. {
  954. struct trace_event_call *call = &tk->tp.call;
  955. struct bpf_prog *prog = call->prog;
  956. struct kprobe_trace_entry_head *entry;
  957. struct hlist_head *head;
  958. int size, __size, dsize;
  959. int rctx;
  960. if (prog && !trace_call_bpf(prog, regs))
  961. return;
  962. head = this_cpu_ptr(call->perf_events);
  963. if (hlist_empty(head))
  964. return;
  965. dsize = __get_data_size(&tk->tp, regs);
  966. __size = sizeof(*entry) + tk->tp.size + dsize;
  967. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  968. size -= sizeof(u32);
  969. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  970. if (!entry)
  971. return;
  972. entry->ip = (unsigned long)tk->rp.kp.addr;
  973. memset(&entry[1], 0, dsize);
  974. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  975. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  976. }
  977. NOKPROBE_SYMBOL(kprobe_perf_func);
  978. /* Kretprobe profile handler */
  979. static void
  980. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  981. struct pt_regs *regs)
  982. {
  983. struct trace_event_call *call = &tk->tp.call;
  984. struct bpf_prog *prog = call->prog;
  985. struct kretprobe_trace_entry_head *entry;
  986. struct hlist_head *head;
  987. int size, __size, dsize;
  988. int rctx;
  989. if (prog && !trace_call_bpf(prog, regs))
  990. return;
  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, NULL, &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. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1007. #endif /* CONFIG_PERF_EVENTS */
  1008. /*
  1009. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1010. *
  1011. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1012. * lockless, but we can't race with this __init function.
  1013. */
  1014. static int kprobe_register(struct trace_event_call *event,
  1015. enum trace_reg type, void *data)
  1016. {
  1017. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1018. struct trace_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 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1039. {
  1040. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1041. tk->nhit++;
  1042. if (tk->tp.flags & TP_FLAG_TRACE)
  1043. kprobe_trace_func(tk, regs);
  1044. #ifdef CONFIG_PERF_EVENTS
  1045. if (tk->tp.flags & TP_FLAG_PROFILE)
  1046. kprobe_perf_func(tk, regs);
  1047. #endif
  1048. return 0; /* We don't tweek kernel, so just return 0 */
  1049. }
  1050. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1051. static int
  1052. 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. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1065. static struct trace_event_functions kretprobe_funcs = {
  1066. .trace = print_kretprobe_event
  1067. };
  1068. static struct trace_event_functions kprobe_funcs = {
  1069. .trace = print_kprobe_event
  1070. };
  1071. static int register_kprobe_event(struct trace_kprobe *tk)
  1072. {
  1073. struct trace_event_call *call = &tk->tp.call;
  1074. int ret;
  1075. /* Initialize trace_event_call */
  1076. INIT_LIST_HEAD(&call->class->fields);
  1077. if (trace_kprobe_is_return(tk)) {
  1078. call->event.funcs = &kretprobe_funcs;
  1079. call->class->define_fields = kretprobe_event_define_fields;
  1080. } else {
  1081. call->event.funcs = &kprobe_funcs;
  1082. call->class->define_fields = kprobe_event_define_fields;
  1083. }
  1084. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1085. return -ENOMEM;
  1086. ret = register_trace_event(&call->event);
  1087. if (!ret) {
  1088. kfree(call->print_fmt);
  1089. return -ENODEV;
  1090. }
  1091. call->flags = TRACE_EVENT_FL_KPROBE;
  1092. call->class->reg = kprobe_register;
  1093. call->data = tk;
  1094. ret = trace_add_event_call(call);
  1095. if (ret) {
  1096. pr_info("Failed to register kprobe event: %s\n",
  1097. trace_event_name(call));
  1098. kfree(call->print_fmt);
  1099. unregister_trace_event(&call->event);
  1100. }
  1101. return ret;
  1102. }
  1103. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1104. {
  1105. int ret;
  1106. /* tp->event is unregistered in trace_remove_event_call() */
  1107. ret = trace_remove_event_call(&tk->tp.call);
  1108. if (!ret)
  1109. kfree(tk->tp.call.print_fmt);
  1110. return ret;
  1111. }
  1112. /* Make a tracefs interface for controlling probe points */
  1113. static __init int init_kprobe_trace(void)
  1114. {
  1115. struct dentry *d_tracer;
  1116. struct dentry *entry;
  1117. if (register_module_notifier(&trace_kprobe_module_nb))
  1118. return -EINVAL;
  1119. d_tracer = tracing_init_dentry();
  1120. if (IS_ERR(d_tracer))
  1121. return 0;
  1122. entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
  1123. NULL, &kprobe_events_ops);
  1124. /* Event list interface */
  1125. if (!entry)
  1126. pr_warning("Could not create tracefs "
  1127. "'kprobe_events' entry\n");
  1128. /* Profile interface */
  1129. entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
  1130. NULL, &kprobe_profile_ops);
  1131. if (!entry)
  1132. pr_warning("Could not create tracefs "
  1133. "'kprobe_profile' entry\n");
  1134. return 0;
  1135. }
  1136. fs_initcall(init_kprobe_trace);
  1137. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1138. /*
  1139. * The "__used" keeps gcc from removing the function symbol
  1140. * from the kallsyms table.
  1141. */
  1142. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1143. int a4, int a5, int a6)
  1144. {
  1145. return a1 + a2 + a3 + a4 + a5 + a6;
  1146. }
  1147. static struct trace_event_file *
  1148. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1149. {
  1150. struct trace_event_file *file;
  1151. list_for_each_entry(file, &tr->events, list)
  1152. if (file->event_call == &tk->tp.call)
  1153. return file;
  1154. return NULL;
  1155. }
  1156. /*
  1157. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1158. * stage, we can do this lockless.
  1159. */
  1160. static __init int kprobe_trace_self_tests_init(void)
  1161. {
  1162. int ret, warn = 0;
  1163. int (*target)(int, int, int, int, int, int);
  1164. struct trace_kprobe *tk;
  1165. struct trace_event_file *file;
  1166. if (tracing_is_disabled())
  1167. return -ENODEV;
  1168. target = kprobe_trace_selftest_target;
  1169. pr_info("Testing kprobe tracing: ");
  1170. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1171. "$stack $stack0 +0($stack)",
  1172. create_trace_kprobe);
  1173. if (WARN_ON_ONCE(ret)) {
  1174. pr_warn("error on probing function entry.\n");
  1175. warn++;
  1176. } else {
  1177. /* Enable trace point */
  1178. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1179. if (WARN_ON_ONCE(tk == NULL)) {
  1180. pr_warn("error on getting new probe.\n");
  1181. warn++;
  1182. } else {
  1183. file = find_trace_probe_file(tk, top_trace_array());
  1184. if (WARN_ON_ONCE(file == NULL)) {
  1185. pr_warn("error on getting probe file.\n");
  1186. warn++;
  1187. } else
  1188. enable_trace_kprobe(tk, file);
  1189. }
  1190. }
  1191. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1192. "$retval", create_trace_kprobe);
  1193. if (WARN_ON_ONCE(ret)) {
  1194. pr_warn("error on probing function return.\n");
  1195. warn++;
  1196. } else {
  1197. /* Enable trace point */
  1198. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1199. if (WARN_ON_ONCE(tk == NULL)) {
  1200. pr_warn("error on getting 2nd new probe.\n");
  1201. warn++;
  1202. } else {
  1203. file = find_trace_probe_file(tk, top_trace_array());
  1204. if (WARN_ON_ONCE(file == NULL)) {
  1205. pr_warn("error on getting probe file.\n");
  1206. warn++;
  1207. } else
  1208. enable_trace_kprobe(tk, file);
  1209. }
  1210. }
  1211. if (warn)
  1212. goto end;
  1213. ret = target(1, 2, 3, 4, 5, 6);
  1214. /* Disable trace points before removing it */
  1215. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1216. if (WARN_ON_ONCE(tk == NULL)) {
  1217. pr_warn("error on getting test probe.\n");
  1218. warn++;
  1219. } else {
  1220. file = find_trace_probe_file(tk, top_trace_array());
  1221. if (WARN_ON_ONCE(file == NULL)) {
  1222. pr_warn("error on getting probe file.\n");
  1223. warn++;
  1224. } else
  1225. disable_trace_kprobe(tk, file);
  1226. }
  1227. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1228. if (WARN_ON_ONCE(tk == NULL)) {
  1229. pr_warn("error on getting 2nd test probe.\n");
  1230. warn++;
  1231. } else {
  1232. file = find_trace_probe_file(tk, top_trace_array());
  1233. if (WARN_ON_ONCE(file == NULL)) {
  1234. pr_warn("error on getting probe file.\n");
  1235. warn++;
  1236. } else
  1237. disable_trace_kprobe(tk, file);
  1238. }
  1239. ret = traceprobe_command("-:testprobe", create_trace_kprobe);
  1240. if (WARN_ON_ONCE(ret)) {
  1241. pr_warn("error on deleting a probe.\n");
  1242. warn++;
  1243. }
  1244. ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
  1245. if (WARN_ON_ONCE(ret)) {
  1246. pr_warn("error on deleting a probe.\n");
  1247. warn++;
  1248. }
  1249. end:
  1250. release_all_trace_kprobes();
  1251. /*
  1252. * Wait for the optimizer work to finish. Otherwise it might fiddle
  1253. * with probes in already freed __init text.
  1254. */
  1255. wait_for_kprobe_optimizer();
  1256. if (warn)
  1257. pr_cont("NG: Some tests are failed. Please check them.\n");
  1258. else
  1259. pr_cont("OK\n");
  1260. return 0;
  1261. }
  1262. late_initcall(kprobe_trace_self_tests_init);
  1263. #endif