trace_kprobe.c 48 KB

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