trace_kprobe.c 51 KB

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