trace_kprobe.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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_class class;
  288. struct ftrace_event_call call;
  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.class = &tp->class;
  354. tp->call.name = kstrdup(event, GFP_KERNEL);
  355. if (!tp->call.name)
  356. goto error;
  357. if (!group || !check_event_name(group)) {
  358. ret = -EINVAL;
  359. goto error;
  360. }
  361. tp->class.system = kstrdup(group, GFP_KERNEL);
  362. if (!tp->class.system)
  363. goto error;
  364. INIT_LIST_HEAD(&tp->list);
  365. return tp;
  366. error:
  367. kfree(tp->call.name);
  368. kfree(tp->symbol);
  369. kfree(tp);
  370. return ERR_PTR(ret);
  371. }
  372. static void free_probe_arg(struct probe_arg *arg)
  373. {
  374. if (CHECK_BASIC_FETCH_FUNCS(deref, arg->fetch.fn))
  375. free_deref_fetch_param(arg->fetch.data);
  376. else if (CHECK_BASIC_FETCH_FUNCS(symbol, arg->fetch.fn))
  377. free_symbol_cache(arg->fetch.data);
  378. kfree(arg->name);
  379. kfree(arg->comm);
  380. }
  381. static void free_trace_probe(struct trace_probe *tp)
  382. {
  383. int i;
  384. for (i = 0; i < tp->nr_args; i++)
  385. free_probe_arg(&tp->args[i]);
  386. kfree(tp->call.class->system);
  387. kfree(tp->call.name);
  388. kfree(tp->symbol);
  389. kfree(tp);
  390. }
  391. static struct trace_probe *find_probe_event(const char *event,
  392. const char *group)
  393. {
  394. struct trace_probe *tp;
  395. list_for_each_entry(tp, &probe_list, list)
  396. if (strcmp(tp->call.name, event) == 0 &&
  397. strcmp(tp->call.class->system, group) == 0)
  398. return tp;
  399. return NULL;
  400. }
  401. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  402. static void unregister_trace_probe(struct trace_probe *tp)
  403. {
  404. if (probe_is_return(tp))
  405. unregister_kretprobe(&tp->rp);
  406. else
  407. unregister_kprobe(&tp->rp.kp);
  408. list_del(&tp->list);
  409. unregister_probe_event(tp);
  410. }
  411. /* Register a trace_probe and probe_event */
  412. static int register_trace_probe(struct trace_probe *tp)
  413. {
  414. struct trace_probe *old_tp;
  415. int ret;
  416. mutex_lock(&probe_lock);
  417. /* register as an event */
  418. old_tp = find_probe_event(tp->call.name, tp->call.class->system);
  419. if (old_tp) {
  420. /* delete old event */
  421. unregister_trace_probe(old_tp);
  422. free_trace_probe(old_tp);
  423. }
  424. ret = register_probe_event(tp);
  425. if (ret) {
  426. pr_warning("Faild to register probe event(%d)\n", ret);
  427. goto end;
  428. }
  429. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  430. if (probe_is_return(tp))
  431. ret = register_kretprobe(&tp->rp);
  432. else
  433. ret = register_kprobe(&tp->rp.kp);
  434. if (ret) {
  435. pr_warning("Could not insert probe(%d)\n", ret);
  436. if (ret == -EILSEQ) {
  437. pr_warning("Probing address(0x%p) is not an "
  438. "instruction boundary.\n",
  439. tp->rp.kp.addr);
  440. ret = -EINVAL;
  441. }
  442. unregister_probe_event(tp);
  443. } else
  444. list_add_tail(&tp->list, &probe_list);
  445. end:
  446. mutex_unlock(&probe_lock);
  447. return ret;
  448. }
  449. /* Split symbol and offset. */
  450. static int split_symbol_offset(char *symbol, unsigned long *offset)
  451. {
  452. char *tmp;
  453. int ret;
  454. if (!offset)
  455. return -EINVAL;
  456. tmp = strchr(symbol, '+');
  457. if (tmp) {
  458. /* skip sign because strict_strtol doesn't accept '+' */
  459. ret = strict_strtoul(tmp + 1, 0, offset);
  460. if (ret)
  461. return ret;
  462. *tmp = '\0';
  463. } else
  464. *offset = 0;
  465. return 0;
  466. }
  467. #define PARAM_MAX_ARGS 16
  468. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  469. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  470. struct fetch_param *f, int is_return)
  471. {
  472. int ret = 0;
  473. unsigned long param;
  474. if (strcmp(arg, "retval") == 0) {
  475. if (is_return)
  476. f->fn = t->retval;
  477. else
  478. ret = -EINVAL;
  479. } else if (strncmp(arg, "stack", 5) == 0) {
  480. if (arg[5] == '\0') {
  481. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
  482. f->fn = fetch_stack_address;
  483. else
  484. ret = -EINVAL;
  485. } else if (isdigit(arg[5])) {
  486. ret = strict_strtoul(arg + 5, 10, &param);
  487. if (ret || param > PARAM_MAX_STACK)
  488. ret = -EINVAL;
  489. else {
  490. f->fn = t->stack;
  491. f->data = (void *)param;
  492. }
  493. } else
  494. ret = -EINVAL;
  495. } else
  496. ret = -EINVAL;
  497. return ret;
  498. }
  499. /* Recursive argument parser */
  500. static int __parse_probe_arg(char *arg, const struct fetch_type *t,
  501. struct fetch_param *f, int is_return)
  502. {
  503. int ret = 0;
  504. unsigned long param;
  505. long offset;
  506. char *tmp;
  507. switch (arg[0]) {
  508. case '$':
  509. ret = parse_probe_vars(arg + 1, t, f, is_return);
  510. break;
  511. case '%': /* named register */
  512. ret = regs_query_register_offset(arg + 1);
  513. if (ret >= 0) {
  514. f->fn = t->reg;
  515. f->data = (void *)(unsigned long)ret;
  516. ret = 0;
  517. }
  518. break;
  519. case '@': /* memory or symbol */
  520. if (isdigit(arg[1])) {
  521. ret = strict_strtoul(arg + 1, 0, &param);
  522. if (ret)
  523. break;
  524. f->fn = t->memory;
  525. f->data = (void *)param;
  526. } else {
  527. ret = split_symbol_offset(arg + 1, &offset);
  528. if (ret)
  529. break;
  530. f->data = alloc_symbol_cache(arg + 1, offset);
  531. if (f->data)
  532. f->fn = t->symbol;
  533. }
  534. break;
  535. case '+': /* deref memory */
  536. case '-':
  537. tmp = strchr(arg, '(');
  538. if (!tmp)
  539. break;
  540. *tmp = '\0';
  541. ret = strict_strtol(arg + 1, 0, &offset);
  542. if (ret)
  543. break;
  544. if (arg[0] == '-')
  545. offset = -offset;
  546. arg = tmp + 1;
  547. tmp = strrchr(arg, ')');
  548. if (tmp) {
  549. struct deref_fetch_param *dprm;
  550. const struct fetch_type *t2 = find_fetch_type(NULL);
  551. *tmp = '\0';
  552. dprm = kzalloc(sizeof(struct deref_fetch_param),
  553. GFP_KERNEL);
  554. if (!dprm)
  555. return -ENOMEM;
  556. dprm->offset = offset;
  557. ret = __parse_probe_arg(arg, t2, &dprm->orig,
  558. is_return);
  559. if (ret)
  560. kfree(dprm);
  561. else {
  562. f->fn = t->deref;
  563. f->data = (void *)dprm;
  564. }
  565. }
  566. break;
  567. }
  568. if (!ret && !f->fn)
  569. ret = -EINVAL;
  570. return ret;
  571. }
  572. /* String length checking wrapper */
  573. static int parse_probe_arg(char *arg, struct trace_probe *tp,
  574. struct probe_arg *parg, int is_return)
  575. {
  576. const char *t;
  577. if (strlen(arg) > MAX_ARGSTR_LEN) {
  578. pr_info("Argument is too long.: %s\n", arg);
  579. return -ENOSPC;
  580. }
  581. parg->comm = kstrdup(arg, GFP_KERNEL);
  582. if (!parg->comm) {
  583. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  584. return -ENOMEM;
  585. }
  586. t = strchr(parg->comm, ':');
  587. if (t) {
  588. arg[t - parg->comm] = '\0';
  589. t++;
  590. }
  591. parg->type = find_fetch_type(t);
  592. if (!parg->type) {
  593. pr_info("Unsupported type: %s\n", t);
  594. return -EINVAL;
  595. }
  596. parg->offset = tp->size;
  597. tp->size += parg->type->size;
  598. return __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
  599. }
  600. /* Return 1 if name is reserved or already used by another argument */
  601. static int conflict_field_name(const char *name,
  602. struct probe_arg *args, int narg)
  603. {
  604. int i;
  605. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  606. if (strcmp(reserved_field_names[i], name) == 0)
  607. return 1;
  608. for (i = 0; i < narg; i++)
  609. if (strcmp(args[i].name, name) == 0)
  610. return 1;
  611. return 0;
  612. }
  613. static int create_trace_probe(int argc, char **argv)
  614. {
  615. /*
  616. * Argument syntax:
  617. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  618. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  619. * Fetch args:
  620. * $retval : fetch return value
  621. * $stack : fetch stack address
  622. * $stackN : fetch Nth of stack (N:0-)
  623. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  624. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  625. * %REG : fetch register REG
  626. * Dereferencing memory fetch:
  627. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  628. * Alias name of args:
  629. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  630. * Type of args:
  631. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  632. */
  633. struct trace_probe *tp;
  634. int i, ret = 0;
  635. int is_return = 0, is_delete = 0;
  636. char *symbol = NULL, *event = NULL, *group = NULL;
  637. char *arg, *tmp;
  638. unsigned long offset = 0;
  639. void *addr = NULL;
  640. char buf[MAX_EVENT_NAME_LEN];
  641. /* argc must be >= 1 */
  642. if (argv[0][0] == 'p')
  643. is_return = 0;
  644. else if (argv[0][0] == 'r')
  645. is_return = 1;
  646. else if (argv[0][0] == '-')
  647. is_delete = 1;
  648. else {
  649. pr_info("Probe definition must be started with 'p', 'r' or"
  650. " '-'.\n");
  651. return -EINVAL;
  652. }
  653. if (argv[0][1] == ':') {
  654. event = &argv[0][2];
  655. if (strchr(event, '/')) {
  656. group = event;
  657. event = strchr(group, '/') + 1;
  658. event[-1] = '\0';
  659. if (strlen(group) == 0) {
  660. pr_info("Group name is not specified\n");
  661. return -EINVAL;
  662. }
  663. }
  664. if (strlen(event) == 0) {
  665. pr_info("Event name is not specified\n");
  666. return -EINVAL;
  667. }
  668. }
  669. if (!group)
  670. group = KPROBE_EVENT_SYSTEM;
  671. if (is_delete) {
  672. if (!event) {
  673. pr_info("Delete command needs an event name.\n");
  674. return -EINVAL;
  675. }
  676. tp = find_probe_event(event, group);
  677. if (!tp) {
  678. pr_info("Event %s/%s doesn't exist.\n", group, event);
  679. return -ENOENT;
  680. }
  681. /* delete an event */
  682. unregister_trace_probe(tp);
  683. free_trace_probe(tp);
  684. return 0;
  685. }
  686. if (argc < 2) {
  687. pr_info("Probe point is not specified.\n");
  688. return -EINVAL;
  689. }
  690. if (isdigit(argv[1][0])) {
  691. if (is_return) {
  692. pr_info("Return probe point must be a symbol.\n");
  693. return -EINVAL;
  694. }
  695. /* an address specified */
  696. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  697. if (ret) {
  698. pr_info("Failed to parse address.\n");
  699. return ret;
  700. }
  701. } else {
  702. /* a symbol specified */
  703. symbol = argv[1];
  704. /* TODO: support .init module functions */
  705. ret = split_symbol_offset(symbol, &offset);
  706. if (ret) {
  707. pr_info("Failed to parse symbol.\n");
  708. return ret;
  709. }
  710. if (offset && is_return) {
  711. pr_info("Return probe must be used without offset.\n");
  712. return -EINVAL;
  713. }
  714. }
  715. argc -= 2; argv += 2;
  716. /* setup a probe */
  717. if (!event) {
  718. /* Make a new event name */
  719. if (symbol)
  720. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  721. is_return ? 'r' : 'p', symbol, offset);
  722. else
  723. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  724. is_return ? 'r' : 'p', addr);
  725. event = buf;
  726. }
  727. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  728. is_return);
  729. if (IS_ERR(tp)) {
  730. pr_info("Failed to allocate trace_probe.(%d)\n",
  731. (int)PTR_ERR(tp));
  732. return PTR_ERR(tp);
  733. }
  734. /* parse arguments */
  735. ret = 0;
  736. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  737. /* Parse argument name */
  738. arg = strchr(argv[i], '=');
  739. if (arg)
  740. *arg++ = '\0';
  741. else
  742. arg = argv[i];
  743. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  744. if (!tp->args[i].name) {
  745. pr_info("Failed to allocate argument%d name '%s'.\n",
  746. i, argv[i]);
  747. ret = -ENOMEM;
  748. goto error;
  749. }
  750. tmp = strchr(tp->args[i].name, ':');
  751. if (tmp)
  752. *tmp = '_'; /* convert : to _ */
  753. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  754. pr_info("Argument%d name '%s' conflicts with "
  755. "another field.\n", i, argv[i]);
  756. ret = -EINVAL;
  757. goto error;
  758. }
  759. /* Parse fetch argument */
  760. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  761. if (ret) {
  762. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  763. kfree(tp->args[i].name);
  764. goto error;
  765. }
  766. tp->nr_args++;
  767. }
  768. ret = register_trace_probe(tp);
  769. if (ret)
  770. goto error;
  771. return 0;
  772. error:
  773. free_trace_probe(tp);
  774. return ret;
  775. }
  776. static void cleanup_all_probes(void)
  777. {
  778. struct trace_probe *tp;
  779. mutex_lock(&probe_lock);
  780. /* TODO: Use batch unregistration */
  781. while (!list_empty(&probe_list)) {
  782. tp = list_entry(probe_list.next, struct trace_probe, list);
  783. unregister_trace_probe(tp);
  784. free_trace_probe(tp);
  785. }
  786. mutex_unlock(&probe_lock);
  787. }
  788. /* Probes listing interfaces */
  789. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  790. {
  791. mutex_lock(&probe_lock);
  792. return seq_list_start(&probe_list, *pos);
  793. }
  794. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  795. {
  796. return seq_list_next(v, &probe_list, pos);
  797. }
  798. static void probes_seq_stop(struct seq_file *m, void *v)
  799. {
  800. mutex_unlock(&probe_lock);
  801. }
  802. static int probes_seq_show(struct seq_file *m, void *v)
  803. {
  804. struct trace_probe *tp = v;
  805. int i;
  806. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  807. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  808. if (!tp->symbol)
  809. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  810. else if (tp->rp.kp.offset)
  811. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  812. else
  813. seq_printf(m, " %s", probe_symbol(tp));
  814. for (i = 0; i < tp->nr_args; i++)
  815. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  816. seq_printf(m, "\n");
  817. return 0;
  818. }
  819. static const struct seq_operations probes_seq_op = {
  820. .start = probes_seq_start,
  821. .next = probes_seq_next,
  822. .stop = probes_seq_stop,
  823. .show = probes_seq_show
  824. };
  825. static int probes_open(struct inode *inode, struct file *file)
  826. {
  827. if ((file->f_mode & FMODE_WRITE) &&
  828. (file->f_flags & O_TRUNC))
  829. cleanup_all_probes();
  830. return seq_open(file, &probes_seq_op);
  831. }
  832. static int command_trace_probe(const char *buf)
  833. {
  834. char **argv;
  835. int argc = 0, ret = 0;
  836. argv = argv_split(GFP_KERNEL, buf, &argc);
  837. if (!argv)
  838. return -ENOMEM;
  839. if (argc)
  840. ret = create_trace_probe(argc, argv);
  841. argv_free(argv);
  842. return ret;
  843. }
  844. #define WRITE_BUFSIZE 128
  845. static ssize_t probes_write(struct file *file, const char __user *buffer,
  846. size_t count, loff_t *ppos)
  847. {
  848. char *kbuf, *tmp;
  849. int ret;
  850. size_t done;
  851. size_t size;
  852. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  853. if (!kbuf)
  854. return -ENOMEM;
  855. ret = done = 0;
  856. while (done < count) {
  857. size = count - done;
  858. if (size >= WRITE_BUFSIZE)
  859. size = WRITE_BUFSIZE - 1;
  860. if (copy_from_user(kbuf, buffer + done, size)) {
  861. ret = -EFAULT;
  862. goto out;
  863. }
  864. kbuf[size] = '\0';
  865. tmp = strchr(kbuf, '\n');
  866. if (tmp) {
  867. *tmp = '\0';
  868. size = tmp - kbuf + 1;
  869. } else if (done + size < count) {
  870. pr_warning("Line length is too long: "
  871. "Should be less than %d.", WRITE_BUFSIZE);
  872. ret = -EINVAL;
  873. goto out;
  874. }
  875. done += size;
  876. /* Remove comments */
  877. tmp = strchr(kbuf, '#');
  878. if (tmp)
  879. *tmp = '\0';
  880. ret = command_trace_probe(kbuf);
  881. if (ret)
  882. goto out;
  883. }
  884. ret = done;
  885. out:
  886. kfree(kbuf);
  887. return ret;
  888. }
  889. static const struct file_operations kprobe_events_ops = {
  890. .owner = THIS_MODULE,
  891. .open = probes_open,
  892. .read = seq_read,
  893. .llseek = seq_lseek,
  894. .release = seq_release,
  895. .write = probes_write,
  896. };
  897. /* Probes profiling interfaces */
  898. static int probes_profile_seq_show(struct seq_file *m, void *v)
  899. {
  900. struct trace_probe *tp = v;
  901. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  902. tp->rp.kp.nmissed);
  903. return 0;
  904. }
  905. static const struct seq_operations profile_seq_op = {
  906. .start = probes_seq_start,
  907. .next = probes_seq_next,
  908. .stop = probes_seq_stop,
  909. .show = probes_profile_seq_show
  910. };
  911. static int profile_open(struct inode *inode, struct file *file)
  912. {
  913. return seq_open(file, &profile_seq_op);
  914. }
  915. static const struct file_operations kprobe_profile_ops = {
  916. .owner = THIS_MODULE,
  917. .open = profile_open,
  918. .read = seq_read,
  919. .llseek = seq_lseek,
  920. .release = seq_release,
  921. };
  922. /* Kprobe handler */
  923. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  924. {
  925. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  926. struct kprobe_trace_entry_head *entry;
  927. struct ring_buffer_event *event;
  928. struct ring_buffer *buffer;
  929. u8 *data;
  930. int size, i, pc;
  931. unsigned long irq_flags;
  932. struct ftrace_event_call *call = &tp->call;
  933. tp->nhit++;
  934. local_save_flags(irq_flags);
  935. pc = preempt_count();
  936. size = sizeof(*entry) + tp->size;
  937. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  938. size, irq_flags, pc);
  939. if (!event)
  940. return;
  941. entry = ring_buffer_event_data(event);
  942. entry->ip = (unsigned long)kp->addr;
  943. data = (u8 *)&entry[1];
  944. for (i = 0; i < tp->nr_args; i++)
  945. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  946. if (!filter_current_check_discard(buffer, call, entry, event))
  947. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  948. }
  949. /* Kretprobe handler */
  950. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  951. struct pt_regs *regs)
  952. {
  953. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  954. struct kretprobe_trace_entry_head *entry;
  955. struct ring_buffer_event *event;
  956. struct ring_buffer *buffer;
  957. u8 *data;
  958. int size, i, pc;
  959. unsigned long irq_flags;
  960. struct ftrace_event_call *call = &tp->call;
  961. local_save_flags(irq_flags);
  962. pc = preempt_count();
  963. size = sizeof(*entry) + tp->size;
  964. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  965. size, irq_flags, pc);
  966. if (!event)
  967. return;
  968. entry = ring_buffer_event_data(event);
  969. entry->func = (unsigned long)tp->rp.kp.addr;
  970. entry->ret_ip = (unsigned long)ri->ret_addr;
  971. data = (u8 *)&entry[1];
  972. for (i = 0; i < tp->nr_args; i++)
  973. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  974. if (!filter_current_check_discard(buffer, call, entry, event))
  975. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  976. }
  977. /* Event entry printers */
  978. enum print_line_t
  979. print_kprobe_event(struct trace_iterator *iter, int flags,
  980. struct trace_event *event)
  981. {
  982. struct kprobe_trace_entry_head *field;
  983. struct trace_seq *s = &iter->seq;
  984. struct trace_probe *tp;
  985. u8 *data;
  986. int i;
  987. field = (struct kprobe_trace_entry_head *)iter->ent;
  988. tp = container_of(event, struct trace_probe, call.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. struct trace_event *event)
  1009. {
  1010. struct kretprobe_trace_entry_head *field;
  1011. struct trace_seq *s = &iter->seq;
  1012. struct trace_probe *tp;
  1013. u8 *data;
  1014. int i;
  1015. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1016. tp = container_of(event, struct trace_probe, call.event);
  1017. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1018. goto partial;
  1019. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1020. goto partial;
  1021. if (!trace_seq_puts(s, " <- "))
  1022. goto partial;
  1023. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1024. goto partial;
  1025. if (!trace_seq_puts(s, ")"))
  1026. goto partial;
  1027. data = (u8 *)&field[1];
  1028. for (i = 0; i < tp->nr_args; i++)
  1029. if (!tp->args[i].type->print(s, tp->args[i].name,
  1030. data + tp->args[i].offset))
  1031. goto partial;
  1032. if (!trace_seq_puts(s, "\n"))
  1033. goto partial;
  1034. return TRACE_TYPE_HANDLED;
  1035. partial:
  1036. return TRACE_TYPE_PARTIAL_LINE;
  1037. }
  1038. static int probe_event_enable(struct ftrace_event_call *call)
  1039. {
  1040. struct trace_probe *tp = (struct trace_probe *)call->data;
  1041. tp->flags |= TP_FLAG_TRACE;
  1042. if (probe_is_return(tp))
  1043. return enable_kretprobe(&tp->rp);
  1044. else
  1045. return enable_kprobe(&tp->rp.kp);
  1046. }
  1047. static void probe_event_disable(struct ftrace_event_call *call)
  1048. {
  1049. struct trace_probe *tp = (struct trace_probe *)call->data;
  1050. tp->flags &= ~TP_FLAG_TRACE;
  1051. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  1052. if (probe_is_return(tp))
  1053. disable_kretprobe(&tp->rp);
  1054. else
  1055. disable_kprobe(&tp->rp.kp);
  1056. }
  1057. }
  1058. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  1059. {
  1060. return 0;
  1061. }
  1062. #undef DEFINE_FIELD
  1063. #define DEFINE_FIELD(type, item, name, is_signed) \
  1064. do { \
  1065. ret = trace_define_field(event_call, #type, name, \
  1066. offsetof(typeof(field), item), \
  1067. sizeof(field.item), is_signed, \
  1068. FILTER_OTHER); \
  1069. if (ret) \
  1070. return ret; \
  1071. } while (0)
  1072. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1073. {
  1074. int ret, i;
  1075. struct kprobe_trace_entry_head field;
  1076. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1077. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1078. /* Set argument names as fields */
  1079. for (i = 0; i < tp->nr_args; i++) {
  1080. ret = trace_define_field(event_call, tp->args[i].type->name,
  1081. tp->args[i].name,
  1082. sizeof(field) + tp->args[i].offset,
  1083. tp->args[i].type->size,
  1084. tp->args[i].type->is_signed,
  1085. FILTER_OTHER);
  1086. if (ret)
  1087. return ret;
  1088. }
  1089. return 0;
  1090. }
  1091. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1092. {
  1093. int ret, i;
  1094. struct kretprobe_trace_entry_head field;
  1095. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1096. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1097. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1098. /* Set argument names as fields */
  1099. for (i = 0; i < tp->nr_args; i++) {
  1100. ret = trace_define_field(event_call, tp->args[i].type->name,
  1101. tp->args[i].name,
  1102. sizeof(field) + tp->args[i].offset,
  1103. tp->args[i].type->size,
  1104. tp->args[i].type->is_signed,
  1105. FILTER_OTHER);
  1106. if (ret)
  1107. return ret;
  1108. }
  1109. return 0;
  1110. }
  1111. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1112. {
  1113. int i;
  1114. int pos = 0;
  1115. const char *fmt, *arg;
  1116. if (!probe_is_return(tp)) {
  1117. fmt = "(%lx)";
  1118. arg = "REC->" FIELD_STRING_IP;
  1119. } else {
  1120. fmt = "(%lx <- %lx)";
  1121. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1122. }
  1123. /* When len=0, we just calculate the needed length */
  1124. #define LEN_OR_ZERO (len ? len - pos : 0)
  1125. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1126. for (i = 0; i < tp->nr_args; i++) {
  1127. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1128. tp->args[i].name, tp->args[i].type->fmt);
  1129. }
  1130. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1131. for (i = 0; i < tp->nr_args; i++) {
  1132. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1133. tp->args[i].name);
  1134. }
  1135. #undef LEN_OR_ZERO
  1136. /* return the length of print_fmt */
  1137. return pos;
  1138. }
  1139. static int set_print_fmt(struct trace_probe *tp)
  1140. {
  1141. int len;
  1142. char *print_fmt;
  1143. /* First: called with 0 length to calculate the needed length */
  1144. len = __set_print_fmt(tp, NULL, 0);
  1145. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1146. if (!print_fmt)
  1147. return -ENOMEM;
  1148. /* Second: actually write the @print_fmt */
  1149. __set_print_fmt(tp, print_fmt, len + 1);
  1150. tp->call.print_fmt = print_fmt;
  1151. return 0;
  1152. }
  1153. #ifdef CONFIG_PERF_EVENTS
  1154. /* Kprobe profile handler */
  1155. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1156. struct pt_regs *regs)
  1157. {
  1158. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1159. struct ftrace_event_call *call = &tp->call;
  1160. struct kprobe_trace_entry_head *entry;
  1161. struct hlist_head *head;
  1162. u8 *data;
  1163. int size, __size, i;
  1164. int rctx;
  1165. __size = sizeof(*entry) + tp->size;
  1166. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1167. size -= sizeof(u32);
  1168. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1169. "profile buffer not large enough"))
  1170. return;
  1171. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1172. if (!entry)
  1173. return;
  1174. entry->ip = (unsigned long)kp->addr;
  1175. data = (u8 *)&entry[1];
  1176. for (i = 0; i < tp->nr_args; i++)
  1177. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  1178. head = this_cpu_ptr(call->perf_events);
  1179. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1180. }
  1181. /* Kretprobe profile handler */
  1182. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1183. struct pt_regs *regs)
  1184. {
  1185. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1186. struct ftrace_event_call *call = &tp->call;
  1187. struct kretprobe_trace_entry_head *entry;
  1188. struct hlist_head *head;
  1189. u8 *data;
  1190. int size, __size, i;
  1191. int rctx;
  1192. __size = sizeof(*entry) + tp->size;
  1193. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1194. size -= sizeof(u32);
  1195. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1196. "profile buffer not large enough"))
  1197. return;
  1198. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1199. if (!entry)
  1200. return;
  1201. entry->func = (unsigned long)tp->rp.kp.addr;
  1202. entry->ret_ip = (unsigned long)ri->ret_addr;
  1203. data = (u8 *)&entry[1];
  1204. for (i = 0; i < tp->nr_args; i++)
  1205. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  1206. head = this_cpu_ptr(call->perf_events);
  1207. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1208. }
  1209. static int probe_perf_enable(struct ftrace_event_call *call)
  1210. {
  1211. struct trace_probe *tp = (struct trace_probe *)call->data;
  1212. tp->flags |= TP_FLAG_PROFILE;
  1213. if (probe_is_return(tp))
  1214. return enable_kretprobe(&tp->rp);
  1215. else
  1216. return enable_kprobe(&tp->rp.kp);
  1217. }
  1218. static void probe_perf_disable(struct ftrace_event_call *call)
  1219. {
  1220. struct trace_probe *tp = (struct trace_probe *)call->data;
  1221. tp->flags &= ~TP_FLAG_PROFILE;
  1222. if (!(tp->flags & TP_FLAG_TRACE)) {
  1223. if (probe_is_return(tp))
  1224. disable_kretprobe(&tp->rp);
  1225. else
  1226. disable_kprobe(&tp->rp.kp);
  1227. }
  1228. }
  1229. #endif /* CONFIG_PERF_EVENTS */
  1230. static __kprobes
  1231. int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
  1232. {
  1233. switch (type) {
  1234. case TRACE_REG_REGISTER:
  1235. return probe_event_enable(event);
  1236. case TRACE_REG_UNREGISTER:
  1237. probe_event_disable(event);
  1238. return 0;
  1239. #ifdef CONFIG_PERF_EVENTS
  1240. case TRACE_REG_PERF_REGISTER:
  1241. return probe_perf_enable(event);
  1242. case TRACE_REG_PERF_UNREGISTER:
  1243. probe_perf_disable(event);
  1244. return 0;
  1245. #endif
  1246. }
  1247. return 0;
  1248. }
  1249. static __kprobes
  1250. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1251. {
  1252. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1253. if (tp->flags & TP_FLAG_TRACE)
  1254. kprobe_trace_func(kp, regs);
  1255. #ifdef CONFIG_PERF_EVENTS
  1256. if (tp->flags & TP_FLAG_PROFILE)
  1257. kprobe_perf_func(kp, regs);
  1258. #endif
  1259. return 0; /* We don't tweek kernel, so just return 0 */
  1260. }
  1261. static __kprobes
  1262. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1263. {
  1264. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1265. if (tp->flags & TP_FLAG_TRACE)
  1266. kretprobe_trace_func(ri, regs);
  1267. #ifdef CONFIG_PERF_EVENTS
  1268. if (tp->flags & TP_FLAG_PROFILE)
  1269. kretprobe_perf_func(ri, regs);
  1270. #endif
  1271. return 0; /* We don't tweek kernel, so just return 0 */
  1272. }
  1273. static struct trace_event_functions kretprobe_funcs = {
  1274. .trace = print_kretprobe_event
  1275. };
  1276. static struct trace_event_functions kprobe_funcs = {
  1277. .trace = print_kprobe_event
  1278. };
  1279. static int register_probe_event(struct trace_probe *tp)
  1280. {
  1281. struct ftrace_event_call *call = &tp->call;
  1282. int ret;
  1283. /* Initialize ftrace_event_call */
  1284. if (probe_is_return(tp)) {
  1285. INIT_LIST_HEAD(&call->class->fields);
  1286. call->event.funcs = &kretprobe_funcs;
  1287. call->class->raw_init = probe_event_raw_init;
  1288. call->class->define_fields = kretprobe_event_define_fields;
  1289. } else {
  1290. INIT_LIST_HEAD(&call->class->fields);
  1291. call->event.funcs = &kprobe_funcs;
  1292. call->class->raw_init = probe_event_raw_init;
  1293. call->class->define_fields = kprobe_event_define_fields;
  1294. }
  1295. if (set_print_fmt(tp) < 0)
  1296. return -ENOMEM;
  1297. ret = register_ftrace_event(&call->event);
  1298. if (!ret) {
  1299. kfree(call->print_fmt);
  1300. return -ENODEV;
  1301. }
  1302. call->flags = 0;
  1303. call->class->reg = kprobe_register;
  1304. call->data = tp;
  1305. ret = trace_add_event_call(call);
  1306. if (ret) {
  1307. pr_info("Failed to register kprobe event: %s\n", call->name);
  1308. kfree(call->print_fmt);
  1309. unregister_ftrace_event(&call->event);
  1310. }
  1311. return ret;
  1312. }
  1313. static void unregister_probe_event(struct trace_probe *tp)
  1314. {
  1315. /* tp->event is unregistered in trace_remove_event_call() */
  1316. trace_remove_event_call(&tp->call);
  1317. kfree(tp->call.print_fmt);
  1318. }
  1319. /* Make a debugfs interface for controling probe points */
  1320. static __init int init_kprobe_trace(void)
  1321. {
  1322. struct dentry *d_tracer;
  1323. struct dentry *entry;
  1324. d_tracer = tracing_init_dentry();
  1325. if (!d_tracer)
  1326. return 0;
  1327. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1328. NULL, &kprobe_events_ops);
  1329. /* Event list interface */
  1330. if (!entry)
  1331. pr_warning("Could not create debugfs "
  1332. "'kprobe_events' entry\n");
  1333. /* Profile interface */
  1334. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1335. NULL, &kprobe_profile_ops);
  1336. if (!entry)
  1337. pr_warning("Could not create debugfs "
  1338. "'kprobe_profile' entry\n");
  1339. return 0;
  1340. }
  1341. fs_initcall(init_kprobe_trace);
  1342. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1343. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1344. int a4, int a5, int a6)
  1345. {
  1346. return a1 + a2 + a3 + a4 + a5 + a6;
  1347. }
  1348. static __init int kprobe_trace_self_tests_init(void)
  1349. {
  1350. int ret, warn = 0;
  1351. int (*target)(int, int, int, int, int, int);
  1352. struct trace_probe *tp;
  1353. target = kprobe_trace_selftest_target;
  1354. pr_info("Testing kprobe tracing: ");
  1355. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1356. "$stack $stack0 +0($stack)");
  1357. if (WARN_ON_ONCE(ret)) {
  1358. pr_warning("error on probing function entry.\n");
  1359. warn++;
  1360. } else {
  1361. /* Enable trace point */
  1362. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1363. if (WARN_ON_ONCE(tp == NULL)) {
  1364. pr_warning("error on getting new probe.\n");
  1365. warn++;
  1366. } else
  1367. probe_event_enable(&tp->call);
  1368. }
  1369. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1370. "$retval");
  1371. if (WARN_ON_ONCE(ret)) {
  1372. pr_warning("error on probing function return.\n");
  1373. warn++;
  1374. } else {
  1375. /* Enable trace point */
  1376. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1377. if (WARN_ON_ONCE(tp == NULL)) {
  1378. pr_warning("error on getting new probe.\n");
  1379. warn++;
  1380. } else
  1381. probe_event_enable(&tp->call);
  1382. }
  1383. if (warn)
  1384. goto end;
  1385. ret = target(1, 2, 3, 4, 5, 6);
  1386. ret = command_trace_probe("-:testprobe");
  1387. if (WARN_ON_ONCE(ret)) {
  1388. pr_warning("error on deleting a probe.\n");
  1389. warn++;
  1390. }
  1391. ret = command_trace_probe("-:testprobe2");
  1392. if (WARN_ON_ONCE(ret)) {
  1393. pr_warning("error on deleting a probe.\n");
  1394. warn++;
  1395. }
  1396. end:
  1397. cleanup_all_probes();
  1398. if (warn)
  1399. pr_cont("NG: Some tests are failed. Please check them.\n");
  1400. else
  1401. pr_cont("OK\n");
  1402. return 0;
  1403. }
  1404. late_initcall(kprobe_trace_self_tests_init);
  1405. #endif