trace_kprobe.c 39 KB

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