trace_kprobe.c 46 KB

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