trace_kprobe.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /*
  2. * Kprobes-based tracing events
  3. *
  4. * Created by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/perf_event.h>
  31. #include <linux/stringify.h>
  32. #include <asm/bitsperlong.h>
  33. #include "trace.h"
  34. #include "trace_output.h"
  35. #define MAX_TRACE_ARGS 128
  36. #define MAX_ARGSTR_LEN 63
  37. #define MAX_EVENT_NAME_LEN 64
  38. #define KPROBE_EVENT_SYSTEM "kprobes"
  39. /* Reserved field names */
  40. #define FIELD_STRING_IP "__probe_ip"
  41. #define FIELD_STRING_RETIP "__probe_ret_ip"
  42. #define FIELD_STRING_FUNC "__probe_func"
  43. const char *reserved_field_names[] = {
  44. "common_type",
  45. "common_flags",
  46. "common_preempt_count",
  47. "common_pid",
  48. "common_tgid",
  49. "common_lock_depth",
  50. FIELD_STRING_IP,
  51. FIELD_STRING_RETIP,
  52. FIELD_STRING_FUNC,
  53. };
  54. /* Printing function type */
  55. typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *);
  56. #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
  57. #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
  58. /* Printing in basic type function template */
  59. #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
  60. static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
  61. const char *name, void *data)\
  62. { \
  63. return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
  64. } \
  65. static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
  66. DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
  67. DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
  68. DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
  69. DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
  70. DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
  71. DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
  72. DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
  73. DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
  74. /* Data fetch function type */
  75. typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
  76. struct fetch_param {
  77. fetch_func_t fn;
  78. void *data;
  79. };
  80. static __kprobes void call_fetch(struct fetch_param *fprm,
  81. struct pt_regs *regs, void *dest)
  82. {
  83. return fprm->fn(regs, fprm->data, dest);
  84. }
  85. #define FETCH_FUNC_NAME(kind, type) fetch_##kind##_##type
  86. /*
  87. * Define macro for basic types - we don't need to define s* types, because
  88. * we have to care only about bitwidth at recording time.
  89. */
  90. #define DEFINE_BASIC_FETCH_FUNCS(kind) \
  91. DEFINE_FETCH_##kind(u8) \
  92. DEFINE_FETCH_##kind(u16) \
  93. DEFINE_FETCH_##kind(u32) \
  94. DEFINE_FETCH_##kind(u64)
  95. #define CHECK_BASIC_FETCH_FUNCS(kind, fn) \
  96. ((FETCH_FUNC_NAME(kind, u8) == fn) || \
  97. (FETCH_FUNC_NAME(kind, u16) == fn) || \
  98. (FETCH_FUNC_NAME(kind, u32) == fn) || \
  99. (FETCH_FUNC_NAME(kind, u64) == fn))
  100. /* Data fetch function templates */
  101. #define DEFINE_FETCH_reg(type) \
  102. static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
  103. void *offset, void *dest) \
  104. { \
  105. *(type *)dest = (type)regs_get_register(regs, \
  106. (unsigned int)((unsigned long)offset)); \
  107. }
  108. DEFINE_BASIC_FETCH_FUNCS(reg)
  109. #define DEFINE_FETCH_stack(type) \
  110. static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
  111. void *offset, void *dest) \
  112. { \
  113. *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
  114. (unsigned int)((unsigned long)offset)); \
  115. }
  116. DEFINE_BASIC_FETCH_FUNCS(stack)
  117. #define DEFINE_FETCH_retval(type) \
  118. static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
  119. void *dummy, void *dest) \
  120. { \
  121. *(type *)dest = (type)regs_return_value(regs); \
  122. }
  123. DEFINE_BASIC_FETCH_FUNCS(retval)
  124. #define DEFINE_FETCH_memory(type) \
  125. static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
  126. void *addr, void *dest) \
  127. { \
  128. type retval; \
  129. if (probe_kernel_address(addr, retval)) \
  130. *(type *)dest = 0; \
  131. else \
  132. *(type *)dest = retval; \
  133. }
  134. DEFINE_BASIC_FETCH_FUNCS(memory)
  135. /* Memory fetching by symbol */
  136. struct symbol_cache {
  137. char *symbol;
  138. long offset;
  139. unsigned long addr;
  140. };
  141. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  142. {
  143. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  144. if (sc->addr)
  145. sc->addr += sc->offset;
  146. return sc->addr;
  147. }
  148. static void free_symbol_cache(struct symbol_cache *sc)
  149. {
  150. kfree(sc->symbol);
  151. kfree(sc);
  152. }
  153. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  154. {
  155. struct symbol_cache *sc;
  156. if (!sym || strlen(sym) == 0)
  157. return NULL;
  158. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  159. if (!sc)
  160. return NULL;
  161. sc->symbol = kstrdup(sym, GFP_KERNEL);
  162. if (!sc->symbol) {
  163. kfree(sc);
  164. return NULL;
  165. }
  166. sc->offset = offset;
  167. update_symbol_cache(sc);
  168. return sc;
  169. }
  170. #define DEFINE_FETCH_symbol(type) \
  171. static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
  172. void *data, void *dest) \
  173. { \
  174. struct symbol_cache *sc = data; \
  175. if (sc->addr) \
  176. fetch_memory_##type(regs, (void *)sc->addr, dest); \
  177. else \
  178. *(type *)dest = 0; \
  179. }
  180. DEFINE_BASIC_FETCH_FUNCS(symbol)
  181. /* Dereference memory access function */
  182. struct deref_fetch_param {
  183. struct fetch_param orig;
  184. long offset;
  185. };
  186. #define DEFINE_FETCH_deref(type) \
  187. static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
  188. void *data, void *dest) \
  189. { \
  190. struct deref_fetch_param *dprm = data; \
  191. unsigned long addr; \
  192. call_fetch(&dprm->orig, regs, &addr); \
  193. if (addr) { \
  194. addr += dprm->offset; \
  195. fetch_memory_##type(regs, (void *)addr, dest); \
  196. } else \
  197. *(type *)dest = 0; \
  198. }
  199. DEFINE_BASIC_FETCH_FUNCS(deref)
  200. static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
  201. {
  202. if (CHECK_BASIC_FETCH_FUNCS(deref, data->orig.fn))
  203. free_deref_fetch_param(data->orig.data);
  204. else if (CHECK_BASIC_FETCH_FUNCS(symbol, data->orig.fn))
  205. free_symbol_cache(data->orig.data);
  206. kfree(data);
  207. }
  208. /* Default (unsigned long) fetch type */
  209. #define __DEFAULT_FETCH_TYPE(t) u##t
  210. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  211. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  212. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  213. #define ASSIGN_FETCH_FUNC(kind, type) \
  214. .kind = FETCH_FUNC_NAME(kind, type)
  215. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  216. {.name = #ptype, \
  217. .size = sizeof(ftype), \
  218. .is_signed = sign, \
  219. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  220. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  221. ASSIGN_FETCH_FUNC(reg, ftype), \
  222. ASSIGN_FETCH_FUNC(stack, ftype), \
  223. ASSIGN_FETCH_FUNC(retval, ftype), \
  224. ASSIGN_FETCH_FUNC(memory, ftype), \
  225. ASSIGN_FETCH_FUNC(symbol, ftype), \
  226. ASSIGN_FETCH_FUNC(deref, ftype), \
  227. }
  228. /* Fetch type information table */
  229. static const struct fetch_type {
  230. const char *name; /* Name of type */
  231. size_t size; /* Byte size of type */
  232. int is_signed; /* Signed flag */
  233. print_type_func_t print; /* Print functions */
  234. const char *fmt; /* Fromat string */
  235. /* Fetch functions */
  236. fetch_func_t reg;
  237. fetch_func_t stack;
  238. fetch_func_t retval;
  239. fetch_func_t memory;
  240. fetch_func_t symbol;
  241. fetch_func_t deref;
  242. } fetch_type_table[] = {
  243. ASSIGN_FETCH_TYPE(u8, u8, 0),
  244. ASSIGN_FETCH_TYPE(u16, u16, 0),
  245. ASSIGN_FETCH_TYPE(u32, u32, 0),
  246. ASSIGN_FETCH_TYPE(u64, u64, 0),
  247. ASSIGN_FETCH_TYPE(s8, u8, 1),
  248. ASSIGN_FETCH_TYPE(s16, u16, 1),
  249. ASSIGN_FETCH_TYPE(s32, u32, 1),
  250. ASSIGN_FETCH_TYPE(s64, u64, 1),
  251. };
  252. static const struct fetch_type *find_fetch_type(const char *type)
  253. {
  254. int i;
  255. if (!type)
  256. type = DEFAULT_FETCH_TYPE_STR;
  257. for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
  258. if (strcmp(type, fetch_type_table[i].name) == 0)
  259. return &fetch_type_table[i];
  260. return NULL;
  261. }
  262. /* Special function : only accept unsigned long */
  263. static __kprobes void fetch_stack_address(struct pt_regs *regs,
  264. void *dummy, void *dest)
  265. {
  266. *(unsigned long *)dest = kernel_stack_pointer(regs);
  267. }
  268. /**
  269. * Kprobe event core functions
  270. */
  271. struct probe_arg {
  272. struct fetch_param fetch;
  273. unsigned int offset; /* Offset from argument entry */
  274. const char *name; /* Name of this argument */
  275. const char *comm; /* Command of this argument */
  276. const struct fetch_type *type; /* Type of this argument */
  277. };
  278. /* Flags for trace_probe */
  279. #define TP_FLAG_TRACE 1
  280. #define TP_FLAG_PROFILE 2
  281. struct trace_probe {
  282. struct list_head list;
  283. struct kretprobe rp; /* Use rp.kp for kprobe use */
  284. unsigned long nhit;
  285. unsigned int flags; /* For TP_FLAG_* */
  286. const char *symbol; /* symbol name */
  287. struct ftrace_event_call call;
  288. struct trace_event event;
  289. ssize_t size; /* trace entry size */
  290. unsigned int nr_args;
  291. struct probe_arg args[];
  292. };
  293. #define SIZEOF_TRACE_PROBE(n) \
  294. (offsetof(struct trace_probe, args) + \
  295. (sizeof(struct probe_arg) * (n)))
  296. static __kprobes int probe_is_return(struct trace_probe *tp)
  297. {
  298. return tp->rp.handler != NULL;
  299. }
  300. static __kprobes const char *probe_symbol(struct trace_probe *tp)
  301. {
  302. return tp->symbol ? tp->symbol : "unknown";
  303. }
  304. static int register_probe_event(struct trace_probe *tp);
  305. static void unregister_probe_event(struct trace_probe *tp);
  306. static DEFINE_MUTEX(probe_lock);
  307. static LIST_HEAD(probe_list);
  308. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  309. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  310. struct pt_regs *regs);
  311. /* Check the name is good for event/group */
  312. static int check_event_name(const char *name)
  313. {
  314. if (!isalpha(*name) && *name != '_')
  315. return 0;
  316. while (*++name != '\0') {
  317. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  318. return 0;
  319. }
  320. return 1;
  321. }
  322. /*
  323. * Allocate new trace_probe and initialize it (including kprobes).
  324. */
  325. static struct trace_probe *alloc_trace_probe(const char *group,
  326. const char *event,
  327. void *addr,
  328. const char *symbol,
  329. unsigned long offs,
  330. int nargs, int is_return)
  331. {
  332. struct trace_probe *tp;
  333. int ret = -ENOMEM;
  334. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  335. if (!tp)
  336. return ERR_PTR(ret);
  337. if (symbol) {
  338. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  339. if (!tp->symbol)
  340. goto error;
  341. tp->rp.kp.symbol_name = tp->symbol;
  342. tp->rp.kp.offset = offs;
  343. } else
  344. tp->rp.kp.addr = addr;
  345. if (is_return)
  346. tp->rp.handler = kretprobe_dispatcher;
  347. else
  348. tp->rp.kp.pre_handler = kprobe_dispatcher;
  349. if (!event || !check_event_name(event)) {
  350. ret = -EINVAL;
  351. goto error;
  352. }
  353. tp->call.name = kstrdup(event, GFP_KERNEL);
  354. if (!tp->call.name)
  355. goto error;
  356. if (!group || !check_event_name(group)) {
  357. ret = -EINVAL;
  358. goto error;
  359. }
  360. tp->call.system = kstrdup(group, GFP_KERNEL);
  361. if (!tp->call.system)
  362. goto error;
  363. INIT_LIST_HEAD(&tp->list);
  364. return tp;
  365. error:
  366. kfree(tp->call.name);
  367. kfree(tp->symbol);
  368. kfree(tp);
  369. return ERR_PTR(ret);
  370. }
  371. static void free_probe_arg(struct probe_arg *arg)
  372. {
  373. if (CHECK_BASIC_FETCH_FUNCS(deref, arg->fetch.fn))
  374. free_deref_fetch_param(arg->fetch.data);
  375. else if (CHECK_BASIC_FETCH_FUNCS(symbol, arg->fetch.fn))
  376. free_symbol_cache(arg->fetch.data);
  377. kfree(arg->name);
  378. kfree(arg->comm);
  379. }
  380. static void free_trace_probe(struct trace_probe *tp)
  381. {
  382. int i;
  383. for (i = 0; i < tp->nr_args; i++)
  384. free_probe_arg(&tp->args[i]);
  385. kfree(tp->call.system);
  386. kfree(tp->call.name);
  387. kfree(tp->symbol);
  388. kfree(tp);
  389. }
  390. static struct trace_probe *find_probe_event(const char *event,
  391. const char *group)
  392. {
  393. struct trace_probe *tp;
  394. list_for_each_entry(tp, &probe_list, list)
  395. if (strcmp(tp->call.name, event) == 0 &&
  396. strcmp(tp->call.system, group) == 0)
  397. return tp;
  398. return NULL;
  399. }
  400. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  401. static void unregister_trace_probe(struct trace_probe *tp)
  402. {
  403. if (probe_is_return(tp))
  404. unregister_kretprobe(&tp->rp);
  405. else
  406. unregister_kprobe(&tp->rp.kp);
  407. list_del(&tp->list);
  408. unregister_probe_event(tp);
  409. }
  410. /* Register a trace_probe and probe_event */
  411. static int register_trace_probe(struct trace_probe *tp)
  412. {
  413. struct trace_probe *old_tp;
  414. int ret;
  415. mutex_lock(&probe_lock);
  416. /* register as an event */
  417. old_tp = find_probe_event(tp->call.name, tp->call.system);
  418. if (old_tp) {
  419. /* delete old event */
  420. unregister_trace_probe(old_tp);
  421. free_trace_probe(old_tp);
  422. }
  423. ret = register_probe_event(tp);
  424. if (ret) {
  425. pr_warning("Faild to register probe event(%d)\n", ret);
  426. goto end;
  427. }
  428. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  429. if (probe_is_return(tp))
  430. ret = register_kretprobe(&tp->rp);
  431. else
  432. ret = register_kprobe(&tp->rp.kp);
  433. if (ret) {
  434. pr_warning("Could not insert probe(%d)\n", ret);
  435. if (ret == -EILSEQ) {
  436. pr_warning("Probing address(0x%p) is not an "
  437. "instruction boundary.\n",
  438. tp->rp.kp.addr);
  439. ret = -EINVAL;
  440. }
  441. unregister_probe_event(tp);
  442. } else
  443. list_add_tail(&tp->list, &probe_list);
  444. end:
  445. mutex_unlock(&probe_lock);
  446. return ret;
  447. }
  448. /* Split symbol and offset. */
  449. static int split_symbol_offset(char *symbol, unsigned long *offset)
  450. {
  451. char *tmp;
  452. int ret;
  453. if (!offset)
  454. return -EINVAL;
  455. tmp = strchr(symbol, '+');
  456. if (tmp) {
  457. /* skip sign because strict_strtol doesn't accept '+' */
  458. ret = strict_strtoul(tmp + 1, 0, offset);
  459. if (ret)
  460. return ret;
  461. *tmp = '\0';
  462. } else
  463. *offset = 0;
  464. return 0;
  465. }
  466. #define PARAM_MAX_ARGS 16
  467. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  468. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  469. struct fetch_param *f, int is_return)
  470. {
  471. int ret = 0;
  472. unsigned long param;
  473. if (strcmp(arg, "retval") == 0) {
  474. if (is_return)
  475. f->fn = t->retval;
  476. else
  477. ret = -EINVAL;
  478. } else if (strncmp(arg, "stack", 5) == 0) {
  479. if (arg[5] == '\0') {
  480. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
  481. f->fn = fetch_stack_address;
  482. else
  483. ret = -EINVAL;
  484. } else if (isdigit(arg[5])) {
  485. ret = strict_strtoul(arg + 5, 10, &param);
  486. if (ret || param > PARAM_MAX_STACK)
  487. ret = -EINVAL;
  488. else {
  489. f->fn = t->stack;
  490. f->data = (void *)param;
  491. }
  492. } else
  493. ret = -EINVAL;
  494. } else
  495. ret = -EINVAL;
  496. return ret;
  497. }
  498. /* Recursive argument parser */
  499. static int __parse_probe_arg(char *arg, const struct fetch_type *t,
  500. struct fetch_param *f, int is_return)
  501. {
  502. int ret = 0;
  503. unsigned long param;
  504. long offset;
  505. char *tmp;
  506. switch (arg[0]) {
  507. case '$':
  508. ret = parse_probe_vars(arg + 1, t, f, is_return);
  509. break;
  510. case '%': /* named register */
  511. ret = regs_query_register_offset(arg + 1);
  512. if (ret >= 0) {
  513. f->fn = t->reg;
  514. f->data = (void *)(unsigned long)ret;
  515. ret = 0;
  516. }
  517. break;
  518. case '@': /* memory or symbol */
  519. if (isdigit(arg[1])) {
  520. ret = strict_strtoul(arg + 1, 0, &param);
  521. if (ret)
  522. break;
  523. f->fn = t->memory;
  524. f->data = (void *)param;
  525. } else {
  526. ret = split_symbol_offset(arg + 1, &offset);
  527. if (ret)
  528. break;
  529. f->data = alloc_symbol_cache(arg + 1, offset);
  530. if (f->data)
  531. f->fn = t->symbol;
  532. }
  533. break;
  534. case '+': /* deref memory */
  535. case '-':
  536. tmp = strchr(arg, '(');
  537. if (!tmp)
  538. break;
  539. *tmp = '\0';
  540. ret = strict_strtol(arg + 1, 0, &offset);
  541. if (ret)
  542. break;
  543. if (arg[0] == '-')
  544. offset = -offset;
  545. arg = tmp + 1;
  546. tmp = strrchr(arg, ')');
  547. if (tmp) {
  548. struct deref_fetch_param *dprm;
  549. const struct fetch_type *t2 = find_fetch_type(NULL);
  550. *tmp = '\0';
  551. dprm = kzalloc(sizeof(struct deref_fetch_param),
  552. GFP_KERNEL);
  553. if (!dprm)
  554. return -ENOMEM;
  555. dprm->offset = offset;
  556. ret = __parse_probe_arg(arg, t2, &dprm->orig,
  557. is_return);
  558. if (ret)
  559. kfree(dprm);
  560. else {
  561. f->fn = t->deref;
  562. f->data = (void *)dprm;
  563. }
  564. }
  565. break;
  566. }
  567. if (!ret && !f->fn)
  568. ret = -EINVAL;
  569. return ret;
  570. }
  571. /* String length checking wrapper */
  572. static int parse_probe_arg(char *arg, struct trace_probe *tp,
  573. struct probe_arg *parg, int is_return)
  574. {
  575. const char *t;
  576. if (strlen(arg) > MAX_ARGSTR_LEN) {
  577. pr_info("Argument is too long.: %s\n", arg);
  578. return -ENOSPC;
  579. }
  580. parg->comm = kstrdup(arg, GFP_KERNEL);
  581. if (!parg->comm) {
  582. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  583. return -ENOMEM;
  584. }
  585. t = strchr(parg->comm, ':');
  586. if (t) {
  587. arg[t - parg->comm] = '\0';
  588. t++;
  589. }
  590. parg->type = find_fetch_type(t);
  591. if (!parg->type) {
  592. pr_info("Unsupported type: %s\n", t);
  593. return -EINVAL;
  594. }
  595. parg->offset = tp->size;
  596. tp->size += parg->type->size;
  597. return __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
  598. }
  599. /* Return 1 if name is reserved or already used by another argument */
  600. static int conflict_field_name(const char *name,
  601. struct probe_arg *args, int narg)
  602. {
  603. int i;
  604. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  605. if (strcmp(reserved_field_names[i], name) == 0)
  606. return 1;
  607. for (i = 0; i < narg; i++)
  608. if (strcmp(args[i].name, name) == 0)
  609. return 1;
  610. return 0;
  611. }
  612. static int create_trace_probe(int argc, char **argv)
  613. {
  614. /*
  615. * Argument syntax:
  616. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  617. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  618. * Fetch args:
  619. * $retval : fetch return value
  620. * $stack : fetch stack address
  621. * $stackN : fetch Nth of stack (N:0-)
  622. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  623. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  624. * %REG : fetch register REG
  625. * Dereferencing memory fetch:
  626. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  627. * Alias name of args:
  628. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  629. * Type of args:
  630. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  631. */
  632. struct trace_probe *tp;
  633. int i, ret = 0;
  634. int is_return = 0, is_delete = 0;
  635. char *symbol = NULL, *event = NULL, *group = NULL;
  636. char *arg, *tmp;
  637. unsigned long offset = 0;
  638. void *addr = NULL;
  639. char buf[MAX_EVENT_NAME_LEN];
  640. /* argc must be >= 1 */
  641. if (argv[0][0] == 'p')
  642. is_return = 0;
  643. else if (argv[0][0] == 'r')
  644. is_return = 1;
  645. else if (argv[0][0] == '-')
  646. is_delete = 1;
  647. else {
  648. pr_info("Probe definition must be started with 'p', 'r' or"
  649. " '-'.\n");
  650. return -EINVAL;
  651. }
  652. if (argv[0][1] == ':') {
  653. event = &argv[0][2];
  654. if (strchr(event, '/')) {
  655. group = event;
  656. event = strchr(group, '/') + 1;
  657. event[-1] = '\0';
  658. if (strlen(group) == 0) {
  659. pr_info("Group name is not specified\n");
  660. return -EINVAL;
  661. }
  662. }
  663. if (strlen(event) == 0) {
  664. pr_info("Event name is not specified\n");
  665. return -EINVAL;
  666. }
  667. }
  668. if (!group)
  669. group = KPROBE_EVENT_SYSTEM;
  670. if (is_delete) {
  671. if (!event) {
  672. pr_info("Delete command needs an event name.\n");
  673. return -EINVAL;
  674. }
  675. tp = find_probe_event(event, group);
  676. if (!tp) {
  677. pr_info("Event %s/%s doesn't exist.\n", group, event);
  678. return -ENOENT;
  679. }
  680. /* delete an event */
  681. unregister_trace_probe(tp);
  682. free_trace_probe(tp);
  683. return 0;
  684. }
  685. if (argc < 2) {
  686. pr_info("Probe point is not specified.\n");
  687. return -EINVAL;
  688. }
  689. if (isdigit(argv[1][0])) {
  690. if (is_return) {
  691. pr_info("Return probe point must be a symbol.\n");
  692. return -EINVAL;
  693. }
  694. /* an address specified */
  695. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  696. if (ret) {
  697. pr_info("Failed to parse address.\n");
  698. return ret;
  699. }
  700. } else {
  701. /* a symbol specified */
  702. symbol = argv[1];
  703. /* TODO: support .init module functions */
  704. ret = split_symbol_offset(symbol, &offset);
  705. if (ret) {
  706. pr_info("Failed to parse symbol.\n");
  707. return ret;
  708. }
  709. if (offset && is_return) {
  710. pr_info("Return probe must be used without offset.\n");
  711. return -EINVAL;
  712. }
  713. }
  714. argc -= 2; argv += 2;
  715. /* setup a probe */
  716. if (!event) {
  717. /* Make a new event name */
  718. if (symbol)
  719. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  720. is_return ? 'r' : 'p', symbol, offset);
  721. else
  722. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  723. is_return ? 'r' : 'p', addr);
  724. event = buf;
  725. }
  726. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  727. is_return);
  728. if (IS_ERR(tp)) {
  729. pr_info("Failed to allocate trace_probe.(%d)\n",
  730. (int)PTR_ERR(tp));
  731. return PTR_ERR(tp);
  732. }
  733. /* parse arguments */
  734. ret = 0;
  735. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  736. /* Parse argument name */
  737. arg = strchr(argv[i], '=');
  738. if (arg)
  739. *arg++ = '\0';
  740. else
  741. arg = argv[i];
  742. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  743. if (!tp->args[i].name) {
  744. pr_info("Failed to allocate argument%d name '%s'.\n",
  745. i, argv[i]);
  746. ret = -ENOMEM;
  747. goto error;
  748. }
  749. tmp = strchr(tp->args[i].name, ':');
  750. if (tmp)
  751. *tmp = '_'; /* convert : to _ */
  752. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  753. pr_info("Argument%d name '%s' conflicts with "
  754. "another field.\n", i, argv[i]);
  755. ret = -EINVAL;
  756. goto error;
  757. }
  758. /* Parse fetch argument */
  759. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  760. if (ret) {
  761. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  762. kfree(tp->args[i].name);
  763. goto error;
  764. }
  765. tp->nr_args++;
  766. }
  767. ret = register_trace_probe(tp);
  768. if (ret)
  769. goto error;
  770. return 0;
  771. error:
  772. free_trace_probe(tp);
  773. return ret;
  774. }
  775. static void cleanup_all_probes(void)
  776. {
  777. struct trace_probe *tp;
  778. mutex_lock(&probe_lock);
  779. /* TODO: Use batch unregistration */
  780. while (!list_empty(&probe_list)) {
  781. tp = list_entry(probe_list.next, struct trace_probe, list);
  782. unregister_trace_probe(tp);
  783. free_trace_probe(tp);
  784. }
  785. mutex_unlock(&probe_lock);
  786. }
  787. /* Probes listing interfaces */
  788. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  789. {
  790. mutex_lock(&probe_lock);
  791. return seq_list_start(&probe_list, *pos);
  792. }
  793. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  794. {
  795. return seq_list_next(v, &probe_list, pos);
  796. }
  797. static void probes_seq_stop(struct seq_file *m, void *v)
  798. {
  799. mutex_unlock(&probe_lock);
  800. }
  801. static int probes_seq_show(struct seq_file *m, void *v)
  802. {
  803. struct trace_probe *tp = v;
  804. int i;
  805. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  806. seq_printf(m, ":%s/%s", tp->call.system, tp->call.name);
  807. if (!tp->symbol)
  808. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  809. else if (tp->rp.kp.offset)
  810. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  811. else
  812. seq_printf(m, " %s", probe_symbol(tp));
  813. for (i = 0; i < tp->nr_args; i++)
  814. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  815. seq_printf(m, "\n");
  816. return 0;
  817. }
  818. static const struct seq_operations probes_seq_op = {
  819. .start = probes_seq_start,
  820. .next = probes_seq_next,
  821. .stop = probes_seq_stop,
  822. .show = probes_seq_show
  823. };
  824. static int probes_open(struct inode *inode, struct file *file)
  825. {
  826. if ((file->f_mode & FMODE_WRITE) &&
  827. (file->f_flags & O_TRUNC))
  828. cleanup_all_probes();
  829. return seq_open(file, &probes_seq_op);
  830. }
  831. static int command_trace_probe(const char *buf)
  832. {
  833. char **argv;
  834. int argc = 0, ret = 0;
  835. argv = argv_split(GFP_KERNEL, buf, &argc);
  836. if (!argv)
  837. return -ENOMEM;
  838. if (argc)
  839. ret = create_trace_probe(argc, argv);
  840. argv_free(argv);
  841. return ret;
  842. }
  843. #define WRITE_BUFSIZE 128
  844. static ssize_t probes_write(struct file *file, const char __user *buffer,
  845. size_t count, loff_t *ppos)
  846. {
  847. char *kbuf, *tmp;
  848. int ret;
  849. size_t done;
  850. size_t size;
  851. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  852. if (!kbuf)
  853. return -ENOMEM;
  854. ret = done = 0;
  855. while (done < count) {
  856. size = count - done;
  857. if (size >= WRITE_BUFSIZE)
  858. size = WRITE_BUFSIZE - 1;
  859. if (copy_from_user(kbuf, buffer + done, size)) {
  860. ret = -EFAULT;
  861. goto out;
  862. }
  863. kbuf[size] = '\0';
  864. tmp = strchr(kbuf, '\n');
  865. if (tmp) {
  866. *tmp = '\0';
  867. size = tmp - kbuf + 1;
  868. } else if (done + size < count) {
  869. pr_warning("Line length is too long: "
  870. "Should be less than %d.", WRITE_BUFSIZE);
  871. ret = -EINVAL;
  872. goto out;
  873. }
  874. done += size;
  875. /* Remove comments */
  876. tmp = strchr(kbuf, '#');
  877. if (tmp)
  878. *tmp = '\0';
  879. ret = command_trace_probe(kbuf);
  880. if (ret)
  881. goto out;
  882. }
  883. ret = done;
  884. out:
  885. kfree(kbuf);
  886. return ret;
  887. }
  888. static const struct file_operations kprobe_events_ops = {
  889. .owner = THIS_MODULE,
  890. .open = probes_open,
  891. .read = seq_read,
  892. .llseek = seq_lseek,
  893. .release = seq_release,
  894. .write = probes_write,
  895. };
  896. /* Probes profiling interfaces */
  897. static int probes_profile_seq_show(struct seq_file *m, void *v)
  898. {
  899. struct trace_probe *tp = v;
  900. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  901. tp->rp.kp.nmissed);
  902. return 0;
  903. }
  904. static const struct seq_operations profile_seq_op = {
  905. .start = probes_seq_start,
  906. .next = probes_seq_next,
  907. .stop = probes_seq_stop,
  908. .show = probes_profile_seq_show
  909. };
  910. static int profile_open(struct inode *inode, struct file *file)
  911. {
  912. return seq_open(file, &profile_seq_op);
  913. }
  914. static const struct file_operations kprobe_profile_ops = {
  915. .owner = THIS_MODULE,
  916. .open = profile_open,
  917. .read = seq_read,
  918. .llseek = seq_lseek,
  919. .release = seq_release,
  920. };
  921. /* Kprobe handler */
  922. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  923. {
  924. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  925. struct kprobe_trace_entry_head *entry;
  926. struct ring_buffer_event *event;
  927. struct ring_buffer *buffer;
  928. u8 *data;
  929. int size, i, pc;
  930. unsigned long irq_flags;
  931. struct ftrace_event_call *call = &tp->call;
  932. tp->nhit++;
  933. local_save_flags(irq_flags);
  934. pc = preempt_count();
  935. size = sizeof(*entry) + tp->size;
  936. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  937. irq_flags, pc);
  938. if (!event)
  939. return;
  940. entry = ring_buffer_event_data(event);
  941. entry->ip = (unsigned long)kp->addr;
  942. data = (u8 *)&entry[1];
  943. for (i = 0; i < tp->nr_args; i++)
  944. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  945. if (!filter_current_check_discard(buffer, call, entry, event))
  946. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  947. }
  948. /* Kretprobe handler */
  949. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  950. struct pt_regs *regs)
  951. {
  952. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  953. struct kretprobe_trace_entry_head *entry;
  954. struct ring_buffer_event *event;
  955. struct ring_buffer *buffer;
  956. u8 *data;
  957. int size, i, pc;
  958. unsigned long irq_flags;
  959. struct ftrace_event_call *call = &tp->call;
  960. local_save_flags(irq_flags);
  961. pc = preempt_count();
  962. size = sizeof(*entry) + tp->size;
  963. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  964. irq_flags, pc);
  965. if (!event)
  966. return;
  967. entry = ring_buffer_event_data(event);
  968. entry->func = (unsigned long)tp->rp.kp.addr;
  969. entry->ret_ip = (unsigned long)ri->ret_addr;
  970. data = (u8 *)&entry[1];
  971. for (i = 0; i < tp->nr_args; i++)
  972. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  973. if (!filter_current_check_discard(buffer, call, entry, event))
  974. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  975. }
  976. /* Event entry printers */
  977. enum print_line_t
  978. print_kprobe_event(struct trace_iterator *iter, int flags)
  979. {
  980. struct kprobe_trace_entry_head *field;
  981. struct trace_seq *s = &iter->seq;
  982. struct trace_event *event;
  983. struct trace_probe *tp;
  984. u8 *data;
  985. int i;
  986. field = (struct kprobe_trace_entry_head *)iter->ent;
  987. event = ftrace_find_event(field->ent.type);
  988. tp = container_of(event, struct trace_probe, event);
  989. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  990. goto partial;
  991. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  992. goto partial;
  993. if (!trace_seq_puts(s, ")"))
  994. goto partial;
  995. data = (u8 *)&field[1];
  996. for (i = 0; i < tp->nr_args; i++)
  997. if (!tp->args[i].type->print(s, tp->args[i].name,
  998. data + tp->args[i].offset))
  999. goto partial;
  1000. if (!trace_seq_puts(s, "\n"))
  1001. goto partial;
  1002. return TRACE_TYPE_HANDLED;
  1003. partial:
  1004. return TRACE_TYPE_PARTIAL_LINE;
  1005. }
  1006. enum print_line_t
  1007. print_kretprobe_event(struct trace_iterator *iter, int flags)
  1008. {
  1009. struct kretprobe_trace_entry_head *field;
  1010. struct trace_seq *s = &iter->seq;
  1011. struct trace_event *event;
  1012. struct trace_probe *tp;
  1013. u8 *data;
  1014. int i;
  1015. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1016. event = ftrace_find_event(field->ent.type);
  1017. tp = container_of(event, struct trace_probe, event);
  1018. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1019. goto partial;
  1020. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1021. goto partial;
  1022. if (!trace_seq_puts(s, " <- "))
  1023. goto partial;
  1024. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1025. goto partial;
  1026. if (!trace_seq_puts(s, ")"))
  1027. goto partial;
  1028. data = (u8 *)&field[1];
  1029. for (i = 0; i < tp->nr_args; i++)
  1030. if (!tp->args[i].type->print(s, tp->args[i].name,
  1031. data + tp->args[i].offset))
  1032. goto partial;
  1033. if (!trace_seq_puts(s, "\n"))
  1034. goto partial;
  1035. return TRACE_TYPE_HANDLED;
  1036. partial:
  1037. return TRACE_TYPE_PARTIAL_LINE;
  1038. }
  1039. static int probe_event_enable(struct ftrace_event_call *call)
  1040. {
  1041. struct trace_probe *tp = (struct trace_probe *)call->data;
  1042. tp->flags |= TP_FLAG_TRACE;
  1043. if (probe_is_return(tp))
  1044. return enable_kretprobe(&tp->rp);
  1045. else
  1046. return enable_kprobe(&tp->rp.kp);
  1047. }
  1048. static void probe_event_disable(struct ftrace_event_call *call)
  1049. {
  1050. struct trace_probe *tp = (struct trace_probe *)call->data;
  1051. tp->flags &= ~TP_FLAG_TRACE;
  1052. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  1053. if (probe_is_return(tp))
  1054. disable_kretprobe(&tp->rp);
  1055. else
  1056. disable_kprobe(&tp->rp.kp);
  1057. }
  1058. }
  1059. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  1060. {
  1061. INIT_LIST_HEAD(&event_call->fields);
  1062. return 0;
  1063. }
  1064. #undef DEFINE_FIELD
  1065. #define DEFINE_FIELD(type, item, name, is_signed) \
  1066. do { \
  1067. ret = trace_define_field(event_call, #type, name, \
  1068. offsetof(typeof(field), item), \
  1069. sizeof(field.item), is_signed, \
  1070. FILTER_OTHER); \
  1071. if (ret) \
  1072. return ret; \
  1073. } while (0)
  1074. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1075. {
  1076. int ret, i;
  1077. struct kprobe_trace_entry_head field;
  1078. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1079. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1080. /* Set argument names as fields */
  1081. for (i = 0; i < tp->nr_args; i++) {
  1082. ret = trace_define_field(event_call, tp->args[i].type->name,
  1083. tp->args[i].name,
  1084. sizeof(field) + tp->args[i].offset,
  1085. tp->args[i].type->size,
  1086. tp->args[i].type->is_signed,
  1087. FILTER_OTHER);
  1088. if (ret)
  1089. return ret;
  1090. }
  1091. return 0;
  1092. }
  1093. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1094. {
  1095. int ret, i;
  1096. struct kretprobe_trace_entry_head field;
  1097. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1098. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1099. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1100. /* Set argument names as fields */
  1101. for (i = 0; i < tp->nr_args; i++) {
  1102. ret = trace_define_field(event_call, tp->args[i].type->name,
  1103. tp->args[i].name,
  1104. sizeof(field) + tp->args[i].offset,
  1105. tp->args[i].type->size,
  1106. tp->args[i].type->is_signed,
  1107. FILTER_OTHER);
  1108. if (ret)
  1109. return ret;
  1110. }
  1111. return 0;
  1112. }
  1113. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1114. {
  1115. int i;
  1116. int pos = 0;
  1117. const char *fmt, *arg;
  1118. if (!probe_is_return(tp)) {
  1119. fmt = "(%lx)";
  1120. arg = "REC->" FIELD_STRING_IP;
  1121. } else {
  1122. fmt = "(%lx <- %lx)";
  1123. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1124. }
  1125. /* When len=0, we just calculate the needed length */
  1126. #define LEN_OR_ZERO (len ? len - pos : 0)
  1127. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1128. for (i = 0; i < tp->nr_args; i++) {
  1129. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1130. tp->args[i].name, tp->args[i].type->fmt);
  1131. }
  1132. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1133. for (i = 0; i < tp->nr_args; i++) {
  1134. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1135. tp->args[i].name);
  1136. }
  1137. #undef LEN_OR_ZERO
  1138. /* return the length of print_fmt */
  1139. return pos;
  1140. }
  1141. static int set_print_fmt(struct trace_probe *tp)
  1142. {
  1143. int len;
  1144. char *print_fmt;
  1145. /* First: called with 0 length to calculate the needed length */
  1146. len = __set_print_fmt(tp, NULL, 0);
  1147. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1148. if (!print_fmt)
  1149. return -ENOMEM;
  1150. /* Second: actually write the @print_fmt */
  1151. __set_print_fmt(tp, print_fmt, len + 1);
  1152. tp->call.print_fmt = print_fmt;
  1153. return 0;
  1154. }
  1155. #ifdef CONFIG_PERF_EVENTS
  1156. /* Kprobe profile handler */
  1157. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1158. struct pt_regs *regs)
  1159. {
  1160. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1161. struct ftrace_event_call *call = &tp->call;
  1162. struct kprobe_trace_entry_head *entry;
  1163. struct hlist_head *head;
  1164. u8 *data;
  1165. int size, __size, i;
  1166. int rctx;
  1167. __size = sizeof(*entry) + tp->size;
  1168. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1169. size -= sizeof(u32);
  1170. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1171. "profile buffer not large enough"))
  1172. return;
  1173. entry = perf_trace_buf_prepare(size, call->id, regs, &rctx);
  1174. if (!entry)
  1175. return;
  1176. entry->ip = (unsigned long)kp->addr;
  1177. data = (u8 *)&entry[1];
  1178. for (i = 0; i < tp->nr_args; i++)
  1179. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  1180. head = per_cpu_ptr(call->perf_events, smp_processor_id());
  1181. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1182. }
  1183. /* Kretprobe profile handler */
  1184. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1185. struct pt_regs *regs)
  1186. {
  1187. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1188. struct ftrace_event_call *call = &tp->call;
  1189. struct kretprobe_trace_entry_head *entry;
  1190. struct hlist_head *head;
  1191. u8 *data;
  1192. int size, __size, i;
  1193. int rctx;
  1194. __size = sizeof(*entry) + tp->size;
  1195. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1196. size -= sizeof(u32);
  1197. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1198. "profile buffer not large enough"))
  1199. return;
  1200. entry = perf_trace_buf_prepare(size, call->id, regs, &rctx);
  1201. if (!entry)
  1202. return;
  1203. entry->func = (unsigned long)tp->rp.kp.addr;
  1204. entry->ret_ip = (unsigned long)ri->ret_addr;
  1205. data = (u8 *)&entry[1];
  1206. for (i = 0; i < tp->nr_args; i++)
  1207. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  1208. head = per_cpu_ptr(call->perf_events, smp_processor_id());
  1209. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1210. }
  1211. static int probe_perf_enable(struct ftrace_event_call *call)
  1212. {
  1213. struct trace_probe *tp = (struct trace_probe *)call->data;
  1214. tp->flags |= TP_FLAG_PROFILE;
  1215. if (probe_is_return(tp))
  1216. return enable_kretprobe(&tp->rp);
  1217. else
  1218. return enable_kprobe(&tp->rp.kp);
  1219. }
  1220. static void probe_perf_disable(struct ftrace_event_call *call)
  1221. {
  1222. struct trace_probe *tp = (struct trace_probe *)call->data;
  1223. tp->flags &= ~TP_FLAG_PROFILE;
  1224. if (!(tp->flags & TP_FLAG_TRACE)) {
  1225. if (probe_is_return(tp))
  1226. disable_kretprobe(&tp->rp);
  1227. else
  1228. disable_kprobe(&tp->rp.kp);
  1229. }
  1230. }
  1231. #endif /* CONFIG_PERF_EVENTS */
  1232. static __kprobes
  1233. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1234. {
  1235. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1236. if (tp->flags & TP_FLAG_TRACE)
  1237. kprobe_trace_func(kp, regs);
  1238. #ifdef CONFIG_PERF_EVENTS
  1239. if (tp->flags & TP_FLAG_PROFILE)
  1240. kprobe_perf_func(kp, regs);
  1241. #endif
  1242. return 0; /* We don't tweek kernel, so just return 0 */
  1243. }
  1244. static __kprobes
  1245. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1246. {
  1247. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1248. if (tp->flags & TP_FLAG_TRACE)
  1249. kretprobe_trace_func(ri, regs);
  1250. #ifdef CONFIG_PERF_EVENTS
  1251. if (tp->flags & TP_FLAG_PROFILE)
  1252. kretprobe_perf_func(ri, regs);
  1253. #endif
  1254. return 0; /* We don't tweek kernel, so just return 0 */
  1255. }
  1256. static int register_probe_event(struct trace_probe *tp)
  1257. {
  1258. struct ftrace_event_call *call = &tp->call;
  1259. int ret;
  1260. /* Initialize ftrace_event_call */
  1261. if (probe_is_return(tp)) {
  1262. tp->event.trace = print_kretprobe_event;
  1263. call->raw_init = probe_event_raw_init;
  1264. call->define_fields = kretprobe_event_define_fields;
  1265. } else {
  1266. tp->event.trace = print_kprobe_event;
  1267. call->raw_init = probe_event_raw_init;
  1268. call->define_fields = kprobe_event_define_fields;
  1269. }
  1270. if (set_print_fmt(tp) < 0)
  1271. return -ENOMEM;
  1272. call->event = &tp->event;
  1273. call->id = register_ftrace_event(&tp->event);
  1274. if (!call->id) {
  1275. kfree(call->print_fmt);
  1276. return -ENODEV;
  1277. }
  1278. call->enabled = 0;
  1279. call->regfunc = probe_event_enable;
  1280. call->unregfunc = probe_event_disable;
  1281. #ifdef CONFIG_PERF_EVENTS
  1282. call->perf_event_enable = probe_perf_enable;
  1283. call->perf_event_disable = probe_perf_disable;
  1284. #endif
  1285. call->data = tp;
  1286. ret = trace_add_event_call(call);
  1287. if (ret) {
  1288. pr_info("Failed to register kprobe event: %s\n", call->name);
  1289. kfree(call->print_fmt);
  1290. unregister_ftrace_event(&tp->event);
  1291. }
  1292. return ret;
  1293. }
  1294. static void unregister_probe_event(struct trace_probe *tp)
  1295. {
  1296. /* tp->event is unregistered in trace_remove_event_call() */
  1297. trace_remove_event_call(&tp->call);
  1298. kfree(tp->call.print_fmt);
  1299. }
  1300. /* Make a debugfs interface for controling probe points */
  1301. static __init int init_kprobe_trace(void)
  1302. {
  1303. struct dentry *d_tracer;
  1304. struct dentry *entry;
  1305. d_tracer = tracing_init_dentry();
  1306. if (!d_tracer)
  1307. return 0;
  1308. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1309. NULL, &kprobe_events_ops);
  1310. /* Event list interface */
  1311. if (!entry)
  1312. pr_warning("Could not create debugfs "
  1313. "'kprobe_events' entry\n");
  1314. /* Profile interface */
  1315. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1316. NULL, &kprobe_profile_ops);
  1317. if (!entry)
  1318. pr_warning("Could not create debugfs "
  1319. "'kprobe_profile' entry\n");
  1320. return 0;
  1321. }
  1322. fs_initcall(init_kprobe_trace);
  1323. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1324. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1325. int a4, int a5, int a6)
  1326. {
  1327. return a1 + a2 + a3 + a4 + a5 + a6;
  1328. }
  1329. static __init int kprobe_trace_self_tests_init(void)
  1330. {
  1331. int ret, warn = 0;
  1332. int (*target)(int, int, int, int, int, int);
  1333. struct trace_probe *tp;
  1334. target = kprobe_trace_selftest_target;
  1335. pr_info("Testing kprobe tracing: ");
  1336. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1337. "$stack $stack0 +0($stack)");
  1338. if (WARN_ON_ONCE(ret)) {
  1339. pr_warning("error on probing function entry.\n");
  1340. warn++;
  1341. } else {
  1342. /* Enable trace point */
  1343. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1344. if (WARN_ON_ONCE(tp == NULL)) {
  1345. pr_warning("error on getting new probe.\n");
  1346. warn++;
  1347. } else
  1348. probe_event_enable(&tp->call);
  1349. }
  1350. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1351. "$retval");
  1352. if (WARN_ON_ONCE(ret)) {
  1353. pr_warning("error on probing function return.\n");
  1354. warn++;
  1355. } else {
  1356. /* Enable trace point */
  1357. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1358. if (WARN_ON_ONCE(tp == NULL)) {
  1359. pr_warning("error on getting new probe.\n");
  1360. warn++;
  1361. } else
  1362. probe_event_enable(&tp->call);
  1363. }
  1364. if (warn)
  1365. goto end;
  1366. ret = target(1, 2, 3, 4, 5, 6);
  1367. ret = command_trace_probe("-:testprobe");
  1368. if (WARN_ON_ONCE(ret)) {
  1369. pr_warning("error on deleting a probe.\n");
  1370. warn++;
  1371. }
  1372. ret = command_trace_probe("-:testprobe2");
  1373. if (WARN_ON_ONCE(ret)) {
  1374. pr_warning("error on deleting a probe.\n");
  1375. warn++;
  1376. }
  1377. end:
  1378. cleanup_all_probes();
  1379. if (warn)
  1380. pr_cont("NG: Some tests are failed. Please check them.\n");
  1381. else
  1382. pr_cont("OK\n");
  1383. return 0;
  1384. }
  1385. late_initcall(kprobe_trace_self_tests_init);
  1386. #endif