trace_kprobe.c 38 KB

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