trace_kprobe.c 53 KB

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