trace_kprobe.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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. case '-':
  678. tmp = strchr(arg, '(');
  679. if (!tmp)
  680. break;
  681. *tmp = '\0';
  682. ret = strict_strtol(arg + 1, 0, &offset);
  683. if (ret)
  684. break;
  685. if (arg[0] == '-')
  686. offset = -offset;
  687. arg = tmp + 1;
  688. tmp = strrchr(arg, ')');
  689. if (tmp) {
  690. struct deref_fetch_param *dprm;
  691. const struct fetch_type *t2 = find_fetch_type(NULL);
  692. *tmp = '\0';
  693. dprm = kzalloc(sizeof(struct deref_fetch_param),
  694. GFP_KERNEL);
  695. if (!dprm)
  696. return -ENOMEM;
  697. dprm->offset = offset;
  698. ret = __parse_probe_arg(arg, t2, &dprm->orig,
  699. is_return);
  700. if (ret)
  701. kfree(dprm);
  702. else {
  703. f->fn = t->fetch[FETCH_MTD_deref];
  704. f->data = (void *)dprm;
  705. }
  706. }
  707. break;
  708. }
  709. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  710. pr_info("%s type has no corresponding fetch method.\n",
  711. t->name);
  712. ret = -EINVAL;
  713. }
  714. return ret;
  715. }
  716. /* String length checking wrapper */
  717. static int parse_probe_arg(char *arg, struct trace_probe *tp,
  718. struct probe_arg *parg, int is_return)
  719. {
  720. const char *t;
  721. int ret;
  722. if (strlen(arg) > MAX_ARGSTR_LEN) {
  723. pr_info("Argument is too long.: %s\n", arg);
  724. return -ENOSPC;
  725. }
  726. parg->comm = kstrdup(arg, GFP_KERNEL);
  727. if (!parg->comm) {
  728. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  729. return -ENOMEM;
  730. }
  731. t = strchr(parg->comm, ':');
  732. if (t) {
  733. arg[t - parg->comm] = '\0';
  734. t++;
  735. }
  736. parg->type = find_fetch_type(t);
  737. if (!parg->type) {
  738. pr_info("Unsupported type: %s\n", t);
  739. return -EINVAL;
  740. }
  741. parg->offset = tp->size;
  742. tp->size += parg->type->size;
  743. ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
  744. if (ret >= 0) {
  745. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  746. parg->fetch.fn);
  747. parg->fetch_size.data = parg->fetch.data;
  748. }
  749. return ret;
  750. }
  751. /* Return 1 if name is reserved or already used by another argument */
  752. static int conflict_field_name(const char *name,
  753. struct probe_arg *args, int narg)
  754. {
  755. int i;
  756. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  757. if (strcmp(reserved_field_names[i], name) == 0)
  758. return 1;
  759. for (i = 0; i < narg; i++)
  760. if (strcmp(args[i].name, name) == 0)
  761. return 1;
  762. return 0;
  763. }
  764. static int create_trace_probe(int argc, char **argv)
  765. {
  766. /*
  767. * Argument syntax:
  768. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  769. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  770. * Fetch args:
  771. * $retval : fetch return value
  772. * $stack : fetch stack address
  773. * $stackN : fetch Nth of stack (N:0-)
  774. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  775. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  776. * %REG : fetch register REG
  777. * Dereferencing memory fetch:
  778. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  779. * Alias name of args:
  780. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  781. * Type of args:
  782. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  783. */
  784. struct trace_probe *tp;
  785. int i, ret = 0;
  786. int is_return = 0, is_delete = 0;
  787. char *symbol = NULL, *event = NULL, *group = NULL;
  788. char *arg;
  789. unsigned long offset = 0;
  790. void *addr = NULL;
  791. char buf[MAX_EVENT_NAME_LEN];
  792. /* argc must be >= 1 */
  793. if (argv[0][0] == 'p')
  794. is_return = 0;
  795. else if (argv[0][0] == 'r')
  796. is_return = 1;
  797. else if (argv[0][0] == '-')
  798. is_delete = 1;
  799. else {
  800. pr_info("Probe definition must be started with 'p', 'r' or"
  801. " '-'.\n");
  802. return -EINVAL;
  803. }
  804. if (argv[0][1] == ':') {
  805. event = &argv[0][2];
  806. if (strchr(event, '/')) {
  807. group = event;
  808. event = strchr(group, '/') + 1;
  809. event[-1] = '\0';
  810. if (strlen(group) == 0) {
  811. pr_info("Group name is not specified\n");
  812. return -EINVAL;
  813. }
  814. }
  815. if (strlen(event) == 0) {
  816. pr_info("Event name is not specified\n");
  817. return -EINVAL;
  818. }
  819. }
  820. if (!group)
  821. group = KPROBE_EVENT_SYSTEM;
  822. if (is_delete) {
  823. if (!event) {
  824. pr_info("Delete command needs an event name.\n");
  825. return -EINVAL;
  826. }
  827. mutex_lock(&probe_lock);
  828. tp = find_probe_event(event, group);
  829. if (!tp) {
  830. mutex_unlock(&probe_lock);
  831. pr_info("Event %s/%s doesn't exist.\n", group, event);
  832. return -ENOENT;
  833. }
  834. /* delete an event */
  835. unregister_trace_probe(tp);
  836. free_trace_probe(tp);
  837. mutex_unlock(&probe_lock);
  838. return 0;
  839. }
  840. if (argc < 2) {
  841. pr_info("Probe point is not specified.\n");
  842. return -EINVAL;
  843. }
  844. if (isdigit(argv[1][0])) {
  845. if (is_return) {
  846. pr_info("Return probe point must be a symbol.\n");
  847. return -EINVAL;
  848. }
  849. /* an address specified */
  850. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  851. if (ret) {
  852. pr_info("Failed to parse address.\n");
  853. return ret;
  854. }
  855. } else {
  856. /* a symbol specified */
  857. symbol = argv[1];
  858. /* TODO: support .init module functions */
  859. ret = split_symbol_offset(symbol, &offset);
  860. if (ret) {
  861. pr_info("Failed to parse symbol.\n");
  862. return ret;
  863. }
  864. if (offset && is_return) {
  865. pr_info("Return probe must be used without offset.\n");
  866. return -EINVAL;
  867. }
  868. }
  869. argc -= 2; argv += 2;
  870. /* setup a probe */
  871. if (!event) {
  872. /* Make a new event name */
  873. if (symbol)
  874. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  875. is_return ? 'r' : 'p', symbol, offset);
  876. else
  877. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  878. is_return ? 'r' : 'p', addr);
  879. event = buf;
  880. }
  881. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  882. is_return);
  883. if (IS_ERR(tp)) {
  884. pr_info("Failed to allocate trace_probe.(%d)\n",
  885. (int)PTR_ERR(tp));
  886. return PTR_ERR(tp);
  887. }
  888. /* parse arguments */
  889. ret = 0;
  890. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  891. /* Increment count for freeing args in error case */
  892. tp->nr_args++;
  893. /* Parse argument name */
  894. arg = strchr(argv[i], '=');
  895. if (arg) {
  896. *arg++ = '\0';
  897. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  898. } else {
  899. arg = argv[i];
  900. /* If argument name is omitted, set "argN" */
  901. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  902. tp->args[i].name = kstrdup(buf, GFP_KERNEL);
  903. }
  904. if (!tp->args[i].name) {
  905. pr_info("Failed to allocate argument[%d] name.\n", i);
  906. ret = -ENOMEM;
  907. goto error;
  908. }
  909. if (!is_good_name(tp->args[i].name)) {
  910. pr_info("Invalid argument[%d] name: %s\n",
  911. i, tp->args[i].name);
  912. ret = -EINVAL;
  913. goto error;
  914. }
  915. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  916. pr_info("Argument[%d] name '%s' conflicts with "
  917. "another field.\n", i, argv[i]);
  918. ret = -EINVAL;
  919. goto error;
  920. }
  921. /* Parse fetch argument */
  922. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  923. if (ret) {
  924. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  925. goto error;
  926. }
  927. }
  928. ret = register_trace_probe(tp);
  929. if (ret)
  930. goto error;
  931. return 0;
  932. error:
  933. free_trace_probe(tp);
  934. return ret;
  935. }
  936. static void cleanup_all_probes(void)
  937. {
  938. struct trace_probe *tp;
  939. mutex_lock(&probe_lock);
  940. /* TODO: Use batch unregistration */
  941. while (!list_empty(&probe_list)) {
  942. tp = list_entry(probe_list.next, struct trace_probe, list);
  943. unregister_trace_probe(tp);
  944. free_trace_probe(tp);
  945. }
  946. mutex_unlock(&probe_lock);
  947. }
  948. /* Probes listing interfaces */
  949. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  950. {
  951. mutex_lock(&probe_lock);
  952. return seq_list_start(&probe_list, *pos);
  953. }
  954. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  955. {
  956. return seq_list_next(v, &probe_list, pos);
  957. }
  958. static void probes_seq_stop(struct seq_file *m, void *v)
  959. {
  960. mutex_unlock(&probe_lock);
  961. }
  962. static int probes_seq_show(struct seq_file *m, void *v)
  963. {
  964. struct trace_probe *tp = v;
  965. int i;
  966. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  967. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  968. if (!tp->symbol)
  969. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  970. else if (tp->rp.kp.offset)
  971. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  972. else
  973. seq_printf(m, " %s", probe_symbol(tp));
  974. for (i = 0; i < tp->nr_args; i++)
  975. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  976. seq_printf(m, "\n");
  977. return 0;
  978. }
  979. static const struct seq_operations probes_seq_op = {
  980. .start = probes_seq_start,
  981. .next = probes_seq_next,
  982. .stop = probes_seq_stop,
  983. .show = probes_seq_show
  984. };
  985. static int probes_open(struct inode *inode, struct file *file)
  986. {
  987. if ((file->f_mode & FMODE_WRITE) &&
  988. (file->f_flags & O_TRUNC))
  989. cleanup_all_probes();
  990. return seq_open(file, &probes_seq_op);
  991. }
  992. static int command_trace_probe(const char *buf)
  993. {
  994. char **argv;
  995. int argc = 0, ret = 0;
  996. argv = argv_split(GFP_KERNEL, buf, &argc);
  997. if (!argv)
  998. return -ENOMEM;
  999. if (argc)
  1000. ret = create_trace_probe(argc, argv);
  1001. argv_free(argv);
  1002. return ret;
  1003. }
  1004. #define WRITE_BUFSIZE 128
  1005. static ssize_t probes_write(struct file *file, const char __user *buffer,
  1006. size_t count, loff_t *ppos)
  1007. {
  1008. char *kbuf, *tmp;
  1009. int ret;
  1010. size_t done;
  1011. size_t size;
  1012. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  1013. if (!kbuf)
  1014. return -ENOMEM;
  1015. ret = done = 0;
  1016. while (done < count) {
  1017. size = count - done;
  1018. if (size >= WRITE_BUFSIZE)
  1019. size = WRITE_BUFSIZE - 1;
  1020. if (copy_from_user(kbuf, buffer + done, size)) {
  1021. ret = -EFAULT;
  1022. goto out;
  1023. }
  1024. kbuf[size] = '\0';
  1025. tmp = strchr(kbuf, '\n');
  1026. if (tmp) {
  1027. *tmp = '\0';
  1028. size = tmp - kbuf + 1;
  1029. } else if (done + size < count) {
  1030. pr_warning("Line length is too long: "
  1031. "Should be less than %d.", WRITE_BUFSIZE);
  1032. ret = -EINVAL;
  1033. goto out;
  1034. }
  1035. done += size;
  1036. /* Remove comments */
  1037. tmp = strchr(kbuf, '#');
  1038. if (tmp)
  1039. *tmp = '\0';
  1040. ret = command_trace_probe(kbuf);
  1041. if (ret)
  1042. goto out;
  1043. }
  1044. ret = done;
  1045. out:
  1046. kfree(kbuf);
  1047. return ret;
  1048. }
  1049. static const struct file_operations kprobe_events_ops = {
  1050. .owner = THIS_MODULE,
  1051. .open = probes_open,
  1052. .read = seq_read,
  1053. .llseek = seq_lseek,
  1054. .release = seq_release,
  1055. .write = probes_write,
  1056. };
  1057. /* Probes profiling interfaces */
  1058. static int probes_profile_seq_show(struct seq_file *m, void *v)
  1059. {
  1060. struct trace_probe *tp = v;
  1061. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  1062. tp->rp.kp.nmissed);
  1063. return 0;
  1064. }
  1065. static const struct seq_operations profile_seq_op = {
  1066. .start = probes_seq_start,
  1067. .next = probes_seq_next,
  1068. .stop = probes_seq_stop,
  1069. .show = probes_profile_seq_show
  1070. };
  1071. static int profile_open(struct inode *inode, struct file *file)
  1072. {
  1073. return seq_open(file, &profile_seq_op);
  1074. }
  1075. static const struct file_operations kprobe_profile_ops = {
  1076. .owner = THIS_MODULE,
  1077. .open = profile_open,
  1078. .read = seq_read,
  1079. .llseek = seq_lseek,
  1080. .release = seq_release,
  1081. };
  1082. /* Sum up total data length for dynamic arraies (strings) */
  1083. static __kprobes int __get_data_size(struct trace_probe *tp,
  1084. struct pt_regs *regs)
  1085. {
  1086. int i, ret = 0;
  1087. u32 len;
  1088. for (i = 0; i < tp->nr_args; i++)
  1089. if (unlikely(tp->args[i].fetch_size.fn)) {
  1090. call_fetch(&tp->args[i].fetch_size, regs, &len);
  1091. ret += len;
  1092. }
  1093. return ret;
  1094. }
  1095. /* Store the value of each argument */
  1096. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  1097. struct pt_regs *regs,
  1098. u8 *data, int maxlen)
  1099. {
  1100. int i;
  1101. u32 end = tp->size;
  1102. u32 *dl; /* Data (relative) location */
  1103. for (i = 0; i < tp->nr_args; i++) {
  1104. if (unlikely(tp->args[i].fetch_size.fn)) {
  1105. /*
  1106. * First, we set the relative location and
  1107. * maximum data length to *dl
  1108. */
  1109. dl = (u32 *)(data + tp->args[i].offset);
  1110. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  1111. /* Then try to fetch string or dynamic array data */
  1112. call_fetch(&tp->args[i].fetch, regs, dl);
  1113. /* Reduce maximum length */
  1114. end += get_rloc_len(*dl);
  1115. maxlen -= get_rloc_len(*dl);
  1116. /* Trick here, convert data_rloc to data_loc */
  1117. *dl = convert_rloc_to_loc(*dl,
  1118. ent_size + tp->args[i].offset);
  1119. } else
  1120. /* Just fetching data normally */
  1121. call_fetch(&tp->args[i].fetch, regs,
  1122. data + tp->args[i].offset);
  1123. }
  1124. }
  1125. /* Kprobe handler */
  1126. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  1127. {
  1128. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1129. struct kprobe_trace_entry_head *entry;
  1130. struct ring_buffer_event *event;
  1131. struct ring_buffer *buffer;
  1132. int size, dsize, pc;
  1133. unsigned long irq_flags;
  1134. struct ftrace_event_call *call = &tp->call;
  1135. tp->nhit++;
  1136. local_save_flags(irq_flags);
  1137. pc = preempt_count();
  1138. dsize = __get_data_size(tp, regs);
  1139. size = sizeof(*entry) + tp->size + dsize;
  1140. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1141. size, irq_flags, pc);
  1142. if (!event)
  1143. return;
  1144. entry = ring_buffer_event_data(event);
  1145. entry->ip = (unsigned long)kp->addr;
  1146. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1147. if (!filter_current_check_discard(buffer, call, entry, event))
  1148. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  1149. }
  1150. /* Kretprobe handler */
  1151. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  1152. struct pt_regs *regs)
  1153. {
  1154. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1155. struct kretprobe_trace_entry_head *entry;
  1156. struct ring_buffer_event *event;
  1157. struct ring_buffer *buffer;
  1158. int size, pc, dsize;
  1159. unsigned long irq_flags;
  1160. struct ftrace_event_call *call = &tp->call;
  1161. local_save_flags(irq_flags);
  1162. pc = preempt_count();
  1163. dsize = __get_data_size(tp, regs);
  1164. size = sizeof(*entry) + tp->size + dsize;
  1165. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1166. size, irq_flags, pc);
  1167. if (!event)
  1168. return;
  1169. entry = ring_buffer_event_data(event);
  1170. entry->func = (unsigned long)tp->rp.kp.addr;
  1171. entry->ret_ip = (unsigned long)ri->ret_addr;
  1172. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1173. if (!filter_current_check_discard(buffer, call, entry, event))
  1174. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  1175. }
  1176. /* Event entry printers */
  1177. enum print_line_t
  1178. print_kprobe_event(struct trace_iterator *iter, int flags,
  1179. struct trace_event *event)
  1180. {
  1181. struct kprobe_trace_entry_head *field;
  1182. struct trace_seq *s = &iter->seq;
  1183. struct trace_probe *tp;
  1184. u8 *data;
  1185. int i;
  1186. field = (struct kprobe_trace_entry_head *)iter->ent;
  1187. tp = container_of(event, struct trace_probe, call.event);
  1188. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1189. goto partial;
  1190. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  1191. goto partial;
  1192. if (!trace_seq_puts(s, ")"))
  1193. goto partial;
  1194. data = (u8 *)&field[1];
  1195. for (i = 0; i < tp->nr_args; i++)
  1196. if (!tp->args[i].type->print(s, tp->args[i].name,
  1197. data + tp->args[i].offset, field))
  1198. goto partial;
  1199. if (!trace_seq_puts(s, "\n"))
  1200. goto partial;
  1201. return TRACE_TYPE_HANDLED;
  1202. partial:
  1203. return TRACE_TYPE_PARTIAL_LINE;
  1204. }
  1205. enum print_line_t
  1206. print_kretprobe_event(struct trace_iterator *iter, int flags,
  1207. struct trace_event *event)
  1208. {
  1209. struct kretprobe_trace_entry_head *field;
  1210. struct trace_seq *s = &iter->seq;
  1211. struct trace_probe *tp;
  1212. u8 *data;
  1213. int i;
  1214. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1215. tp = container_of(event, struct trace_probe, call.event);
  1216. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1217. goto partial;
  1218. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1219. goto partial;
  1220. if (!trace_seq_puts(s, " <- "))
  1221. goto partial;
  1222. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1223. goto partial;
  1224. if (!trace_seq_puts(s, ")"))
  1225. goto partial;
  1226. data = (u8 *)&field[1];
  1227. for (i = 0; i < tp->nr_args; i++)
  1228. if (!tp->args[i].type->print(s, tp->args[i].name,
  1229. data + tp->args[i].offset, field))
  1230. goto partial;
  1231. if (!trace_seq_puts(s, "\n"))
  1232. goto partial;
  1233. return TRACE_TYPE_HANDLED;
  1234. partial:
  1235. return TRACE_TYPE_PARTIAL_LINE;
  1236. }
  1237. static int probe_event_enable(struct ftrace_event_call *call)
  1238. {
  1239. struct trace_probe *tp = (struct trace_probe *)call->data;
  1240. tp->flags |= TP_FLAG_TRACE;
  1241. if (probe_is_return(tp))
  1242. return enable_kretprobe(&tp->rp);
  1243. else
  1244. return enable_kprobe(&tp->rp.kp);
  1245. }
  1246. static void probe_event_disable(struct ftrace_event_call *call)
  1247. {
  1248. struct trace_probe *tp = (struct trace_probe *)call->data;
  1249. tp->flags &= ~TP_FLAG_TRACE;
  1250. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  1251. if (probe_is_return(tp))
  1252. disable_kretprobe(&tp->rp);
  1253. else
  1254. disable_kprobe(&tp->rp.kp);
  1255. }
  1256. }
  1257. #undef DEFINE_FIELD
  1258. #define DEFINE_FIELD(type, item, name, is_signed) \
  1259. do { \
  1260. ret = trace_define_field(event_call, #type, name, \
  1261. offsetof(typeof(field), item), \
  1262. sizeof(field.item), is_signed, \
  1263. FILTER_OTHER); \
  1264. if (ret) \
  1265. return ret; \
  1266. } while (0)
  1267. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1268. {
  1269. int ret, i;
  1270. struct kprobe_trace_entry_head field;
  1271. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1272. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1273. /* Set argument names as fields */
  1274. for (i = 0; i < tp->nr_args; i++) {
  1275. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1276. tp->args[i].name,
  1277. sizeof(field) + tp->args[i].offset,
  1278. tp->args[i].type->size,
  1279. tp->args[i].type->is_signed,
  1280. FILTER_OTHER);
  1281. if (ret)
  1282. return ret;
  1283. }
  1284. return 0;
  1285. }
  1286. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1287. {
  1288. int ret, i;
  1289. struct kretprobe_trace_entry_head field;
  1290. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1291. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1292. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1293. /* Set argument names as fields */
  1294. for (i = 0; i < tp->nr_args; i++) {
  1295. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1296. tp->args[i].name,
  1297. sizeof(field) + tp->args[i].offset,
  1298. tp->args[i].type->size,
  1299. tp->args[i].type->is_signed,
  1300. FILTER_OTHER);
  1301. if (ret)
  1302. return ret;
  1303. }
  1304. return 0;
  1305. }
  1306. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1307. {
  1308. int i;
  1309. int pos = 0;
  1310. const char *fmt, *arg;
  1311. if (!probe_is_return(tp)) {
  1312. fmt = "(%lx)";
  1313. arg = "REC->" FIELD_STRING_IP;
  1314. } else {
  1315. fmt = "(%lx <- %lx)";
  1316. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1317. }
  1318. /* When len=0, we just calculate the needed length */
  1319. #define LEN_OR_ZERO (len ? len - pos : 0)
  1320. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1321. for (i = 0; i < tp->nr_args; i++) {
  1322. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1323. tp->args[i].name, tp->args[i].type->fmt);
  1324. }
  1325. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1326. for (i = 0; i < tp->nr_args; i++) {
  1327. if (strcmp(tp->args[i].type->name, "string") == 0)
  1328. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1329. ", __get_str(%s)",
  1330. tp->args[i].name);
  1331. else
  1332. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1333. tp->args[i].name);
  1334. }
  1335. #undef LEN_OR_ZERO
  1336. /* return the length of print_fmt */
  1337. return pos;
  1338. }
  1339. static int set_print_fmt(struct trace_probe *tp)
  1340. {
  1341. int len;
  1342. char *print_fmt;
  1343. /* First: called with 0 length to calculate the needed length */
  1344. len = __set_print_fmt(tp, NULL, 0);
  1345. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1346. if (!print_fmt)
  1347. return -ENOMEM;
  1348. /* Second: actually write the @print_fmt */
  1349. __set_print_fmt(tp, print_fmt, len + 1);
  1350. tp->call.print_fmt = print_fmt;
  1351. return 0;
  1352. }
  1353. #ifdef CONFIG_PERF_EVENTS
  1354. /* Kprobe profile handler */
  1355. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1356. struct pt_regs *regs)
  1357. {
  1358. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1359. struct ftrace_event_call *call = &tp->call;
  1360. struct kprobe_trace_entry_head *entry;
  1361. struct hlist_head *head;
  1362. int size, __size, dsize;
  1363. int rctx;
  1364. dsize = __get_data_size(tp, regs);
  1365. __size = sizeof(*entry) + tp->size + dsize;
  1366. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1367. size -= sizeof(u32);
  1368. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1369. "profile buffer not large enough"))
  1370. return;
  1371. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1372. if (!entry)
  1373. return;
  1374. entry->ip = (unsigned long)kp->addr;
  1375. memset(&entry[1], 0, dsize);
  1376. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1377. head = this_cpu_ptr(call->perf_events);
  1378. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1379. }
  1380. /* Kretprobe profile handler */
  1381. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1382. struct pt_regs *regs)
  1383. {
  1384. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1385. struct ftrace_event_call *call = &tp->call;
  1386. struct kretprobe_trace_entry_head *entry;
  1387. struct hlist_head *head;
  1388. int size, __size, dsize;
  1389. int rctx;
  1390. dsize = __get_data_size(tp, regs);
  1391. __size = sizeof(*entry) + tp->size + dsize;
  1392. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1393. size -= sizeof(u32);
  1394. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1395. "profile buffer not large enough"))
  1396. return;
  1397. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1398. if (!entry)
  1399. return;
  1400. entry->func = (unsigned long)tp->rp.kp.addr;
  1401. entry->ret_ip = (unsigned long)ri->ret_addr;
  1402. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1403. head = this_cpu_ptr(call->perf_events);
  1404. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1405. }
  1406. static int probe_perf_enable(struct ftrace_event_call *call)
  1407. {
  1408. struct trace_probe *tp = (struct trace_probe *)call->data;
  1409. tp->flags |= TP_FLAG_PROFILE;
  1410. if (probe_is_return(tp))
  1411. return enable_kretprobe(&tp->rp);
  1412. else
  1413. return enable_kprobe(&tp->rp.kp);
  1414. }
  1415. static void probe_perf_disable(struct ftrace_event_call *call)
  1416. {
  1417. struct trace_probe *tp = (struct trace_probe *)call->data;
  1418. tp->flags &= ~TP_FLAG_PROFILE;
  1419. if (!(tp->flags & TP_FLAG_TRACE)) {
  1420. if (probe_is_return(tp))
  1421. disable_kretprobe(&tp->rp);
  1422. else
  1423. disable_kprobe(&tp->rp.kp);
  1424. }
  1425. }
  1426. #endif /* CONFIG_PERF_EVENTS */
  1427. static __kprobes
  1428. int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
  1429. {
  1430. switch (type) {
  1431. case TRACE_REG_REGISTER:
  1432. return probe_event_enable(event);
  1433. case TRACE_REG_UNREGISTER:
  1434. probe_event_disable(event);
  1435. return 0;
  1436. #ifdef CONFIG_PERF_EVENTS
  1437. case TRACE_REG_PERF_REGISTER:
  1438. return probe_perf_enable(event);
  1439. case TRACE_REG_PERF_UNREGISTER:
  1440. probe_perf_disable(event);
  1441. return 0;
  1442. #endif
  1443. }
  1444. return 0;
  1445. }
  1446. static __kprobes
  1447. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1448. {
  1449. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1450. if (tp->flags & TP_FLAG_TRACE)
  1451. kprobe_trace_func(kp, regs);
  1452. #ifdef CONFIG_PERF_EVENTS
  1453. if (tp->flags & TP_FLAG_PROFILE)
  1454. kprobe_perf_func(kp, regs);
  1455. #endif
  1456. return 0; /* We don't tweek kernel, so just return 0 */
  1457. }
  1458. static __kprobes
  1459. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1460. {
  1461. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1462. if (tp->flags & TP_FLAG_TRACE)
  1463. kretprobe_trace_func(ri, regs);
  1464. #ifdef CONFIG_PERF_EVENTS
  1465. if (tp->flags & TP_FLAG_PROFILE)
  1466. kretprobe_perf_func(ri, regs);
  1467. #endif
  1468. return 0; /* We don't tweek kernel, so just return 0 */
  1469. }
  1470. static struct trace_event_functions kretprobe_funcs = {
  1471. .trace = print_kretprobe_event
  1472. };
  1473. static struct trace_event_functions kprobe_funcs = {
  1474. .trace = print_kprobe_event
  1475. };
  1476. static int register_probe_event(struct trace_probe *tp)
  1477. {
  1478. struct ftrace_event_call *call = &tp->call;
  1479. int ret;
  1480. /* Initialize ftrace_event_call */
  1481. INIT_LIST_HEAD(&call->class->fields);
  1482. if (probe_is_return(tp)) {
  1483. call->event.funcs = &kretprobe_funcs;
  1484. call->class->define_fields = kretprobe_event_define_fields;
  1485. } else {
  1486. call->event.funcs = &kprobe_funcs;
  1487. call->class->define_fields = kprobe_event_define_fields;
  1488. }
  1489. if (set_print_fmt(tp) < 0)
  1490. return -ENOMEM;
  1491. ret = register_ftrace_event(&call->event);
  1492. if (!ret) {
  1493. kfree(call->print_fmt);
  1494. return -ENODEV;
  1495. }
  1496. call->flags = 0;
  1497. call->class->reg = kprobe_register;
  1498. call->data = tp;
  1499. ret = trace_add_event_call(call);
  1500. if (ret) {
  1501. pr_info("Failed to register kprobe event: %s\n", call->name);
  1502. kfree(call->print_fmt);
  1503. unregister_ftrace_event(&call->event);
  1504. }
  1505. return ret;
  1506. }
  1507. static void unregister_probe_event(struct trace_probe *tp)
  1508. {
  1509. /* tp->event is unregistered in trace_remove_event_call() */
  1510. trace_remove_event_call(&tp->call);
  1511. kfree(tp->call.print_fmt);
  1512. }
  1513. /* Make a debugfs interface for controling probe points */
  1514. static __init int init_kprobe_trace(void)
  1515. {
  1516. struct dentry *d_tracer;
  1517. struct dentry *entry;
  1518. d_tracer = tracing_init_dentry();
  1519. if (!d_tracer)
  1520. return 0;
  1521. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1522. NULL, &kprobe_events_ops);
  1523. /* Event list interface */
  1524. if (!entry)
  1525. pr_warning("Could not create debugfs "
  1526. "'kprobe_events' entry\n");
  1527. /* Profile interface */
  1528. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1529. NULL, &kprobe_profile_ops);
  1530. if (!entry)
  1531. pr_warning("Could not create debugfs "
  1532. "'kprobe_profile' entry\n");
  1533. return 0;
  1534. }
  1535. fs_initcall(init_kprobe_trace);
  1536. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1537. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1538. int a4, int a5, int a6)
  1539. {
  1540. return a1 + a2 + a3 + a4 + a5 + a6;
  1541. }
  1542. static __init int kprobe_trace_self_tests_init(void)
  1543. {
  1544. int ret, warn = 0;
  1545. int (*target)(int, int, int, int, int, int);
  1546. struct trace_probe *tp;
  1547. target = kprobe_trace_selftest_target;
  1548. pr_info("Testing kprobe tracing: ");
  1549. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1550. "$stack $stack0 +0($stack)");
  1551. if (WARN_ON_ONCE(ret)) {
  1552. pr_warning("error on probing function entry.\n");
  1553. warn++;
  1554. } else {
  1555. /* Enable trace point */
  1556. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1557. if (WARN_ON_ONCE(tp == NULL)) {
  1558. pr_warning("error on getting new probe.\n");
  1559. warn++;
  1560. } else
  1561. probe_event_enable(&tp->call);
  1562. }
  1563. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1564. "$retval");
  1565. if (WARN_ON_ONCE(ret)) {
  1566. pr_warning("error on probing function return.\n");
  1567. warn++;
  1568. } else {
  1569. /* Enable trace point */
  1570. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1571. if (WARN_ON_ONCE(tp == NULL)) {
  1572. pr_warning("error on getting new probe.\n");
  1573. warn++;
  1574. } else
  1575. probe_event_enable(&tp->call);
  1576. }
  1577. if (warn)
  1578. goto end;
  1579. ret = target(1, 2, 3, 4, 5, 6);
  1580. ret = command_trace_probe("-:testprobe");
  1581. if (WARN_ON_ONCE(ret)) {
  1582. pr_warning("error on deleting a probe.\n");
  1583. warn++;
  1584. }
  1585. ret = command_trace_probe("-:testprobe2");
  1586. if (WARN_ON_ONCE(ret)) {
  1587. pr_warning("error on deleting a probe.\n");
  1588. warn++;
  1589. }
  1590. end:
  1591. cleanup_all_probes();
  1592. if (warn)
  1593. pr_cont("NG: Some tests are failed. Please check them.\n");
  1594. else
  1595. pr_cont("OK\n");
  1596. return 0;
  1597. }
  1598. late_initcall(kprobe_trace_self_tests_init);
  1599. #endif