trace_kprobe.c 46 KB

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