trace_kprobe.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. u8 *data;
  1164. int size, __size, i;
  1165. unsigned long irq_flags;
  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, &rctx, &irq_flags);
  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. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags, regs, call->perf_data);
  1181. }
  1182. /* Kretprobe profile handler */
  1183. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1184. struct pt_regs *regs)
  1185. {
  1186. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1187. struct ftrace_event_call *call = &tp->call;
  1188. struct kretprobe_trace_entry_head *entry;
  1189. u8 *data;
  1190. int size, __size, i;
  1191. unsigned long irq_flags;
  1192. int rctx;
  1193. __size = sizeof(*entry) + tp->size;
  1194. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1195. size -= sizeof(u32);
  1196. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1197. "profile buffer not large enough"))
  1198. return;
  1199. entry = perf_trace_buf_prepare(size, call->id, &rctx, &irq_flags);
  1200. if (!entry)
  1201. return;
  1202. entry->func = (unsigned long)tp->rp.kp.addr;
  1203. entry->ret_ip = (unsigned long)ri->ret_addr;
  1204. data = (u8 *)&entry[1];
  1205. for (i = 0; i < tp->nr_args; i++)
  1206. call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
  1207. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1,
  1208. irq_flags, regs, call->perf_data);
  1209. }
  1210. static int probe_perf_enable(struct ftrace_event_call *call)
  1211. {
  1212. struct trace_probe *tp = (struct trace_probe *)call->data;
  1213. tp->flags |= TP_FLAG_PROFILE;
  1214. if (probe_is_return(tp))
  1215. return enable_kretprobe(&tp->rp);
  1216. else
  1217. return enable_kprobe(&tp->rp.kp);
  1218. }
  1219. static void probe_perf_disable(struct ftrace_event_call *call)
  1220. {
  1221. struct trace_probe *tp = (struct trace_probe *)call->data;
  1222. tp->flags &= ~TP_FLAG_PROFILE;
  1223. if (!(tp->flags & TP_FLAG_TRACE)) {
  1224. if (probe_is_return(tp))
  1225. disable_kretprobe(&tp->rp);
  1226. else
  1227. disable_kprobe(&tp->rp.kp);
  1228. }
  1229. }
  1230. #endif /* CONFIG_PERF_EVENTS */
  1231. static __kprobes
  1232. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1233. {
  1234. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1235. if (tp->flags & TP_FLAG_TRACE)
  1236. kprobe_trace_func(kp, regs);
  1237. #ifdef CONFIG_PERF_EVENTS
  1238. if (tp->flags & TP_FLAG_PROFILE)
  1239. kprobe_perf_func(kp, regs);
  1240. #endif
  1241. return 0; /* We don't tweek kernel, so just return 0 */
  1242. }
  1243. static __kprobes
  1244. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1245. {
  1246. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1247. if (tp->flags & TP_FLAG_TRACE)
  1248. kretprobe_trace_func(ri, regs);
  1249. #ifdef CONFIG_PERF_EVENTS
  1250. if (tp->flags & TP_FLAG_PROFILE)
  1251. kretprobe_perf_func(ri, regs);
  1252. #endif
  1253. return 0; /* We don't tweek kernel, so just return 0 */
  1254. }
  1255. static int register_probe_event(struct trace_probe *tp)
  1256. {
  1257. struct ftrace_event_call *call = &tp->call;
  1258. int ret;
  1259. /* Initialize ftrace_event_call */
  1260. if (probe_is_return(tp)) {
  1261. tp->event.trace = print_kretprobe_event;
  1262. call->raw_init = probe_event_raw_init;
  1263. call->define_fields = kretprobe_event_define_fields;
  1264. } else {
  1265. tp->event.trace = print_kprobe_event;
  1266. call->raw_init = probe_event_raw_init;
  1267. call->define_fields = kprobe_event_define_fields;
  1268. }
  1269. if (set_print_fmt(tp) < 0)
  1270. return -ENOMEM;
  1271. call->event = &tp->event;
  1272. call->id = register_ftrace_event(&tp->event);
  1273. if (!call->id) {
  1274. kfree(call->print_fmt);
  1275. return -ENODEV;
  1276. }
  1277. call->enabled = 0;
  1278. call->regfunc = probe_event_enable;
  1279. call->unregfunc = probe_event_disable;
  1280. #ifdef CONFIG_PERF_EVENTS
  1281. call->perf_event_enable = probe_perf_enable;
  1282. call->perf_event_disable = probe_perf_disable;
  1283. #endif
  1284. call->data = tp;
  1285. ret = trace_add_event_call(call);
  1286. if (ret) {
  1287. pr_info("Failed to register kprobe event: %s\n", call->name);
  1288. kfree(call->print_fmt);
  1289. unregister_ftrace_event(&tp->event);
  1290. }
  1291. return ret;
  1292. }
  1293. static void unregister_probe_event(struct trace_probe *tp)
  1294. {
  1295. /* tp->event is unregistered in trace_remove_event_call() */
  1296. trace_remove_event_call(&tp->call);
  1297. kfree(tp->call.print_fmt);
  1298. }
  1299. /* Make a debugfs interface for controling probe points */
  1300. static __init int init_kprobe_trace(void)
  1301. {
  1302. struct dentry *d_tracer;
  1303. struct dentry *entry;
  1304. d_tracer = tracing_init_dentry();
  1305. if (!d_tracer)
  1306. return 0;
  1307. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1308. NULL, &kprobe_events_ops);
  1309. /* Event list interface */
  1310. if (!entry)
  1311. pr_warning("Could not create debugfs "
  1312. "'kprobe_events' entry\n");
  1313. /* Profile interface */
  1314. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1315. NULL, &kprobe_profile_ops);
  1316. if (!entry)
  1317. pr_warning("Could not create debugfs "
  1318. "'kprobe_profile' entry\n");
  1319. return 0;
  1320. }
  1321. fs_initcall(init_kprobe_trace);
  1322. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1323. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1324. int a4, int a5, int a6)
  1325. {
  1326. return a1 + a2 + a3 + a4 + a5 + a6;
  1327. }
  1328. static __init int kprobe_trace_self_tests_init(void)
  1329. {
  1330. int ret, warn = 0;
  1331. int (*target)(int, int, int, int, int, int);
  1332. struct trace_probe *tp;
  1333. target = kprobe_trace_selftest_target;
  1334. pr_info("Testing kprobe tracing: ");
  1335. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1336. "$stack $stack0 +0($stack)");
  1337. if (WARN_ON_ONCE(ret)) {
  1338. pr_warning("error on probing function entry.\n");
  1339. warn++;
  1340. } else {
  1341. /* Enable trace point */
  1342. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1343. if (WARN_ON_ONCE(tp == NULL)) {
  1344. pr_warning("error on getting new probe.\n");
  1345. warn++;
  1346. } else
  1347. probe_event_enable(&tp->call);
  1348. }
  1349. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1350. "$retval");
  1351. if (WARN_ON_ONCE(ret)) {
  1352. pr_warning("error on probing function return.\n");
  1353. warn++;
  1354. } else {
  1355. /* Enable trace point */
  1356. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1357. if (WARN_ON_ONCE(tp == NULL)) {
  1358. pr_warning("error on getting new probe.\n");
  1359. warn++;
  1360. } else
  1361. probe_event_enable(&tp->call);
  1362. }
  1363. if (warn)
  1364. goto end;
  1365. ret = target(1, 2, 3, 4, 5, 6);
  1366. ret = command_trace_probe("-:testprobe");
  1367. if (WARN_ON_ONCE(ret)) {
  1368. pr_warning("error on deleting a probe.\n");
  1369. warn++;
  1370. }
  1371. ret = command_trace_probe("-:testprobe2");
  1372. if (WARN_ON_ONCE(ret)) {
  1373. pr_warning("error on deleting a probe.\n");
  1374. warn++;
  1375. }
  1376. end:
  1377. cleanup_all_probes();
  1378. if (warn)
  1379. pr_cont("NG: Some tests are failed. Please check them.\n");
  1380. else
  1381. pr_cont("OK\n");
  1382. return 0;
  1383. }
  1384. late_initcall(kprobe_trace_self_tests_init);
  1385. #endif