trace_kprobe.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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. struct event_file_link *link = NULL;
  318. int ret = 0;
  319. if (file) {
  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. if (ret) {
  337. if (file) {
  338. /* Notice the if is true on not WARN() */
  339. if (!WARN_ON_ONCE(!link))
  340. list_del_rcu(&link->list);
  341. kfree(link);
  342. tk->tp.flags &= ~TP_FLAG_TRACE;
  343. } else {
  344. tk->tp.flags &= ~TP_FLAG_PROFILE;
  345. }
  346. }
  347. out:
  348. return ret;
  349. }
  350. /*
  351. * Disable trace_probe
  352. * if the file is NULL, disable "perf" handler, or disable "trace" handler.
  353. */
  354. static int
  355. disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
  356. {
  357. struct event_file_link *link = NULL;
  358. int wait = 0;
  359. int ret = 0;
  360. if (file) {
  361. link = find_event_file_link(&tk->tp, file);
  362. if (!link) {
  363. ret = -EINVAL;
  364. goto out;
  365. }
  366. list_del_rcu(&link->list);
  367. wait = 1;
  368. if (!list_empty(&tk->tp.files))
  369. goto out;
  370. tk->tp.flags &= ~TP_FLAG_TRACE;
  371. } else
  372. tk->tp.flags &= ~TP_FLAG_PROFILE;
  373. if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
  374. if (trace_kprobe_is_return(tk))
  375. disable_kretprobe(&tk->rp);
  376. else
  377. disable_kprobe(&tk->rp.kp);
  378. wait = 1;
  379. }
  380. out:
  381. if (wait) {
  382. /*
  383. * Synchronize with kprobe_trace_func/kretprobe_trace_func
  384. * to ensure disabled (all running handlers are finished).
  385. * This is not only for kfree(), but also the caller,
  386. * trace_remove_event_call() supposes it for releasing
  387. * event_call related objects, which will be accessed in
  388. * the kprobe_trace_func/kretprobe_trace_func.
  389. */
  390. synchronize_sched();
  391. kfree(link); /* Ignored if link == NULL */
  392. }
  393. return ret;
  394. }
  395. /* Internal register function - just handle k*probes and flags */
  396. static int __register_trace_kprobe(struct trace_kprobe *tk)
  397. {
  398. int i, ret;
  399. if (trace_probe_is_registered(&tk->tp))
  400. return -EINVAL;
  401. for (i = 0; i < tk->tp.nr_args; i++)
  402. traceprobe_update_arg(&tk->tp.args[i]);
  403. /* Set/clear disabled flag according to tp->flag */
  404. if (trace_probe_is_enabled(&tk->tp))
  405. tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  406. else
  407. tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  408. if (trace_kprobe_is_return(tk))
  409. ret = register_kretprobe(&tk->rp);
  410. else
  411. ret = register_kprobe(&tk->rp.kp);
  412. if (ret == 0)
  413. tk->tp.flags |= TP_FLAG_REGISTERED;
  414. else {
  415. pr_warn("Could not insert probe at %s+%lu: %d\n",
  416. trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
  417. if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
  418. pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
  419. ret = 0;
  420. } else if (ret == -EILSEQ) {
  421. pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
  422. tk->rp.kp.addr);
  423. ret = -EINVAL;
  424. }
  425. }
  426. return ret;
  427. }
  428. /* Internal unregister function - just handle k*probes and flags */
  429. static void __unregister_trace_kprobe(struct trace_kprobe *tk)
  430. {
  431. if (trace_probe_is_registered(&tk->tp)) {
  432. if (trace_kprobe_is_return(tk))
  433. unregister_kretprobe(&tk->rp);
  434. else
  435. unregister_kprobe(&tk->rp.kp);
  436. tk->tp.flags &= ~TP_FLAG_REGISTERED;
  437. /* Cleanup kprobe for reuse */
  438. if (tk->rp.kp.symbol_name)
  439. tk->rp.kp.addr = NULL;
  440. }
  441. }
  442. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  443. static int unregister_trace_kprobe(struct trace_kprobe *tk)
  444. {
  445. /* Enabled event can not be unregistered */
  446. if (trace_probe_is_enabled(&tk->tp))
  447. return -EBUSY;
  448. /* Will fail if probe is being used by ftrace or perf */
  449. if (unregister_kprobe_event(tk))
  450. return -EBUSY;
  451. __unregister_trace_kprobe(tk);
  452. list_del(&tk->list);
  453. return 0;
  454. }
  455. /* Register a trace_probe and probe_event */
  456. static int register_trace_kprobe(struct trace_kprobe *tk)
  457. {
  458. struct trace_kprobe *old_tk;
  459. int ret;
  460. mutex_lock(&probe_lock);
  461. /* Delete old (same name) event if exist */
  462. old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
  463. tk->tp.call.class->system);
  464. if (old_tk) {
  465. ret = unregister_trace_kprobe(old_tk);
  466. if (ret < 0)
  467. goto end;
  468. free_trace_kprobe(old_tk);
  469. }
  470. /* Register new event */
  471. ret = register_kprobe_event(tk);
  472. if (ret) {
  473. pr_warn("Failed to register probe event(%d)\n", ret);
  474. goto end;
  475. }
  476. /* Register k*probe */
  477. ret = __register_trace_kprobe(tk);
  478. if (ret < 0)
  479. unregister_kprobe_event(tk);
  480. else
  481. list_add_tail(&tk->list, &probe_list);
  482. end:
  483. mutex_unlock(&probe_lock);
  484. return ret;
  485. }
  486. /* Module notifier call back, checking event on the module */
  487. static int trace_kprobe_module_callback(struct notifier_block *nb,
  488. unsigned long val, void *data)
  489. {
  490. struct module *mod = data;
  491. struct trace_kprobe *tk;
  492. int ret;
  493. if (val != MODULE_STATE_COMING)
  494. return NOTIFY_DONE;
  495. /* Update probes on coming module */
  496. mutex_lock(&probe_lock);
  497. list_for_each_entry(tk, &probe_list, list) {
  498. if (trace_kprobe_within_module(tk, mod)) {
  499. /* Don't need to check busy - this should have gone. */
  500. __unregister_trace_kprobe(tk);
  501. ret = __register_trace_kprobe(tk);
  502. if (ret)
  503. pr_warn("Failed to re-register probe %s on %s: %d\n",
  504. trace_event_name(&tk->tp.call),
  505. mod->name, ret);
  506. }
  507. }
  508. mutex_unlock(&probe_lock);
  509. return NOTIFY_DONE;
  510. }
  511. static struct notifier_block trace_kprobe_module_nb = {
  512. .notifier_call = trace_kprobe_module_callback,
  513. .priority = 1 /* Invoked after kprobe module callback */
  514. };
  515. /* Convert certain expected symbols into '_' when generating event names */
  516. static inline void sanitize_event_name(char *name)
  517. {
  518. while (*name++ != '\0')
  519. if (*name == ':' || *name == '.')
  520. *name = '_';
  521. }
  522. static int create_trace_kprobe(int argc, char **argv)
  523. {
  524. /*
  525. * Argument syntax:
  526. * - Add kprobe:
  527. * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  528. * - Add kretprobe:
  529. * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  530. * Fetch args:
  531. * $retval : fetch return value
  532. * $stack : fetch stack address
  533. * $stackN : fetch Nth of stack (N:0-)
  534. * $comm : fetch current task comm
  535. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  536. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  537. * %REG : fetch register REG
  538. * Dereferencing memory fetch:
  539. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  540. * Alias name of args:
  541. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  542. * Type of args:
  543. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  544. */
  545. struct trace_kprobe *tk;
  546. int i, ret = 0;
  547. bool is_return = false, is_delete = false;
  548. char *symbol = NULL, *event = NULL, *group = NULL;
  549. int maxactive = 0;
  550. char *arg;
  551. long offset = 0;
  552. void *addr = NULL;
  553. char buf[MAX_EVENT_NAME_LEN];
  554. /* argc must be >= 1 */
  555. if (argv[0][0] == 'p')
  556. is_return = false;
  557. else if (argv[0][0] == 'r')
  558. is_return = true;
  559. else if (argv[0][0] == '-')
  560. is_delete = true;
  561. else {
  562. pr_info("Probe definition must be started with 'p', 'r' or"
  563. " '-'.\n");
  564. return -EINVAL;
  565. }
  566. event = strchr(&argv[0][1], ':');
  567. if (event) {
  568. event[0] = '\0';
  569. event++;
  570. }
  571. if (is_return && isdigit(argv[0][1])) {
  572. ret = kstrtouint(&argv[0][1], 0, &maxactive);
  573. if (ret) {
  574. pr_info("Failed to parse maxactive.\n");
  575. return ret;
  576. }
  577. /* kretprobes instances are iterated over via a list. The
  578. * maximum should stay reasonable.
  579. */
  580. if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
  581. pr_info("Maxactive is too big (%d > %d).\n",
  582. maxactive, KRETPROBE_MAXACTIVE_MAX);
  583. return -E2BIG;
  584. }
  585. }
  586. if (event) {
  587. if (strchr(event, '/')) {
  588. group = event;
  589. event = strchr(group, '/') + 1;
  590. event[-1] = '\0';
  591. if (strlen(group) == 0) {
  592. pr_info("Group name is not specified\n");
  593. return -EINVAL;
  594. }
  595. }
  596. if (strlen(event) == 0) {
  597. pr_info("Event name is not specified\n");
  598. return -EINVAL;
  599. }
  600. }
  601. if (!group)
  602. group = KPROBE_EVENT_SYSTEM;
  603. if (is_delete) {
  604. if (!event) {
  605. pr_info("Delete command needs an event name.\n");
  606. return -EINVAL;
  607. }
  608. mutex_lock(&probe_lock);
  609. tk = find_trace_kprobe(event, group);
  610. if (!tk) {
  611. mutex_unlock(&probe_lock);
  612. pr_info("Event %s/%s doesn't exist.\n", group, event);
  613. return -ENOENT;
  614. }
  615. /* delete an event */
  616. ret = unregister_trace_kprobe(tk);
  617. if (ret == 0)
  618. free_trace_kprobe(tk);
  619. mutex_unlock(&probe_lock);
  620. return ret;
  621. }
  622. if (argc < 2) {
  623. pr_info("Probe point is not specified.\n");
  624. return -EINVAL;
  625. }
  626. /* try to parse an address. if that fails, try to read the
  627. * input as a symbol. */
  628. if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
  629. /* a symbol specified */
  630. symbol = argv[1];
  631. /* TODO: support .init module functions */
  632. ret = traceprobe_split_symbol_offset(symbol, &offset);
  633. if (ret || offset < 0 || offset > UINT_MAX) {
  634. pr_info("Failed to parse either an address or a symbol.\n");
  635. return ret;
  636. }
  637. if (offset && is_return &&
  638. !kprobe_on_func_entry(NULL, symbol, offset)) {
  639. pr_info("Given offset is not valid for return probe.\n");
  640. return -EINVAL;
  641. }
  642. }
  643. argc -= 2; argv += 2;
  644. /* setup a probe */
  645. if (!event) {
  646. /* Make a new event name */
  647. if (symbol)
  648. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  649. is_return ? 'r' : 'p', symbol, offset);
  650. else
  651. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  652. is_return ? 'r' : 'p', addr);
  653. sanitize_event_name(buf);
  654. event = buf;
  655. }
  656. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
  657. argc, is_return);
  658. if (IS_ERR(tk)) {
  659. pr_info("Failed to allocate trace_probe.(%d)\n",
  660. (int)PTR_ERR(tk));
  661. return PTR_ERR(tk);
  662. }
  663. /* parse arguments */
  664. ret = 0;
  665. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  666. struct probe_arg *parg = &tk->tp.args[i];
  667. /* Increment count for freeing args in error case */
  668. tk->tp.nr_args++;
  669. /* Parse argument name */
  670. arg = strchr(argv[i], '=');
  671. if (arg) {
  672. *arg++ = '\0';
  673. parg->name = kstrdup(argv[i], GFP_KERNEL);
  674. } else {
  675. arg = argv[i];
  676. /* If argument name is omitted, set "argN" */
  677. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  678. parg->name = kstrdup(buf, GFP_KERNEL);
  679. }
  680. if (!parg->name) {
  681. pr_info("Failed to allocate argument[%d] name.\n", i);
  682. ret = -ENOMEM;
  683. goto error;
  684. }
  685. if (!is_good_name(parg->name)) {
  686. pr_info("Invalid argument[%d] name: %s\n",
  687. i, parg->name);
  688. ret = -EINVAL;
  689. goto error;
  690. }
  691. if (traceprobe_conflict_field_name(parg->name,
  692. tk->tp.args, i)) {
  693. pr_info("Argument[%d] name '%s' conflicts with "
  694. "another field.\n", i, argv[i]);
  695. ret = -EINVAL;
  696. goto error;
  697. }
  698. /* Parse fetch argument */
  699. ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
  700. is_return, true,
  701. kprobes_fetch_type_table);
  702. if (ret) {
  703. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  704. goto error;
  705. }
  706. }
  707. ret = register_trace_kprobe(tk);
  708. if (ret)
  709. goto error;
  710. return 0;
  711. error:
  712. free_trace_kprobe(tk);
  713. return ret;
  714. }
  715. static int release_all_trace_kprobes(void)
  716. {
  717. struct trace_kprobe *tk;
  718. int ret = 0;
  719. mutex_lock(&probe_lock);
  720. /* Ensure no probe is in use. */
  721. list_for_each_entry(tk, &probe_list, list)
  722. if (trace_probe_is_enabled(&tk->tp)) {
  723. ret = -EBUSY;
  724. goto end;
  725. }
  726. /* TODO: Use batch unregistration */
  727. while (!list_empty(&probe_list)) {
  728. tk = list_entry(probe_list.next, struct trace_kprobe, list);
  729. ret = unregister_trace_kprobe(tk);
  730. if (ret)
  731. goto end;
  732. free_trace_kprobe(tk);
  733. }
  734. end:
  735. mutex_unlock(&probe_lock);
  736. return ret;
  737. }
  738. /* Probes listing interfaces */
  739. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  740. {
  741. mutex_lock(&probe_lock);
  742. return seq_list_start(&probe_list, *pos);
  743. }
  744. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  745. {
  746. return seq_list_next(v, &probe_list, pos);
  747. }
  748. static void probes_seq_stop(struct seq_file *m, void *v)
  749. {
  750. mutex_unlock(&probe_lock);
  751. }
  752. static int probes_seq_show(struct seq_file *m, void *v)
  753. {
  754. struct trace_kprobe *tk = v;
  755. int i;
  756. seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  757. if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
  758. seq_printf(m, "%d", tk->rp.maxactive);
  759. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  760. trace_event_name(&tk->tp.call));
  761. if (!tk->symbol)
  762. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  763. else if (tk->rp.kp.offset)
  764. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  765. tk->rp.kp.offset);
  766. else
  767. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  768. for (i = 0; i < tk->tp.nr_args; i++)
  769. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  770. seq_putc(m, '\n');
  771. return 0;
  772. }
  773. static const struct seq_operations probes_seq_op = {
  774. .start = probes_seq_start,
  775. .next = probes_seq_next,
  776. .stop = probes_seq_stop,
  777. .show = probes_seq_show
  778. };
  779. static int probes_open(struct inode *inode, struct file *file)
  780. {
  781. int ret;
  782. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  783. ret = release_all_trace_kprobes();
  784. if (ret < 0)
  785. return ret;
  786. }
  787. return seq_open(file, &probes_seq_op);
  788. }
  789. static ssize_t probes_write(struct file *file, const char __user *buffer,
  790. size_t count, loff_t *ppos)
  791. {
  792. return traceprobe_probes_write(file, buffer, count, ppos,
  793. create_trace_kprobe);
  794. }
  795. static const struct file_operations kprobe_events_ops = {
  796. .owner = THIS_MODULE,
  797. .open = probes_open,
  798. .read = seq_read,
  799. .llseek = seq_lseek,
  800. .release = seq_release,
  801. .write = probes_write,
  802. };
  803. /* Probes profiling interfaces */
  804. static int probes_profile_seq_show(struct seq_file *m, void *v)
  805. {
  806. struct trace_kprobe *tk = v;
  807. seq_printf(m, " %-44s %15lu %15lu\n",
  808. trace_event_name(&tk->tp.call),
  809. trace_kprobe_nhit(tk),
  810. tk->rp.kp.nmissed);
  811. return 0;
  812. }
  813. static const struct seq_operations profile_seq_op = {
  814. .start = probes_seq_start,
  815. .next = probes_seq_next,
  816. .stop = probes_seq_stop,
  817. .show = probes_profile_seq_show
  818. };
  819. static int profile_open(struct inode *inode, struct file *file)
  820. {
  821. return seq_open(file, &profile_seq_op);
  822. }
  823. static const struct file_operations kprobe_profile_ops = {
  824. .owner = THIS_MODULE,
  825. .open = profile_open,
  826. .read = seq_read,
  827. .llseek = seq_lseek,
  828. .release = seq_release,
  829. };
  830. /* Kprobe handler */
  831. static nokprobe_inline void
  832. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  833. struct trace_event_file *trace_file)
  834. {
  835. struct kprobe_trace_entry_head *entry;
  836. struct ring_buffer_event *event;
  837. struct ring_buffer *buffer;
  838. int size, dsize, pc;
  839. unsigned long irq_flags;
  840. struct trace_event_call *call = &tk->tp.call;
  841. WARN_ON(call != trace_file->event_call);
  842. if (trace_trigger_soft_disabled(trace_file))
  843. return;
  844. local_save_flags(irq_flags);
  845. pc = preempt_count();
  846. dsize = __get_data_size(&tk->tp, regs);
  847. size = sizeof(*entry) + tk->tp.size + dsize;
  848. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  849. call->event.type,
  850. size, irq_flags, pc);
  851. if (!event)
  852. return;
  853. entry = ring_buffer_event_data(event);
  854. entry->ip = (unsigned long)tk->rp.kp.addr;
  855. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  856. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  857. entry, irq_flags, pc, regs);
  858. }
  859. static void
  860. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  861. {
  862. struct event_file_link *link;
  863. list_for_each_entry_rcu(link, &tk->tp.files, list)
  864. __kprobe_trace_func(tk, regs, link->file);
  865. }
  866. NOKPROBE_SYMBOL(kprobe_trace_func);
  867. /* Kretprobe handler */
  868. static nokprobe_inline void
  869. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  870. struct pt_regs *regs,
  871. struct trace_event_file *trace_file)
  872. {
  873. struct kretprobe_trace_entry_head *entry;
  874. struct ring_buffer_event *event;
  875. struct ring_buffer *buffer;
  876. int size, pc, dsize;
  877. unsigned long irq_flags;
  878. struct trace_event_call *call = &tk->tp.call;
  879. WARN_ON(call != trace_file->event_call);
  880. if (trace_trigger_soft_disabled(trace_file))
  881. return;
  882. local_save_flags(irq_flags);
  883. pc = preempt_count();
  884. dsize = __get_data_size(&tk->tp, regs);
  885. size = sizeof(*entry) + tk->tp.size + dsize;
  886. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  887. call->event.type,
  888. size, irq_flags, pc);
  889. if (!event)
  890. return;
  891. entry = ring_buffer_event_data(event);
  892. entry->func = (unsigned long)tk->rp.kp.addr;
  893. entry->ret_ip = (unsigned long)ri->ret_addr;
  894. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  895. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  896. entry, irq_flags, pc, regs);
  897. }
  898. static void
  899. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  900. struct pt_regs *regs)
  901. {
  902. struct event_file_link *link;
  903. list_for_each_entry_rcu(link, &tk->tp.files, list)
  904. __kretprobe_trace_func(tk, ri, regs, link->file);
  905. }
  906. NOKPROBE_SYMBOL(kretprobe_trace_func);
  907. /* Event entry printers */
  908. static enum print_line_t
  909. print_kprobe_event(struct trace_iterator *iter, int flags,
  910. struct trace_event *event)
  911. {
  912. struct kprobe_trace_entry_head *field;
  913. struct trace_seq *s = &iter->seq;
  914. struct trace_probe *tp;
  915. u8 *data;
  916. int i;
  917. field = (struct kprobe_trace_entry_head *)iter->ent;
  918. tp = container_of(event, struct trace_probe, call.event);
  919. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  920. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  921. goto out;
  922. trace_seq_putc(s, ')');
  923. data = (u8 *)&field[1];
  924. for (i = 0; i < tp->nr_args; i++)
  925. if (!tp->args[i].type->print(s, tp->args[i].name,
  926. data + tp->args[i].offset, field))
  927. goto out;
  928. trace_seq_putc(s, '\n');
  929. out:
  930. return trace_handle_return(s);
  931. }
  932. static enum print_line_t
  933. print_kretprobe_event(struct trace_iterator *iter, int flags,
  934. struct trace_event *event)
  935. {
  936. struct kretprobe_trace_entry_head *field;
  937. struct trace_seq *s = &iter->seq;
  938. struct trace_probe *tp;
  939. u8 *data;
  940. int i;
  941. field = (struct kretprobe_trace_entry_head *)iter->ent;
  942. tp = container_of(event, struct trace_probe, call.event);
  943. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  944. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  945. goto out;
  946. trace_seq_puts(s, " <- ");
  947. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  948. goto out;
  949. trace_seq_putc(s, ')');
  950. data = (u8 *)&field[1];
  951. for (i = 0; i < tp->nr_args; i++)
  952. if (!tp->args[i].type->print(s, tp->args[i].name,
  953. data + tp->args[i].offset, field))
  954. goto out;
  955. trace_seq_putc(s, '\n');
  956. out:
  957. return trace_handle_return(s);
  958. }
  959. static int kprobe_event_define_fields(struct trace_event_call *event_call)
  960. {
  961. int ret, i;
  962. struct kprobe_trace_entry_head field;
  963. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  964. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  965. /* Set argument names as fields */
  966. for (i = 0; i < tk->tp.nr_args; i++) {
  967. struct probe_arg *parg = &tk->tp.args[i];
  968. ret = trace_define_field(event_call, parg->type->fmttype,
  969. parg->name,
  970. sizeof(field) + parg->offset,
  971. parg->type->size,
  972. parg->type->is_signed,
  973. FILTER_OTHER);
  974. if (ret)
  975. return ret;
  976. }
  977. return 0;
  978. }
  979. static int kretprobe_event_define_fields(struct trace_event_call *event_call)
  980. {
  981. int ret, i;
  982. struct kretprobe_trace_entry_head field;
  983. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  984. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  985. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  986. /* Set argument names as fields */
  987. for (i = 0; i < tk->tp.nr_args; i++) {
  988. struct probe_arg *parg = &tk->tp.args[i];
  989. ret = trace_define_field(event_call, parg->type->fmttype,
  990. parg->name,
  991. sizeof(field) + parg->offset,
  992. parg->type->size,
  993. parg->type->is_signed,
  994. FILTER_OTHER);
  995. if (ret)
  996. return ret;
  997. }
  998. return 0;
  999. }
  1000. #ifdef CONFIG_PERF_EVENTS
  1001. /* Kprobe profile handler */
  1002. static void
  1003. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  1004. {
  1005. struct trace_event_call *call = &tk->tp.call;
  1006. struct kprobe_trace_entry_head *entry;
  1007. struct hlist_head *head;
  1008. int size, __size, dsize;
  1009. int rctx;
  1010. if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
  1011. return;
  1012. head = this_cpu_ptr(call->perf_events);
  1013. if (hlist_empty(head))
  1014. return;
  1015. dsize = __get_data_size(&tk->tp, regs);
  1016. __size = sizeof(*entry) + tk->tp.size + dsize;
  1017. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1018. size -= sizeof(u32);
  1019. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1020. if (!entry)
  1021. return;
  1022. entry->ip = (unsigned long)tk->rp.kp.addr;
  1023. memset(&entry[1], 0, dsize);
  1024. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1025. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1026. head, NULL, NULL);
  1027. }
  1028. NOKPROBE_SYMBOL(kprobe_perf_func);
  1029. /* Kretprobe profile handler */
  1030. static void
  1031. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  1032. struct pt_regs *regs)
  1033. {
  1034. struct trace_event_call *call = &tk->tp.call;
  1035. struct kretprobe_trace_entry_head *entry;
  1036. struct hlist_head *head;
  1037. int size, __size, dsize;
  1038. int rctx;
  1039. if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
  1040. return;
  1041. head = this_cpu_ptr(call->perf_events);
  1042. if (hlist_empty(head))
  1043. return;
  1044. dsize = __get_data_size(&tk->tp, regs);
  1045. __size = sizeof(*entry) + tk->tp.size + dsize;
  1046. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1047. size -= sizeof(u32);
  1048. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1049. if (!entry)
  1050. return;
  1051. entry->func = (unsigned long)tk->rp.kp.addr;
  1052. entry->ret_ip = (unsigned long)ri->ret_addr;
  1053. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1054. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1055. head, NULL, NULL);
  1056. }
  1057. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1058. #endif /* CONFIG_PERF_EVENTS */
  1059. /*
  1060. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1061. *
  1062. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1063. * lockless, but we can't race with this __init function.
  1064. */
  1065. static int kprobe_register(struct trace_event_call *event,
  1066. enum trace_reg type, void *data)
  1067. {
  1068. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1069. struct trace_event_file *file = data;
  1070. switch (type) {
  1071. case TRACE_REG_REGISTER:
  1072. return enable_trace_kprobe(tk, file);
  1073. case TRACE_REG_UNREGISTER:
  1074. return disable_trace_kprobe(tk, file);
  1075. #ifdef CONFIG_PERF_EVENTS
  1076. case TRACE_REG_PERF_REGISTER:
  1077. return enable_trace_kprobe(tk, NULL);
  1078. case TRACE_REG_PERF_UNREGISTER:
  1079. return disable_trace_kprobe(tk, NULL);
  1080. case TRACE_REG_PERF_OPEN:
  1081. case TRACE_REG_PERF_CLOSE:
  1082. case TRACE_REG_PERF_ADD:
  1083. case TRACE_REG_PERF_DEL:
  1084. return 0;
  1085. #endif
  1086. }
  1087. return 0;
  1088. }
  1089. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1090. {
  1091. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1092. raw_cpu_inc(*tk->nhit);
  1093. if (tk->tp.flags & TP_FLAG_TRACE)
  1094. kprobe_trace_func(tk, regs);
  1095. #ifdef CONFIG_PERF_EVENTS
  1096. if (tk->tp.flags & TP_FLAG_PROFILE)
  1097. kprobe_perf_func(tk, regs);
  1098. #endif
  1099. return 0; /* We don't tweek kernel, so just return 0 */
  1100. }
  1101. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1102. static int
  1103. kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1104. {
  1105. struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
  1106. raw_cpu_inc(*tk->nhit);
  1107. if (tk->tp.flags & TP_FLAG_TRACE)
  1108. kretprobe_trace_func(tk, ri, regs);
  1109. #ifdef CONFIG_PERF_EVENTS
  1110. if (tk->tp.flags & TP_FLAG_PROFILE)
  1111. kretprobe_perf_func(tk, ri, regs);
  1112. #endif
  1113. return 0; /* We don't tweek kernel, so just return 0 */
  1114. }
  1115. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1116. static struct trace_event_functions kretprobe_funcs = {
  1117. .trace = print_kretprobe_event
  1118. };
  1119. static struct trace_event_functions kprobe_funcs = {
  1120. .trace = print_kprobe_event
  1121. };
  1122. static int register_kprobe_event(struct trace_kprobe *tk)
  1123. {
  1124. struct trace_event_call *call = &tk->tp.call;
  1125. int ret;
  1126. /* Initialize trace_event_call */
  1127. INIT_LIST_HEAD(&call->class->fields);
  1128. if (trace_kprobe_is_return(tk)) {
  1129. call->event.funcs = &kretprobe_funcs;
  1130. call->class->define_fields = kretprobe_event_define_fields;
  1131. } else {
  1132. call->event.funcs = &kprobe_funcs;
  1133. call->class->define_fields = kprobe_event_define_fields;
  1134. }
  1135. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1136. return -ENOMEM;
  1137. ret = register_trace_event(&call->event);
  1138. if (!ret) {
  1139. kfree(call->print_fmt);
  1140. return -ENODEV;
  1141. }
  1142. call->flags = TRACE_EVENT_FL_KPROBE;
  1143. call->class->reg = kprobe_register;
  1144. call->data = tk;
  1145. ret = trace_add_event_call(call);
  1146. if (ret) {
  1147. pr_info("Failed to register kprobe event: %s\n",
  1148. trace_event_name(call));
  1149. kfree(call->print_fmt);
  1150. unregister_trace_event(&call->event);
  1151. }
  1152. return ret;
  1153. }
  1154. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1155. {
  1156. int ret;
  1157. /* tp->event is unregistered in trace_remove_event_call() */
  1158. ret = trace_remove_event_call(&tk->tp.call);
  1159. if (!ret)
  1160. kfree(tk->tp.call.print_fmt);
  1161. return ret;
  1162. }
  1163. /* Make a tracefs interface for controlling probe points */
  1164. static __init int init_kprobe_trace(void)
  1165. {
  1166. struct dentry *d_tracer;
  1167. struct dentry *entry;
  1168. if (register_module_notifier(&trace_kprobe_module_nb))
  1169. return -EINVAL;
  1170. d_tracer = tracing_init_dentry();
  1171. if (IS_ERR(d_tracer))
  1172. return 0;
  1173. entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
  1174. NULL, &kprobe_events_ops);
  1175. /* Event list interface */
  1176. if (!entry)
  1177. pr_warn("Could not create tracefs 'kprobe_events' entry\n");
  1178. /* Profile interface */
  1179. entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
  1180. NULL, &kprobe_profile_ops);
  1181. if (!entry)
  1182. pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
  1183. return 0;
  1184. }
  1185. fs_initcall(init_kprobe_trace);
  1186. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1187. /*
  1188. * The "__used" keeps gcc from removing the function symbol
  1189. * from the kallsyms table. 'noinline' makes sure that there
  1190. * isn't an inlined version used by the test method below
  1191. */
  1192. static __used __init noinline int
  1193. kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
  1194. {
  1195. return a1 + a2 + a3 + a4 + a5 + a6;
  1196. }
  1197. static __init struct trace_event_file *
  1198. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1199. {
  1200. struct trace_event_file *file;
  1201. list_for_each_entry(file, &tr->events, list)
  1202. if (file->event_call == &tk->tp.call)
  1203. return file;
  1204. return NULL;
  1205. }
  1206. /*
  1207. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1208. * stage, we can do this lockless.
  1209. */
  1210. static __init int kprobe_trace_self_tests_init(void)
  1211. {
  1212. int ret, warn = 0;
  1213. int (*target)(int, int, int, int, int, int);
  1214. struct trace_kprobe *tk;
  1215. struct trace_event_file *file;
  1216. if (tracing_is_disabled())
  1217. return -ENODEV;
  1218. target = kprobe_trace_selftest_target;
  1219. pr_info("Testing kprobe tracing: ");
  1220. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1221. "$stack $stack0 +0($stack)",
  1222. create_trace_kprobe);
  1223. if (WARN_ON_ONCE(ret)) {
  1224. pr_warn("error on probing function entry.\n");
  1225. warn++;
  1226. } else {
  1227. /* Enable trace point */
  1228. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1229. if (WARN_ON_ONCE(tk == NULL)) {
  1230. pr_warn("error on getting new probe.\n");
  1231. warn++;
  1232. } else {
  1233. file = find_trace_probe_file(tk, top_trace_array());
  1234. if (WARN_ON_ONCE(file == NULL)) {
  1235. pr_warn("error on getting probe file.\n");
  1236. warn++;
  1237. } else
  1238. enable_trace_kprobe(tk, file);
  1239. }
  1240. }
  1241. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1242. "$retval", create_trace_kprobe);
  1243. if (WARN_ON_ONCE(ret)) {
  1244. pr_warn("error on probing function return.\n");
  1245. warn++;
  1246. } else {
  1247. /* Enable trace point */
  1248. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1249. if (WARN_ON_ONCE(tk == NULL)) {
  1250. pr_warn("error on getting 2nd new probe.\n");
  1251. warn++;
  1252. } else {
  1253. file = find_trace_probe_file(tk, top_trace_array());
  1254. if (WARN_ON_ONCE(file == NULL)) {
  1255. pr_warn("error on getting probe file.\n");
  1256. warn++;
  1257. } else
  1258. enable_trace_kprobe(tk, file);
  1259. }
  1260. }
  1261. if (warn)
  1262. goto end;
  1263. ret = target(1, 2, 3, 4, 5, 6);
  1264. /*
  1265. * Not expecting an error here, the check is only to prevent the
  1266. * optimizer from removing the call to target() as otherwise there
  1267. * are no side-effects and the call is never performed.
  1268. */
  1269. if (ret != 21)
  1270. warn++;
  1271. /* Disable trace points before removing it */
  1272. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1273. if (WARN_ON_ONCE(tk == NULL)) {
  1274. pr_warn("error on getting test probe.\n");
  1275. warn++;
  1276. } else {
  1277. if (trace_kprobe_nhit(tk) != 1) {
  1278. pr_warn("incorrect number of testprobe hits\n");
  1279. warn++;
  1280. }
  1281. file = find_trace_probe_file(tk, top_trace_array());
  1282. if (WARN_ON_ONCE(file == NULL)) {
  1283. pr_warn("error on getting probe file.\n");
  1284. warn++;
  1285. } else
  1286. disable_trace_kprobe(tk, file);
  1287. }
  1288. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1289. if (WARN_ON_ONCE(tk == NULL)) {
  1290. pr_warn("error on getting 2nd test probe.\n");
  1291. warn++;
  1292. } else {
  1293. if (trace_kprobe_nhit(tk) != 1) {
  1294. pr_warn("incorrect number of testprobe2 hits\n");
  1295. warn++;
  1296. }
  1297. file = find_trace_probe_file(tk, top_trace_array());
  1298. if (WARN_ON_ONCE(file == NULL)) {
  1299. pr_warn("error on getting probe file.\n");
  1300. warn++;
  1301. } else
  1302. disable_trace_kprobe(tk, file);
  1303. }
  1304. ret = traceprobe_command("-:testprobe", create_trace_kprobe);
  1305. if (WARN_ON_ONCE(ret)) {
  1306. pr_warn("error on deleting a probe.\n");
  1307. warn++;
  1308. }
  1309. ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
  1310. if (WARN_ON_ONCE(ret)) {
  1311. pr_warn("error on deleting a probe.\n");
  1312. warn++;
  1313. }
  1314. end:
  1315. release_all_trace_kprobes();
  1316. /*
  1317. * Wait for the optimizer work to finish. Otherwise it might fiddle
  1318. * with probes in already freed __init text.
  1319. */
  1320. wait_for_kprobe_optimizer();
  1321. if (warn)
  1322. pr_cont("NG: Some tests are failed. Please check them.\n");
  1323. else
  1324. pr_cont("OK\n");
  1325. return 0;
  1326. }
  1327. late_initcall(kprobe_trace_self_tests_init);
  1328. #endif