ftrace.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  1. /*
  2. * Infrastructure for profiling code inserted by 'gcc -pg'.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally ported from the -rt patch by:
  8. * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code in the latency_tracer, that is:
  11. *
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 William Lee Irwin III
  14. */
  15. #include <linux/stop_machine.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/suspend.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/ctype.h>
  28. #include <linux/list.h>
  29. #include <linux/hash.h>
  30. #include <asm/ftrace.h>
  31. #include "trace.h"
  32. #define FTRACE_WARN_ON(cond) \
  33. do { \
  34. if (WARN_ON(cond)) \
  35. ftrace_kill(); \
  36. } while (0)
  37. #define FTRACE_WARN_ON_ONCE(cond) \
  38. do { \
  39. if (WARN_ON_ONCE(cond)) \
  40. ftrace_kill(); \
  41. } while (0)
  42. /* hash bits for specific function selection */
  43. #define FTRACE_HASH_BITS 7
  44. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  45. /* ftrace_enabled is a method to turn ftrace on or off */
  46. int ftrace_enabled __read_mostly;
  47. static int last_ftrace_enabled;
  48. /* Quick disabling of function tracer. */
  49. int function_trace_stop;
  50. /*
  51. * ftrace_disabled is set when an anomaly is discovered.
  52. * ftrace_disabled is much stronger than ftrace_enabled.
  53. */
  54. static int ftrace_disabled __read_mostly;
  55. static DEFINE_MUTEX(ftrace_lock);
  56. static struct ftrace_ops ftrace_list_end __read_mostly =
  57. {
  58. .func = ftrace_stub,
  59. };
  60. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  61. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  62. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  63. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  64. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  65. {
  66. struct ftrace_ops *op = ftrace_list;
  67. /* in case someone actually ports this to alpha! */
  68. read_barrier_depends();
  69. while (op != &ftrace_list_end) {
  70. /* silly alpha */
  71. read_barrier_depends();
  72. op->func(ip, parent_ip);
  73. op = op->next;
  74. };
  75. }
  76. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  77. {
  78. if (!test_tsk_trace_trace(current))
  79. return;
  80. ftrace_pid_function(ip, parent_ip);
  81. }
  82. static void set_ftrace_pid_function(ftrace_func_t func)
  83. {
  84. /* do not set ftrace_pid_function to itself! */
  85. if (func != ftrace_pid_func)
  86. ftrace_pid_function = func;
  87. }
  88. /**
  89. * clear_ftrace_function - reset the ftrace function
  90. *
  91. * This NULLs the ftrace function and in essence stops
  92. * tracing. There may be lag
  93. */
  94. void clear_ftrace_function(void)
  95. {
  96. ftrace_trace_function = ftrace_stub;
  97. __ftrace_trace_function = ftrace_stub;
  98. ftrace_pid_function = ftrace_stub;
  99. }
  100. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  101. /*
  102. * For those archs that do not test ftrace_trace_stop in their
  103. * mcount call site, we need to do it from C.
  104. */
  105. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  106. {
  107. if (function_trace_stop)
  108. return;
  109. __ftrace_trace_function(ip, parent_ip);
  110. }
  111. #endif
  112. static int __register_ftrace_function(struct ftrace_ops *ops)
  113. {
  114. ops->next = ftrace_list;
  115. /*
  116. * We are entering ops into the ftrace_list but another
  117. * CPU might be walking that list. We need to make sure
  118. * the ops->next pointer is valid before another CPU sees
  119. * the ops pointer included into the ftrace_list.
  120. */
  121. smp_wmb();
  122. ftrace_list = ops;
  123. if (ftrace_enabled) {
  124. ftrace_func_t func;
  125. if (ops->next == &ftrace_list_end)
  126. func = ops->func;
  127. else
  128. func = ftrace_list_func;
  129. if (ftrace_pid_trace) {
  130. set_ftrace_pid_function(func);
  131. func = ftrace_pid_func;
  132. }
  133. /*
  134. * For one func, simply call it directly.
  135. * For more than one func, call the chain.
  136. */
  137. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  138. ftrace_trace_function = func;
  139. #else
  140. __ftrace_trace_function = func;
  141. ftrace_trace_function = ftrace_test_stop_func;
  142. #endif
  143. }
  144. return 0;
  145. }
  146. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  147. {
  148. struct ftrace_ops **p;
  149. /*
  150. * If we are removing the last function, then simply point
  151. * to the ftrace_stub.
  152. */
  153. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  154. ftrace_trace_function = ftrace_stub;
  155. ftrace_list = &ftrace_list_end;
  156. return 0;
  157. }
  158. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  159. if (*p == ops)
  160. break;
  161. if (*p != ops)
  162. return -1;
  163. *p = (*p)->next;
  164. if (ftrace_enabled) {
  165. /* If we only have one func left, then call that directly */
  166. if (ftrace_list->next == &ftrace_list_end) {
  167. ftrace_func_t func = ftrace_list->func;
  168. if (ftrace_pid_trace) {
  169. set_ftrace_pid_function(func);
  170. func = ftrace_pid_func;
  171. }
  172. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  173. ftrace_trace_function = func;
  174. #else
  175. __ftrace_trace_function = func;
  176. #endif
  177. }
  178. }
  179. return 0;
  180. }
  181. static void ftrace_update_pid_func(void)
  182. {
  183. ftrace_func_t func;
  184. if (ftrace_trace_function == ftrace_stub)
  185. return;
  186. func = ftrace_trace_function;
  187. if (ftrace_pid_trace) {
  188. set_ftrace_pid_function(func);
  189. func = ftrace_pid_func;
  190. } else {
  191. if (func == ftrace_pid_func)
  192. func = ftrace_pid_function;
  193. }
  194. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  195. ftrace_trace_function = func;
  196. #else
  197. __ftrace_trace_function = func;
  198. #endif
  199. }
  200. /* set when tracing only a pid */
  201. struct pid *ftrace_pid_trace;
  202. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  203. #ifdef CONFIG_DYNAMIC_FTRACE
  204. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  205. # error Dynamic ftrace depends on MCOUNT_RECORD
  206. #endif
  207. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  208. struct ftrace_func_probe {
  209. struct hlist_node node;
  210. struct ftrace_probe_ops *ops;
  211. unsigned long flags;
  212. unsigned long ip;
  213. void *data;
  214. struct rcu_head rcu;
  215. };
  216. enum {
  217. FTRACE_ENABLE_CALLS = (1 << 0),
  218. FTRACE_DISABLE_CALLS = (1 << 1),
  219. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  220. FTRACE_ENABLE_MCOUNT = (1 << 3),
  221. FTRACE_DISABLE_MCOUNT = (1 << 4),
  222. FTRACE_START_FUNC_RET = (1 << 5),
  223. FTRACE_STOP_FUNC_RET = (1 << 6),
  224. };
  225. static int ftrace_filtered;
  226. static LIST_HEAD(ftrace_new_addrs);
  227. static DEFINE_MUTEX(ftrace_regex_lock);
  228. struct ftrace_page {
  229. struct ftrace_page *next;
  230. int index;
  231. struct dyn_ftrace records[];
  232. };
  233. #define ENTRIES_PER_PAGE \
  234. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  235. /* estimate from running different kernels */
  236. #define NR_TO_INIT 10000
  237. static struct ftrace_page *ftrace_pages_start;
  238. static struct ftrace_page *ftrace_pages;
  239. static struct dyn_ftrace *ftrace_free_records;
  240. /*
  241. * This is a double for. Do not use 'break' to break out of the loop,
  242. * you must use a goto.
  243. */
  244. #define do_for_each_ftrace_rec(pg, rec) \
  245. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  246. int _____i; \
  247. for (_____i = 0; _____i < pg->index; _____i++) { \
  248. rec = &pg->records[_____i];
  249. #define while_for_each_ftrace_rec() \
  250. } \
  251. }
  252. #ifdef CONFIG_KPROBES
  253. static int frozen_record_count;
  254. static inline void freeze_record(struct dyn_ftrace *rec)
  255. {
  256. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  257. rec->flags |= FTRACE_FL_FROZEN;
  258. frozen_record_count++;
  259. }
  260. }
  261. static inline void unfreeze_record(struct dyn_ftrace *rec)
  262. {
  263. if (rec->flags & FTRACE_FL_FROZEN) {
  264. rec->flags &= ~FTRACE_FL_FROZEN;
  265. frozen_record_count--;
  266. }
  267. }
  268. static inline int record_frozen(struct dyn_ftrace *rec)
  269. {
  270. return rec->flags & FTRACE_FL_FROZEN;
  271. }
  272. #else
  273. # define freeze_record(rec) ({ 0; })
  274. # define unfreeze_record(rec) ({ 0; })
  275. # define record_frozen(rec) ({ 0; })
  276. #endif /* CONFIG_KPROBES */
  277. static void ftrace_free_rec(struct dyn_ftrace *rec)
  278. {
  279. rec->ip = (unsigned long)ftrace_free_records;
  280. ftrace_free_records = rec;
  281. rec->flags |= FTRACE_FL_FREE;
  282. }
  283. void ftrace_release(void *start, unsigned long size)
  284. {
  285. struct dyn_ftrace *rec;
  286. struct ftrace_page *pg;
  287. unsigned long s = (unsigned long)start;
  288. unsigned long e = s + size;
  289. if (ftrace_disabled || !start)
  290. return;
  291. mutex_lock(&ftrace_lock);
  292. do_for_each_ftrace_rec(pg, rec) {
  293. if ((rec->ip >= s) && (rec->ip < e) &&
  294. !(rec->flags & FTRACE_FL_FREE))
  295. ftrace_free_rec(rec);
  296. } while_for_each_ftrace_rec();
  297. mutex_unlock(&ftrace_lock);
  298. }
  299. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  300. {
  301. struct dyn_ftrace *rec;
  302. /* First check for freed records */
  303. if (ftrace_free_records) {
  304. rec = ftrace_free_records;
  305. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  306. FTRACE_WARN_ON_ONCE(1);
  307. ftrace_free_records = NULL;
  308. return NULL;
  309. }
  310. ftrace_free_records = (void *)rec->ip;
  311. memset(rec, 0, sizeof(*rec));
  312. return rec;
  313. }
  314. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  315. if (!ftrace_pages->next) {
  316. /* allocate another page */
  317. ftrace_pages->next =
  318. (void *)get_zeroed_page(GFP_KERNEL);
  319. if (!ftrace_pages->next)
  320. return NULL;
  321. }
  322. ftrace_pages = ftrace_pages->next;
  323. }
  324. return &ftrace_pages->records[ftrace_pages->index++];
  325. }
  326. static struct dyn_ftrace *
  327. ftrace_record_ip(unsigned long ip)
  328. {
  329. struct dyn_ftrace *rec;
  330. if (ftrace_disabled)
  331. return NULL;
  332. rec = ftrace_alloc_dyn_node(ip);
  333. if (!rec)
  334. return NULL;
  335. rec->ip = ip;
  336. list_add(&rec->list, &ftrace_new_addrs);
  337. return rec;
  338. }
  339. static void print_ip_ins(const char *fmt, unsigned char *p)
  340. {
  341. int i;
  342. printk(KERN_CONT "%s", fmt);
  343. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  344. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  345. }
  346. static void ftrace_bug(int failed, unsigned long ip)
  347. {
  348. switch (failed) {
  349. case -EFAULT:
  350. FTRACE_WARN_ON_ONCE(1);
  351. pr_info("ftrace faulted on modifying ");
  352. print_ip_sym(ip);
  353. break;
  354. case -EINVAL:
  355. FTRACE_WARN_ON_ONCE(1);
  356. pr_info("ftrace failed to modify ");
  357. print_ip_sym(ip);
  358. print_ip_ins(" actual: ", (unsigned char *)ip);
  359. printk(KERN_CONT "\n");
  360. break;
  361. case -EPERM:
  362. FTRACE_WARN_ON_ONCE(1);
  363. pr_info("ftrace faulted on writing ");
  364. print_ip_sym(ip);
  365. break;
  366. default:
  367. FTRACE_WARN_ON_ONCE(1);
  368. pr_info("ftrace faulted on unknown error ");
  369. print_ip_sym(ip);
  370. }
  371. }
  372. static int
  373. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  374. {
  375. unsigned long ftrace_addr;
  376. unsigned long ip, fl;
  377. ftrace_addr = (unsigned long)FTRACE_ADDR;
  378. ip = rec->ip;
  379. /*
  380. * If this record is not to be traced and
  381. * it is not enabled then do nothing.
  382. *
  383. * If this record is not to be traced and
  384. * it is enabled then disable it.
  385. *
  386. */
  387. if (rec->flags & FTRACE_FL_NOTRACE) {
  388. if (rec->flags & FTRACE_FL_ENABLED)
  389. rec->flags &= ~FTRACE_FL_ENABLED;
  390. else
  391. return 0;
  392. } else if (ftrace_filtered && enable) {
  393. /*
  394. * Filtering is on:
  395. */
  396. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  397. /* Record is filtered and enabled, do nothing */
  398. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  399. return 0;
  400. /* Record is not filtered or enabled, do nothing */
  401. if (!fl)
  402. return 0;
  403. /* Record is not filtered but enabled, disable it */
  404. if (fl == FTRACE_FL_ENABLED)
  405. rec->flags &= ~FTRACE_FL_ENABLED;
  406. else
  407. /* Otherwise record is filtered but not enabled, enable it */
  408. rec->flags |= FTRACE_FL_ENABLED;
  409. } else {
  410. /* Disable or not filtered */
  411. if (enable) {
  412. /* if record is enabled, do nothing */
  413. if (rec->flags & FTRACE_FL_ENABLED)
  414. return 0;
  415. rec->flags |= FTRACE_FL_ENABLED;
  416. } else {
  417. /* if record is not enabled, do nothing */
  418. if (!(rec->flags & FTRACE_FL_ENABLED))
  419. return 0;
  420. rec->flags &= ~FTRACE_FL_ENABLED;
  421. }
  422. }
  423. if (rec->flags & FTRACE_FL_ENABLED)
  424. return ftrace_make_call(rec, ftrace_addr);
  425. else
  426. return ftrace_make_nop(NULL, rec, ftrace_addr);
  427. }
  428. static void ftrace_replace_code(int enable)
  429. {
  430. struct dyn_ftrace *rec;
  431. struct ftrace_page *pg;
  432. int failed;
  433. do_for_each_ftrace_rec(pg, rec) {
  434. /*
  435. * Skip over free records, records that have
  436. * failed and not converted.
  437. */
  438. if (rec->flags & FTRACE_FL_FREE ||
  439. rec->flags & FTRACE_FL_FAILED ||
  440. rec->flags & FTRACE_FL_CONVERTED)
  441. continue;
  442. /* ignore updates to this record's mcount site */
  443. if (get_kprobe((void *)rec->ip)) {
  444. freeze_record(rec);
  445. continue;
  446. } else {
  447. unfreeze_record(rec);
  448. }
  449. failed = __ftrace_replace_code(rec, enable);
  450. if (failed) {
  451. rec->flags |= FTRACE_FL_FAILED;
  452. if ((system_state == SYSTEM_BOOTING) ||
  453. !core_kernel_text(rec->ip)) {
  454. ftrace_free_rec(rec);
  455. } else {
  456. ftrace_bug(failed, rec->ip);
  457. /* Stop processing */
  458. return;
  459. }
  460. }
  461. } while_for_each_ftrace_rec();
  462. }
  463. static int
  464. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  465. {
  466. unsigned long ip;
  467. int ret;
  468. ip = rec->ip;
  469. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  470. if (ret) {
  471. ftrace_bug(ret, ip);
  472. rec->flags |= FTRACE_FL_FAILED;
  473. return 0;
  474. }
  475. return 1;
  476. }
  477. /*
  478. * archs can override this function if they must do something
  479. * before the modifying code is performed.
  480. */
  481. int __weak ftrace_arch_code_modify_prepare(void)
  482. {
  483. return 0;
  484. }
  485. /*
  486. * archs can override this function if they must do something
  487. * after the modifying code is performed.
  488. */
  489. int __weak ftrace_arch_code_modify_post_process(void)
  490. {
  491. return 0;
  492. }
  493. static int __ftrace_modify_code(void *data)
  494. {
  495. int *command = data;
  496. if (*command & FTRACE_ENABLE_CALLS)
  497. ftrace_replace_code(1);
  498. else if (*command & FTRACE_DISABLE_CALLS)
  499. ftrace_replace_code(0);
  500. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  501. ftrace_update_ftrace_func(ftrace_trace_function);
  502. if (*command & FTRACE_START_FUNC_RET)
  503. ftrace_enable_ftrace_graph_caller();
  504. else if (*command & FTRACE_STOP_FUNC_RET)
  505. ftrace_disable_ftrace_graph_caller();
  506. return 0;
  507. }
  508. static void ftrace_run_update_code(int command)
  509. {
  510. int ret;
  511. ret = ftrace_arch_code_modify_prepare();
  512. FTRACE_WARN_ON(ret);
  513. if (ret)
  514. return;
  515. stop_machine(__ftrace_modify_code, &command, NULL);
  516. ret = ftrace_arch_code_modify_post_process();
  517. FTRACE_WARN_ON(ret);
  518. }
  519. static ftrace_func_t saved_ftrace_func;
  520. static int ftrace_start_up;
  521. static void ftrace_startup_enable(int command)
  522. {
  523. if (saved_ftrace_func != ftrace_trace_function) {
  524. saved_ftrace_func = ftrace_trace_function;
  525. command |= FTRACE_UPDATE_TRACE_FUNC;
  526. }
  527. if (!command || !ftrace_enabled)
  528. return;
  529. ftrace_run_update_code(command);
  530. }
  531. static void ftrace_startup(int command)
  532. {
  533. if (unlikely(ftrace_disabled))
  534. return;
  535. ftrace_start_up++;
  536. command |= FTRACE_ENABLE_CALLS;
  537. ftrace_startup_enable(command);
  538. }
  539. static void ftrace_shutdown(int command)
  540. {
  541. if (unlikely(ftrace_disabled))
  542. return;
  543. ftrace_start_up--;
  544. if (!ftrace_start_up)
  545. command |= FTRACE_DISABLE_CALLS;
  546. if (saved_ftrace_func != ftrace_trace_function) {
  547. saved_ftrace_func = ftrace_trace_function;
  548. command |= FTRACE_UPDATE_TRACE_FUNC;
  549. }
  550. if (!command || !ftrace_enabled)
  551. return;
  552. ftrace_run_update_code(command);
  553. }
  554. static void ftrace_startup_sysctl(void)
  555. {
  556. int command = FTRACE_ENABLE_MCOUNT;
  557. if (unlikely(ftrace_disabled))
  558. return;
  559. /* Force update next time */
  560. saved_ftrace_func = NULL;
  561. /* ftrace_start_up is true if we want ftrace running */
  562. if (ftrace_start_up)
  563. command |= FTRACE_ENABLE_CALLS;
  564. ftrace_run_update_code(command);
  565. }
  566. static void ftrace_shutdown_sysctl(void)
  567. {
  568. int command = FTRACE_DISABLE_MCOUNT;
  569. if (unlikely(ftrace_disabled))
  570. return;
  571. /* ftrace_start_up is true if ftrace is running */
  572. if (ftrace_start_up)
  573. command |= FTRACE_DISABLE_CALLS;
  574. ftrace_run_update_code(command);
  575. }
  576. static cycle_t ftrace_update_time;
  577. static unsigned long ftrace_update_cnt;
  578. unsigned long ftrace_update_tot_cnt;
  579. static int ftrace_update_code(struct module *mod)
  580. {
  581. struct dyn_ftrace *p, *t;
  582. cycle_t start, stop;
  583. start = ftrace_now(raw_smp_processor_id());
  584. ftrace_update_cnt = 0;
  585. list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
  586. /* If something went wrong, bail without enabling anything */
  587. if (unlikely(ftrace_disabled))
  588. return -1;
  589. list_del_init(&p->list);
  590. /* convert record (i.e, patch mcount-call with NOP) */
  591. if (ftrace_code_disable(mod, p)) {
  592. p->flags |= FTRACE_FL_CONVERTED;
  593. ftrace_update_cnt++;
  594. } else
  595. ftrace_free_rec(p);
  596. }
  597. stop = ftrace_now(raw_smp_processor_id());
  598. ftrace_update_time = stop - start;
  599. ftrace_update_tot_cnt += ftrace_update_cnt;
  600. return 0;
  601. }
  602. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  603. {
  604. struct ftrace_page *pg;
  605. int cnt;
  606. int i;
  607. /* allocate a few pages */
  608. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  609. if (!ftrace_pages_start)
  610. return -1;
  611. /*
  612. * Allocate a few more pages.
  613. *
  614. * TODO: have some parser search vmlinux before
  615. * final linking to find all calls to ftrace.
  616. * Then we can:
  617. * a) know how many pages to allocate.
  618. * and/or
  619. * b) set up the table then.
  620. *
  621. * The dynamic code is still necessary for
  622. * modules.
  623. */
  624. pg = ftrace_pages = ftrace_pages_start;
  625. cnt = num_to_init / ENTRIES_PER_PAGE;
  626. pr_info("ftrace: allocating %ld entries in %d pages\n",
  627. num_to_init, cnt + 1);
  628. for (i = 0; i < cnt; i++) {
  629. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  630. /* If we fail, we'll try later anyway */
  631. if (!pg->next)
  632. break;
  633. pg = pg->next;
  634. }
  635. return 0;
  636. }
  637. enum {
  638. FTRACE_ITER_FILTER = (1 << 0),
  639. FTRACE_ITER_CONT = (1 << 1),
  640. FTRACE_ITER_NOTRACE = (1 << 2),
  641. FTRACE_ITER_FAILURES = (1 << 3),
  642. FTRACE_ITER_PRINTALL = (1 << 4),
  643. FTRACE_ITER_HASH = (1 << 5),
  644. };
  645. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  646. struct ftrace_iterator {
  647. struct ftrace_page *pg;
  648. int hidx;
  649. int idx;
  650. unsigned flags;
  651. unsigned char buffer[FTRACE_BUFF_MAX+1];
  652. unsigned buffer_idx;
  653. unsigned filtered;
  654. };
  655. static void *
  656. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  657. {
  658. struct ftrace_iterator *iter = m->private;
  659. struct hlist_node *hnd = v;
  660. struct hlist_head *hhd;
  661. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  662. (*pos)++;
  663. retry:
  664. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  665. return NULL;
  666. hhd = &ftrace_func_hash[iter->hidx];
  667. if (hlist_empty(hhd)) {
  668. iter->hidx++;
  669. hnd = NULL;
  670. goto retry;
  671. }
  672. if (!hnd)
  673. hnd = hhd->first;
  674. else {
  675. hnd = hnd->next;
  676. if (!hnd) {
  677. iter->hidx++;
  678. goto retry;
  679. }
  680. }
  681. return hnd;
  682. }
  683. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  684. {
  685. struct ftrace_iterator *iter = m->private;
  686. void *p = NULL;
  687. iter->flags |= FTRACE_ITER_HASH;
  688. return t_hash_next(m, p, pos);
  689. }
  690. static int t_hash_show(struct seq_file *m, void *v)
  691. {
  692. struct ftrace_func_probe *rec;
  693. struct hlist_node *hnd = v;
  694. char str[KSYM_SYMBOL_LEN];
  695. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  696. if (rec->ops->print)
  697. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  698. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  699. seq_printf(m, "%s:", str);
  700. kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
  701. seq_printf(m, "%s", str);
  702. if (rec->data)
  703. seq_printf(m, ":%p", rec->data);
  704. seq_putc(m, '\n');
  705. return 0;
  706. }
  707. static void *
  708. t_next(struct seq_file *m, void *v, loff_t *pos)
  709. {
  710. struct ftrace_iterator *iter = m->private;
  711. struct dyn_ftrace *rec = NULL;
  712. if (iter->flags & FTRACE_ITER_HASH)
  713. return t_hash_next(m, v, pos);
  714. (*pos)++;
  715. if (iter->flags & FTRACE_ITER_PRINTALL)
  716. return NULL;
  717. retry:
  718. if (iter->idx >= iter->pg->index) {
  719. if (iter->pg->next) {
  720. iter->pg = iter->pg->next;
  721. iter->idx = 0;
  722. goto retry;
  723. } else {
  724. iter->idx = -1;
  725. }
  726. } else {
  727. rec = &iter->pg->records[iter->idx++];
  728. if ((rec->flags & FTRACE_FL_FREE) ||
  729. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  730. (rec->flags & FTRACE_FL_FAILED)) ||
  731. ((iter->flags & FTRACE_ITER_FAILURES) &&
  732. !(rec->flags & FTRACE_FL_FAILED)) ||
  733. ((iter->flags & FTRACE_ITER_FILTER) &&
  734. !(rec->flags & FTRACE_FL_FILTER)) ||
  735. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  736. !(rec->flags & FTRACE_FL_NOTRACE))) {
  737. rec = NULL;
  738. goto retry;
  739. }
  740. }
  741. return rec;
  742. }
  743. static void *t_start(struct seq_file *m, loff_t *pos)
  744. {
  745. struct ftrace_iterator *iter = m->private;
  746. void *p = NULL;
  747. mutex_lock(&ftrace_lock);
  748. /*
  749. * For set_ftrace_filter reading, if we have the filter
  750. * off, we can short cut and just print out that all
  751. * functions are enabled.
  752. */
  753. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  754. if (*pos > 0)
  755. return t_hash_start(m, pos);
  756. iter->flags |= FTRACE_ITER_PRINTALL;
  757. (*pos)++;
  758. return iter;
  759. }
  760. if (iter->flags & FTRACE_ITER_HASH)
  761. return t_hash_start(m, pos);
  762. if (*pos > 0) {
  763. if (iter->idx < 0)
  764. return p;
  765. (*pos)--;
  766. iter->idx--;
  767. }
  768. p = t_next(m, p, pos);
  769. if (!p)
  770. return t_hash_start(m, pos);
  771. return p;
  772. }
  773. static void t_stop(struct seq_file *m, void *p)
  774. {
  775. mutex_unlock(&ftrace_lock);
  776. }
  777. static int t_show(struct seq_file *m, void *v)
  778. {
  779. struct ftrace_iterator *iter = m->private;
  780. struct dyn_ftrace *rec = v;
  781. char str[KSYM_SYMBOL_LEN];
  782. if (iter->flags & FTRACE_ITER_HASH)
  783. return t_hash_show(m, v);
  784. if (iter->flags & FTRACE_ITER_PRINTALL) {
  785. seq_printf(m, "#### all functions enabled ####\n");
  786. return 0;
  787. }
  788. if (!rec)
  789. return 0;
  790. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  791. seq_printf(m, "%s\n", str);
  792. return 0;
  793. }
  794. static struct seq_operations show_ftrace_seq_ops = {
  795. .start = t_start,
  796. .next = t_next,
  797. .stop = t_stop,
  798. .show = t_show,
  799. };
  800. static int
  801. ftrace_avail_open(struct inode *inode, struct file *file)
  802. {
  803. struct ftrace_iterator *iter;
  804. int ret;
  805. if (unlikely(ftrace_disabled))
  806. return -ENODEV;
  807. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  808. if (!iter)
  809. return -ENOMEM;
  810. iter->pg = ftrace_pages_start;
  811. ret = seq_open(file, &show_ftrace_seq_ops);
  812. if (!ret) {
  813. struct seq_file *m = file->private_data;
  814. m->private = iter;
  815. } else {
  816. kfree(iter);
  817. }
  818. return ret;
  819. }
  820. int ftrace_avail_release(struct inode *inode, struct file *file)
  821. {
  822. struct seq_file *m = (struct seq_file *)file->private_data;
  823. struct ftrace_iterator *iter = m->private;
  824. seq_release(inode, file);
  825. kfree(iter);
  826. return 0;
  827. }
  828. static int
  829. ftrace_failures_open(struct inode *inode, struct file *file)
  830. {
  831. int ret;
  832. struct seq_file *m;
  833. struct ftrace_iterator *iter;
  834. ret = ftrace_avail_open(inode, file);
  835. if (!ret) {
  836. m = (struct seq_file *)file->private_data;
  837. iter = (struct ftrace_iterator *)m->private;
  838. iter->flags = FTRACE_ITER_FAILURES;
  839. }
  840. return ret;
  841. }
  842. static void ftrace_filter_reset(int enable)
  843. {
  844. struct ftrace_page *pg;
  845. struct dyn_ftrace *rec;
  846. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  847. mutex_lock(&ftrace_lock);
  848. if (enable)
  849. ftrace_filtered = 0;
  850. do_for_each_ftrace_rec(pg, rec) {
  851. if (rec->flags & FTRACE_FL_FAILED)
  852. continue;
  853. rec->flags &= ~type;
  854. } while_for_each_ftrace_rec();
  855. mutex_unlock(&ftrace_lock);
  856. }
  857. static int
  858. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  859. {
  860. struct ftrace_iterator *iter;
  861. int ret = 0;
  862. if (unlikely(ftrace_disabled))
  863. return -ENODEV;
  864. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  865. if (!iter)
  866. return -ENOMEM;
  867. mutex_lock(&ftrace_regex_lock);
  868. if ((file->f_mode & FMODE_WRITE) &&
  869. !(file->f_flags & O_APPEND))
  870. ftrace_filter_reset(enable);
  871. if (file->f_mode & FMODE_READ) {
  872. iter->pg = ftrace_pages_start;
  873. iter->flags = enable ? FTRACE_ITER_FILTER :
  874. FTRACE_ITER_NOTRACE;
  875. ret = seq_open(file, &show_ftrace_seq_ops);
  876. if (!ret) {
  877. struct seq_file *m = file->private_data;
  878. m->private = iter;
  879. } else
  880. kfree(iter);
  881. } else
  882. file->private_data = iter;
  883. mutex_unlock(&ftrace_regex_lock);
  884. return ret;
  885. }
  886. static int
  887. ftrace_filter_open(struct inode *inode, struct file *file)
  888. {
  889. return ftrace_regex_open(inode, file, 1);
  890. }
  891. static int
  892. ftrace_notrace_open(struct inode *inode, struct file *file)
  893. {
  894. return ftrace_regex_open(inode, file, 0);
  895. }
  896. static loff_t
  897. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  898. {
  899. loff_t ret;
  900. if (file->f_mode & FMODE_READ)
  901. ret = seq_lseek(file, offset, origin);
  902. else
  903. file->f_pos = ret = 1;
  904. return ret;
  905. }
  906. enum {
  907. MATCH_FULL,
  908. MATCH_FRONT_ONLY,
  909. MATCH_MIDDLE_ONLY,
  910. MATCH_END_ONLY,
  911. };
  912. /*
  913. * (static function - no need for kernel doc)
  914. *
  915. * Pass in a buffer containing a glob and this function will
  916. * set search to point to the search part of the buffer and
  917. * return the type of search it is (see enum above).
  918. * This does modify buff.
  919. *
  920. * Returns enum type.
  921. * search returns the pointer to use for comparison.
  922. * not returns 1 if buff started with a '!'
  923. * 0 otherwise.
  924. */
  925. static int
  926. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  927. {
  928. int type = MATCH_FULL;
  929. int i;
  930. if (buff[0] == '!') {
  931. *not = 1;
  932. buff++;
  933. len--;
  934. } else
  935. *not = 0;
  936. *search = buff;
  937. for (i = 0; i < len; i++) {
  938. if (buff[i] == '*') {
  939. if (!i) {
  940. *search = buff + 1;
  941. type = MATCH_END_ONLY;
  942. } else {
  943. if (type == MATCH_END_ONLY)
  944. type = MATCH_MIDDLE_ONLY;
  945. else
  946. type = MATCH_FRONT_ONLY;
  947. buff[i] = 0;
  948. break;
  949. }
  950. }
  951. }
  952. return type;
  953. }
  954. static int ftrace_match(char *str, char *regex, int len, int type)
  955. {
  956. int matched = 0;
  957. char *ptr;
  958. switch (type) {
  959. case MATCH_FULL:
  960. if (strcmp(str, regex) == 0)
  961. matched = 1;
  962. break;
  963. case MATCH_FRONT_ONLY:
  964. if (strncmp(str, regex, len) == 0)
  965. matched = 1;
  966. break;
  967. case MATCH_MIDDLE_ONLY:
  968. if (strstr(str, regex))
  969. matched = 1;
  970. break;
  971. case MATCH_END_ONLY:
  972. ptr = strstr(str, regex);
  973. if (ptr && (ptr[len] == 0))
  974. matched = 1;
  975. break;
  976. }
  977. return matched;
  978. }
  979. static int
  980. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  981. {
  982. char str[KSYM_SYMBOL_LEN];
  983. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  984. return ftrace_match(str, regex, len, type);
  985. }
  986. static void ftrace_match_records(char *buff, int len, int enable)
  987. {
  988. unsigned int search_len;
  989. struct ftrace_page *pg;
  990. struct dyn_ftrace *rec;
  991. unsigned long flag;
  992. char *search;
  993. int type;
  994. int not;
  995. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  996. type = ftrace_setup_glob(buff, len, &search, &not);
  997. search_len = strlen(search);
  998. mutex_lock(&ftrace_lock);
  999. do_for_each_ftrace_rec(pg, rec) {
  1000. if (rec->flags & FTRACE_FL_FAILED)
  1001. continue;
  1002. if (ftrace_match_record(rec, search, search_len, type)) {
  1003. if (not)
  1004. rec->flags &= ~flag;
  1005. else
  1006. rec->flags |= flag;
  1007. }
  1008. /*
  1009. * Only enable filtering if we have a function that
  1010. * is filtered on.
  1011. */
  1012. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1013. ftrace_filtered = 1;
  1014. } while_for_each_ftrace_rec();
  1015. mutex_unlock(&ftrace_lock);
  1016. }
  1017. static int
  1018. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1019. char *regex, int len, int type)
  1020. {
  1021. char str[KSYM_SYMBOL_LEN];
  1022. char *modname;
  1023. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1024. if (!modname || strcmp(modname, mod))
  1025. return 0;
  1026. /* blank search means to match all funcs in the mod */
  1027. if (len)
  1028. return ftrace_match(str, regex, len, type);
  1029. else
  1030. return 1;
  1031. }
  1032. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1033. {
  1034. unsigned search_len = 0;
  1035. struct ftrace_page *pg;
  1036. struct dyn_ftrace *rec;
  1037. int type = MATCH_FULL;
  1038. char *search = buff;
  1039. unsigned long flag;
  1040. int not = 0;
  1041. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1042. /* blank or '*' mean the same */
  1043. if (strcmp(buff, "*") == 0)
  1044. buff[0] = 0;
  1045. /* handle the case of 'dont filter this module' */
  1046. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1047. buff[0] = 0;
  1048. not = 1;
  1049. }
  1050. if (strlen(buff)) {
  1051. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1052. search_len = strlen(search);
  1053. }
  1054. mutex_lock(&ftrace_lock);
  1055. do_for_each_ftrace_rec(pg, rec) {
  1056. if (rec->flags & FTRACE_FL_FAILED)
  1057. continue;
  1058. if (ftrace_match_module_record(rec, mod,
  1059. search, search_len, type)) {
  1060. if (not)
  1061. rec->flags &= ~flag;
  1062. else
  1063. rec->flags |= flag;
  1064. }
  1065. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1066. ftrace_filtered = 1;
  1067. } while_for_each_ftrace_rec();
  1068. mutex_unlock(&ftrace_lock);
  1069. }
  1070. /*
  1071. * We register the module command as a template to show others how
  1072. * to register the a command as well.
  1073. */
  1074. static int
  1075. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1076. {
  1077. char *mod;
  1078. /*
  1079. * cmd == 'mod' because we only registered this func
  1080. * for the 'mod' ftrace_func_command.
  1081. * But if you register one func with multiple commands,
  1082. * you can tell which command was used by the cmd
  1083. * parameter.
  1084. */
  1085. /* we must have a module name */
  1086. if (!param)
  1087. return -EINVAL;
  1088. mod = strsep(&param, ":");
  1089. if (!strlen(mod))
  1090. return -EINVAL;
  1091. ftrace_match_module_records(func, mod, enable);
  1092. return 0;
  1093. }
  1094. static struct ftrace_func_command ftrace_mod_cmd = {
  1095. .name = "mod",
  1096. .func = ftrace_mod_callback,
  1097. };
  1098. static int __init ftrace_mod_cmd_init(void)
  1099. {
  1100. return register_ftrace_command(&ftrace_mod_cmd);
  1101. }
  1102. device_initcall(ftrace_mod_cmd_init);
  1103. static void
  1104. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1105. {
  1106. struct ftrace_func_probe *entry;
  1107. struct hlist_head *hhd;
  1108. struct hlist_node *n;
  1109. unsigned long key;
  1110. int resched;
  1111. key = hash_long(ip, FTRACE_HASH_BITS);
  1112. hhd = &ftrace_func_hash[key];
  1113. if (hlist_empty(hhd))
  1114. return;
  1115. /*
  1116. * Disable preemption for these calls to prevent a RCU grace
  1117. * period. This syncs the hash iteration and freeing of items
  1118. * on the hash. rcu_read_lock is too dangerous here.
  1119. */
  1120. resched = ftrace_preempt_disable();
  1121. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1122. if (entry->ip == ip)
  1123. entry->ops->func(ip, parent_ip, &entry->data);
  1124. }
  1125. ftrace_preempt_enable(resched);
  1126. }
  1127. static struct ftrace_ops trace_probe_ops __read_mostly =
  1128. {
  1129. .func = function_trace_probe_call,
  1130. };
  1131. static int ftrace_probe_registered;
  1132. static void __enable_ftrace_function_probe(void)
  1133. {
  1134. int i;
  1135. if (ftrace_probe_registered)
  1136. return;
  1137. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1138. struct hlist_head *hhd = &ftrace_func_hash[i];
  1139. if (hhd->first)
  1140. break;
  1141. }
  1142. /* Nothing registered? */
  1143. if (i == FTRACE_FUNC_HASHSIZE)
  1144. return;
  1145. __register_ftrace_function(&trace_probe_ops);
  1146. ftrace_startup(0);
  1147. ftrace_probe_registered = 1;
  1148. }
  1149. static void __disable_ftrace_function_probe(void)
  1150. {
  1151. int i;
  1152. if (!ftrace_probe_registered)
  1153. return;
  1154. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1155. struct hlist_head *hhd = &ftrace_func_hash[i];
  1156. if (hhd->first)
  1157. return;
  1158. }
  1159. /* no more funcs left */
  1160. __unregister_ftrace_function(&trace_probe_ops);
  1161. ftrace_shutdown(0);
  1162. ftrace_probe_registered = 0;
  1163. }
  1164. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1165. {
  1166. struct ftrace_func_probe *entry =
  1167. container_of(rhp, struct ftrace_func_probe, rcu);
  1168. if (entry->ops->free)
  1169. entry->ops->free(&entry->data);
  1170. kfree(entry);
  1171. }
  1172. int
  1173. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1174. void *data)
  1175. {
  1176. struct ftrace_func_probe *entry;
  1177. struct ftrace_page *pg;
  1178. struct dyn_ftrace *rec;
  1179. int type, len, not;
  1180. unsigned long key;
  1181. int count = 0;
  1182. char *search;
  1183. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1184. len = strlen(search);
  1185. /* we do not support '!' for function probes */
  1186. if (WARN_ON(not))
  1187. return -EINVAL;
  1188. mutex_lock(&ftrace_lock);
  1189. do_for_each_ftrace_rec(pg, rec) {
  1190. if (rec->flags & FTRACE_FL_FAILED)
  1191. continue;
  1192. if (!ftrace_match_record(rec, search, len, type))
  1193. continue;
  1194. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1195. if (!entry) {
  1196. /* If we did not process any, then return error */
  1197. if (!count)
  1198. count = -ENOMEM;
  1199. goto out_unlock;
  1200. }
  1201. count++;
  1202. entry->data = data;
  1203. /*
  1204. * The caller might want to do something special
  1205. * for each function we find. We call the callback
  1206. * to give the caller an opportunity to do so.
  1207. */
  1208. if (ops->callback) {
  1209. if (ops->callback(rec->ip, &entry->data) < 0) {
  1210. /* caller does not like this func */
  1211. kfree(entry);
  1212. continue;
  1213. }
  1214. }
  1215. entry->ops = ops;
  1216. entry->ip = rec->ip;
  1217. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1218. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1219. } while_for_each_ftrace_rec();
  1220. __enable_ftrace_function_probe();
  1221. out_unlock:
  1222. mutex_unlock(&ftrace_lock);
  1223. return count;
  1224. }
  1225. enum {
  1226. PROBE_TEST_FUNC = 1,
  1227. PROBE_TEST_DATA = 2
  1228. };
  1229. static void
  1230. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1231. void *data, int flags)
  1232. {
  1233. struct ftrace_func_probe *entry;
  1234. struct hlist_node *n, *tmp;
  1235. char str[KSYM_SYMBOL_LEN];
  1236. int type = MATCH_FULL;
  1237. int i, len = 0;
  1238. char *search;
  1239. if (glob && (strcmp(glob, "*") || !strlen(glob)))
  1240. glob = NULL;
  1241. else {
  1242. int not;
  1243. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1244. len = strlen(search);
  1245. /* we do not support '!' for function probes */
  1246. if (WARN_ON(not))
  1247. return;
  1248. }
  1249. mutex_lock(&ftrace_lock);
  1250. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1251. struct hlist_head *hhd = &ftrace_func_hash[i];
  1252. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1253. /* break up if statements for readability */
  1254. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1255. continue;
  1256. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1257. continue;
  1258. /* do this last, since it is the most expensive */
  1259. if (glob) {
  1260. kallsyms_lookup(entry->ip, NULL, NULL,
  1261. NULL, str);
  1262. if (!ftrace_match(str, glob, len, type))
  1263. continue;
  1264. }
  1265. hlist_del(&entry->node);
  1266. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1267. }
  1268. }
  1269. __disable_ftrace_function_probe();
  1270. mutex_unlock(&ftrace_lock);
  1271. }
  1272. void
  1273. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1274. void *data)
  1275. {
  1276. __unregister_ftrace_function_probe(glob, ops, data,
  1277. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1278. }
  1279. void
  1280. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1281. {
  1282. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1283. }
  1284. void unregister_ftrace_function_probe_all(char *glob)
  1285. {
  1286. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1287. }
  1288. static LIST_HEAD(ftrace_commands);
  1289. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1290. int register_ftrace_command(struct ftrace_func_command *cmd)
  1291. {
  1292. struct ftrace_func_command *p;
  1293. int ret = 0;
  1294. mutex_lock(&ftrace_cmd_mutex);
  1295. list_for_each_entry(p, &ftrace_commands, list) {
  1296. if (strcmp(cmd->name, p->name) == 0) {
  1297. ret = -EBUSY;
  1298. goto out_unlock;
  1299. }
  1300. }
  1301. list_add(&cmd->list, &ftrace_commands);
  1302. out_unlock:
  1303. mutex_unlock(&ftrace_cmd_mutex);
  1304. return ret;
  1305. }
  1306. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1307. {
  1308. struct ftrace_func_command *p, *n;
  1309. int ret = -ENODEV;
  1310. mutex_lock(&ftrace_cmd_mutex);
  1311. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1312. if (strcmp(cmd->name, p->name) == 0) {
  1313. ret = 0;
  1314. list_del_init(&p->list);
  1315. goto out_unlock;
  1316. }
  1317. }
  1318. out_unlock:
  1319. mutex_unlock(&ftrace_cmd_mutex);
  1320. return ret;
  1321. }
  1322. static int ftrace_process_regex(char *buff, int len, int enable)
  1323. {
  1324. char *func, *command, *next = buff;
  1325. struct ftrace_func_command *p;
  1326. int ret = -EINVAL;
  1327. func = strsep(&next, ":");
  1328. if (!next) {
  1329. ftrace_match_records(func, len, enable);
  1330. return 0;
  1331. }
  1332. /* command found */
  1333. command = strsep(&next, ":");
  1334. mutex_lock(&ftrace_cmd_mutex);
  1335. list_for_each_entry(p, &ftrace_commands, list) {
  1336. if (strcmp(p->name, command) == 0) {
  1337. ret = p->func(func, command, next, enable);
  1338. goto out_unlock;
  1339. }
  1340. }
  1341. out_unlock:
  1342. mutex_unlock(&ftrace_cmd_mutex);
  1343. return ret;
  1344. }
  1345. static ssize_t
  1346. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1347. size_t cnt, loff_t *ppos, int enable)
  1348. {
  1349. struct ftrace_iterator *iter;
  1350. char ch;
  1351. size_t read = 0;
  1352. ssize_t ret;
  1353. if (!cnt || cnt < 0)
  1354. return 0;
  1355. mutex_lock(&ftrace_regex_lock);
  1356. if (file->f_mode & FMODE_READ) {
  1357. struct seq_file *m = file->private_data;
  1358. iter = m->private;
  1359. } else
  1360. iter = file->private_data;
  1361. if (!*ppos) {
  1362. iter->flags &= ~FTRACE_ITER_CONT;
  1363. iter->buffer_idx = 0;
  1364. }
  1365. ret = get_user(ch, ubuf++);
  1366. if (ret)
  1367. goto out;
  1368. read++;
  1369. cnt--;
  1370. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1371. /* skip white space */
  1372. while (cnt && isspace(ch)) {
  1373. ret = get_user(ch, ubuf++);
  1374. if (ret)
  1375. goto out;
  1376. read++;
  1377. cnt--;
  1378. }
  1379. if (isspace(ch)) {
  1380. file->f_pos += read;
  1381. ret = read;
  1382. goto out;
  1383. }
  1384. iter->buffer_idx = 0;
  1385. }
  1386. while (cnt && !isspace(ch)) {
  1387. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1388. iter->buffer[iter->buffer_idx++] = ch;
  1389. else {
  1390. ret = -EINVAL;
  1391. goto out;
  1392. }
  1393. ret = get_user(ch, ubuf++);
  1394. if (ret)
  1395. goto out;
  1396. read++;
  1397. cnt--;
  1398. }
  1399. if (isspace(ch)) {
  1400. iter->filtered++;
  1401. iter->buffer[iter->buffer_idx] = 0;
  1402. ret = ftrace_process_regex(iter->buffer,
  1403. iter->buffer_idx, enable);
  1404. if (ret)
  1405. goto out;
  1406. iter->buffer_idx = 0;
  1407. } else
  1408. iter->flags |= FTRACE_ITER_CONT;
  1409. file->f_pos += read;
  1410. ret = read;
  1411. out:
  1412. mutex_unlock(&ftrace_regex_lock);
  1413. return ret;
  1414. }
  1415. static ssize_t
  1416. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1417. size_t cnt, loff_t *ppos)
  1418. {
  1419. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1420. }
  1421. static ssize_t
  1422. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1423. size_t cnt, loff_t *ppos)
  1424. {
  1425. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1426. }
  1427. static void
  1428. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1429. {
  1430. if (unlikely(ftrace_disabled))
  1431. return;
  1432. mutex_lock(&ftrace_regex_lock);
  1433. if (reset)
  1434. ftrace_filter_reset(enable);
  1435. if (buf)
  1436. ftrace_match_records(buf, len, enable);
  1437. mutex_unlock(&ftrace_regex_lock);
  1438. }
  1439. /**
  1440. * ftrace_set_filter - set a function to filter on in ftrace
  1441. * @buf - the string that holds the function filter text.
  1442. * @len - the length of the string.
  1443. * @reset - non zero to reset all filters before applying this filter.
  1444. *
  1445. * Filters denote which functions should be enabled when tracing is enabled.
  1446. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1447. */
  1448. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1449. {
  1450. ftrace_set_regex(buf, len, reset, 1);
  1451. }
  1452. /**
  1453. * ftrace_set_notrace - set a function to not trace in ftrace
  1454. * @buf - the string that holds the function notrace text.
  1455. * @len - the length of the string.
  1456. * @reset - non zero to reset all filters before applying this filter.
  1457. *
  1458. * Notrace Filters denote which functions should not be enabled when tracing
  1459. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1460. * for tracing.
  1461. */
  1462. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1463. {
  1464. ftrace_set_regex(buf, len, reset, 0);
  1465. }
  1466. static int
  1467. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1468. {
  1469. struct seq_file *m = (struct seq_file *)file->private_data;
  1470. struct ftrace_iterator *iter;
  1471. mutex_lock(&ftrace_regex_lock);
  1472. if (file->f_mode & FMODE_READ) {
  1473. iter = m->private;
  1474. seq_release(inode, file);
  1475. } else
  1476. iter = file->private_data;
  1477. if (iter->buffer_idx) {
  1478. iter->filtered++;
  1479. iter->buffer[iter->buffer_idx] = 0;
  1480. ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
  1481. }
  1482. mutex_lock(&ftrace_lock);
  1483. if (ftrace_start_up && ftrace_enabled)
  1484. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1485. mutex_unlock(&ftrace_lock);
  1486. kfree(iter);
  1487. mutex_unlock(&ftrace_regex_lock);
  1488. return 0;
  1489. }
  1490. static int
  1491. ftrace_filter_release(struct inode *inode, struct file *file)
  1492. {
  1493. return ftrace_regex_release(inode, file, 1);
  1494. }
  1495. static int
  1496. ftrace_notrace_release(struct inode *inode, struct file *file)
  1497. {
  1498. return ftrace_regex_release(inode, file, 0);
  1499. }
  1500. static const struct file_operations ftrace_avail_fops = {
  1501. .open = ftrace_avail_open,
  1502. .read = seq_read,
  1503. .llseek = seq_lseek,
  1504. .release = ftrace_avail_release,
  1505. };
  1506. static const struct file_operations ftrace_failures_fops = {
  1507. .open = ftrace_failures_open,
  1508. .read = seq_read,
  1509. .llseek = seq_lseek,
  1510. .release = ftrace_avail_release,
  1511. };
  1512. static const struct file_operations ftrace_filter_fops = {
  1513. .open = ftrace_filter_open,
  1514. .read = seq_read,
  1515. .write = ftrace_filter_write,
  1516. .llseek = ftrace_regex_lseek,
  1517. .release = ftrace_filter_release,
  1518. };
  1519. static const struct file_operations ftrace_notrace_fops = {
  1520. .open = ftrace_notrace_open,
  1521. .read = seq_read,
  1522. .write = ftrace_notrace_write,
  1523. .llseek = ftrace_regex_lseek,
  1524. .release = ftrace_notrace_release,
  1525. };
  1526. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1527. static DEFINE_MUTEX(graph_lock);
  1528. int ftrace_graph_count;
  1529. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1530. static void *
  1531. g_next(struct seq_file *m, void *v, loff_t *pos)
  1532. {
  1533. unsigned long *array = m->private;
  1534. int index = *pos;
  1535. (*pos)++;
  1536. if (index >= ftrace_graph_count)
  1537. return NULL;
  1538. return &array[index];
  1539. }
  1540. static void *g_start(struct seq_file *m, loff_t *pos)
  1541. {
  1542. void *p = NULL;
  1543. mutex_lock(&graph_lock);
  1544. /* Nothing, tell g_show to print all functions are enabled */
  1545. if (!ftrace_graph_count && !*pos)
  1546. return (void *)1;
  1547. p = g_next(m, p, pos);
  1548. return p;
  1549. }
  1550. static void g_stop(struct seq_file *m, void *p)
  1551. {
  1552. mutex_unlock(&graph_lock);
  1553. }
  1554. static int g_show(struct seq_file *m, void *v)
  1555. {
  1556. unsigned long *ptr = v;
  1557. char str[KSYM_SYMBOL_LEN];
  1558. if (!ptr)
  1559. return 0;
  1560. if (ptr == (unsigned long *)1) {
  1561. seq_printf(m, "#### all functions enabled ####\n");
  1562. return 0;
  1563. }
  1564. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1565. seq_printf(m, "%s\n", str);
  1566. return 0;
  1567. }
  1568. static struct seq_operations ftrace_graph_seq_ops = {
  1569. .start = g_start,
  1570. .next = g_next,
  1571. .stop = g_stop,
  1572. .show = g_show,
  1573. };
  1574. static int
  1575. ftrace_graph_open(struct inode *inode, struct file *file)
  1576. {
  1577. int ret = 0;
  1578. if (unlikely(ftrace_disabled))
  1579. return -ENODEV;
  1580. mutex_lock(&graph_lock);
  1581. if ((file->f_mode & FMODE_WRITE) &&
  1582. !(file->f_flags & O_APPEND)) {
  1583. ftrace_graph_count = 0;
  1584. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1585. }
  1586. if (file->f_mode & FMODE_READ) {
  1587. ret = seq_open(file, &ftrace_graph_seq_ops);
  1588. if (!ret) {
  1589. struct seq_file *m = file->private_data;
  1590. m->private = ftrace_graph_funcs;
  1591. }
  1592. } else
  1593. file->private_data = ftrace_graph_funcs;
  1594. mutex_unlock(&graph_lock);
  1595. return ret;
  1596. }
  1597. static int
  1598. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  1599. {
  1600. struct dyn_ftrace *rec;
  1601. struct ftrace_page *pg;
  1602. int search_len;
  1603. int found = 0;
  1604. int type, not;
  1605. char *search;
  1606. bool exists;
  1607. int i;
  1608. if (ftrace_disabled)
  1609. return -ENODEV;
  1610. /* decode regex */
  1611. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  1612. if (not)
  1613. return -EINVAL;
  1614. search_len = strlen(search);
  1615. mutex_lock(&ftrace_lock);
  1616. do_for_each_ftrace_rec(pg, rec) {
  1617. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  1618. break;
  1619. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1620. continue;
  1621. if (ftrace_match_record(rec, search, search_len, type)) {
  1622. /* ensure it is not already in the array */
  1623. exists = false;
  1624. for (i = 0; i < *idx; i++)
  1625. if (array[i] == rec->ip) {
  1626. exists = true;
  1627. break;
  1628. }
  1629. if (!exists) {
  1630. array[(*idx)++] = rec->ip;
  1631. found = 1;
  1632. }
  1633. }
  1634. } while_for_each_ftrace_rec();
  1635. mutex_unlock(&ftrace_lock);
  1636. return found ? 0 : -EINVAL;
  1637. }
  1638. static ssize_t
  1639. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1640. size_t cnt, loff_t *ppos)
  1641. {
  1642. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1643. unsigned long *array;
  1644. size_t read = 0;
  1645. ssize_t ret;
  1646. int index = 0;
  1647. char ch;
  1648. if (!cnt || cnt < 0)
  1649. return 0;
  1650. mutex_lock(&graph_lock);
  1651. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1652. ret = -EBUSY;
  1653. goto out;
  1654. }
  1655. if (file->f_mode & FMODE_READ) {
  1656. struct seq_file *m = file->private_data;
  1657. array = m->private;
  1658. } else
  1659. array = file->private_data;
  1660. ret = get_user(ch, ubuf++);
  1661. if (ret)
  1662. goto out;
  1663. read++;
  1664. cnt--;
  1665. /* skip white space */
  1666. while (cnt && isspace(ch)) {
  1667. ret = get_user(ch, ubuf++);
  1668. if (ret)
  1669. goto out;
  1670. read++;
  1671. cnt--;
  1672. }
  1673. if (isspace(ch)) {
  1674. *ppos += read;
  1675. ret = read;
  1676. goto out;
  1677. }
  1678. while (cnt && !isspace(ch)) {
  1679. if (index < FTRACE_BUFF_MAX)
  1680. buffer[index++] = ch;
  1681. else {
  1682. ret = -EINVAL;
  1683. goto out;
  1684. }
  1685. ret = get_user(ch, ubuf++);
  1686. if (ret)
  1687. goto out;
  1688. read++;
  1689. cnt--;
  1690. }
  1691. buffer[index] = 0;
  1692. /* we allow only one expression at a time */
  1693. ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
  1694. if (ret)
  1695. goto out;
  1696. file->f_pos += read;
  1697. ret = read;
  1698. out:
  1699. mutex_unlock(&graph_lock);
  1700. return ret;
  1701. }
  1702. static const struct file_operations ftrace_graph_fops = {
  1703. .open = ftrace_graph_open,
  1704. .read = seq_read,
  1705. .write = ftrace_graph_write,
  1706. };
  1707. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1708. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1709. {
  1710. struct dentry *entry;
  1711. entry = debugfs_create_file("available_filter_functions", 0444,
  1712. d_tracer, NULL, &ftrace_avail_fops);
  1713. if (!entry)
  1714. pr_warning("Could not create debugfs "
  1715. "'available_filter_functions' entry\n");
  1716. entry = debugfs_create_file("failures", 0444,
  1717. d_tracer, NULL, &ftrace_failures_fops);
  1718. if (!entry)
  1719. pr_warning("Could not create debugfs 'failures' entry\n");
  1720. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1721. NULL, &ftrace_filter_fops);
  1722. if (!entry)
  1723. pr_warning("Could not create debugfs "
  1724. "'set_ftrace_filter' entry\n");
  1725. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1726. NULL, &ftrace_notrace_fops);
  1727. if (!entry)
  1728. pr_warning("Could not create debugfs "
  1729. "'set_ftrace_notrace' entry\n");
  1730. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1731. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  1732. NULL,
  1733. &ftrace_graph_fops);
  1734. if (!entry)
  1735. pr_warning("Could not create debugfs "
  1736. "'set_graph_function' entry\n");
  1737. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1738. return 0;
  1739. }
  1740. static int ftrace_convert_nops(struct module *mod,
  1741. unsigned long *start,
  1742. unsigned long *end)
  1743. {
  1744. unsigned long *p;
  1745. unsigned long addr;
  1746. unsigned long flags;
  1747. mutex_lock(&ftrace_lock);
  1748. p = start;
  1749. while (p < end) {
  1750. addr = ftrace_call_adjust(*p++);
  1751. /*
  1752. * Some architecture linkers will pad between
  1753. * the different mcount_loc sections of different
  1754. * object files to satisfy alignments.
  1755. * Skip any NULL pointers.
  1756. */
  1757. if (!addr)
  1758. continue;
  1759. ftrace_record_ip(addr);
  1760. }
  1761. /* disable interrupts to prevent kstop machine */
  1762. local_irq_save(flags);
  1763. ftrace_update_code(mod);
  1764. local_irq_restore(flags);
  1765. mutex_unlock(&ftrace_lock);
  1766. return 0;
  1767. }
  1768. void ftrace_init_module(struct module *mod,
  1769. unsigned long *start, unsigned long *end)
  1770. {
  1771. if (ftrace_disabled || start == end)
  1772. return;
  1773. ftrace_convert_nops(mod, start, end);
  1774. }
  1775. extern unsigned long __start_mcount_loc[];
  1776. extern unsigned long __stop_mcount_loc[];
  1777. void __init ftrace_init(void)
  1778. {
  1779. unsigned long count, addr, flags;
  1780. int ret;
  1781. /* Keep the ftrace pointer to the stub */
  1782. addr = (unsigned long)ftrace_stub;
  1783. local_irq_save(flags);
  1784. ftrace_dyn_arch_init(&addr);
  1785. local_irq_restore(flags);
  1786. /* ftrace_dyn_arch_init places the return code in addr */
  1787. if (addr)
  1788. goto failed;
  1789. count = __stop_mcount_loc - __start_mcount_loc;
  1790. ret = ftrace_dyn_table_alloc(count);
  1791. if (ret)
  1792. goto failed;
  1793. last_ftrace_enabled = ftrace_enabled = 1;
  1794. ret = ftrace_convert_nops(NULL,
  1795. __start_mcount_loc,
  1796. __stop_mcount_loc);
  1797. return;
  1798. failed:
  1799. ftrace_disabled = 1;
  1800. }
  1801. #else
  1802. static int __init ftrace_nodyn_init(void)
  1803. {
  1804. ftrace_enabled = 1;
  1805. return 0;
  1806. }
  1807. device_initcall(ftrace_nodyn_init);
  1808. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1809. static inline void ftrace_startup_enable(int command) { }
  1810. /* Keep as macros so we do not need to define the commands */
  1811. # define ftrace_startup(command) do { } while (0)
  1812. # define ftrace_shutdown(command) do { } while (0)
  1813. # define ftrace_startup_sysctl() do { } while (0)
  1814. # define ftrace_shutdown_sysctl() do { } while (0)
  1815. #endif /* CONFIG_DYNAMIC_FTRACE */
  1816. static ssize_t
  1817. ftrace_pid_read(struct file *file, char __user *ubuf,
  1818. size_t cnt, loff_t *ppos)
  1819. {
  1820. char buf[64];
  1821. int r;
  1822. if (ftrace_pid_trace == ftrace_swapper_pid)
  1823. r = sprintf(buf, "swapper tasks\n");
  1824. else if (ftrace_pid_trace)
  1825. r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
  1826. else
  1827. r = sprintf(buf, "no pid\n");
  1828. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1829. }
  1830. static void clear_ftrace_swapper(void)
  1831. {
  1832. struct task_struct *p;
  1833. int cpu;
  1834. get_online_cpus();
  1835. for_each_online_cpu(cpu) {
  1836. p = idle_task(cpu);
  1837. clear_tsk_trace_trace(p);
  1838. }
  1839. put_online_cpus();
  1840. }
  1841. static void set_ftrace_swapper(void)
  1842. {
  1843. struct task_struct *p;
  1844. int cpu;
  1845. get_online_cpus();
  1846. for_each_online_cpu(cpu) {
  1847. p = idle_task(cpu);
  1848. set_tsk_trace_trace(p);
  1849. }
  1850. put_online_cpus();
  1851. }
  1852. static void clear_ftrace_pid(struct pid *pid)
  1853. {
  1854. struct task_struct *p;
  1855. rcu_read_lock();
  1856. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1857. clear_tsk_trace_trace(p);
  1858. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1859. rcu_read_unlock();
  1860. put_pid(pid);
  1861. }
  1862. static void set_ftrace_pid(struct pid *pid)
  1863. {
  1864. struct task_struct *p;
  1865. rcu_read_lock();
  1866. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1867. set_tsk_trace_trace(p);
  1868. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1869. rcu_read_unlock();
  1870. }
  1871. static void clear_ftrace_pid_task(struct pid **pid)
  1872. {
  1873. if (*pid == ftrace_swapper_pid)
  1874. clear_ftrace_swapper();
  1875. else
  1876. clear_ftrace_pid(*pid);
  1877. *pid = NULL;
  1878. }
  1879. static void set_ftrace_pid_task(struct pid *pid)
  1880. {
  1881. if (pid == ftrace_swapper_pid)
  1882. set_ftrace_swapper();
  1883. else
  1884. set_ftrace_pid(pid);
  1885. }
  1886. static ssize_t
  1887. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1888. size_t cnt, loff_t *ppos)
  1889. {
  1890. struct pid *pid;
  1891. char buf[64];
  1892. long val;
  1893. int ret;
  1894. if (cnt >= sizeof(buf))
  1895. return -EINVAL;
  1896. if (copy_from_user(&buf, ubuf, cnt))
  1897. return -EFAULT;
  1898. buf[cnt] = 0;
  1899. ret = strict_strtol(buf, 10, &val);
  1900. if (ret < 0)
  1901. return ret;
  1902. mutex_lock(&ftrace_lock);
  1903. if (val < 0) {
  1904. /* disable pid tracing */
  1905. if (!ftrace_pid_trace)
  1906. goto out;
  1907. clear_ftrace_pid_task(&ftrace_pid_trace);
  1908. } else {
  1909. /* swapper task is special */
  1910. if (!val) {
  1911. pid = ftrace_swapper_pid;
  1912. if (pid == ftrace_pid_trace)
  1913. goto out;
  1914. } else {
  1915. pid = find_get_pid(val);
  1916. if (pid == ftrace_pid_trace) {
  1917. put_pid(pid);
  1918. goto out;
  1919. }
  1920. }
  1921. if (ftrace_pid_trace)
  1922. clear_ftrace_pid_task(&ftrace_pid_trace);
  1923. if (!pid)
  1924. goto out;
  1925. ftrace_pid_trace = pid;
  1926. set_ftrace_pid_task(ftrace_pid_trace);
  1927. }
  1928. /* update the function call */
  1929. ftrace_update_pid_func();
  1930. ftrace_startup_enable(0);
  1931. out:
  1932. mutex_unlock(&ftrace_lock);
  1933. return cnt;
  1934. }
  1935. static const struct file_operations ftrace_pid_fops = {
  1936. .read = ftrace_pid_read,
  1937. .write = ftrace_pid_write,
  1938. };
  1939. static __init int ftrace_init_debugfs(void)
  1940. {
  1941. struct dentry *d_tracer;
  1942. struct dentry *entry;
  1943. d_tracer = tracing_init_dentry();
  1944. if (!d_tracer)
  1945. return 0;
  1946. ftrace_init_dyn_debugfs(d_tracer);
  1947. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1948. NULL, &ftrace_pid_fops);
  1949. if (!entry)
  1950. pr_warning("Could not create debugfs "
  1951. "'set_ftrace_pid' entry\n");
  1952. return 0;
  1953. }
  1954. fs_initcall(ftrace_init_debugfs);
  1955. /**
  1956. * ftrace_kill - kill ftrace
  1957. *
  1958. * This function should be used by panic code. It stops ftrace
  1959. * but in a not so nice way. If you need to simply kill ftrace
  1960. * from a non-atomic section, use ftrace_kill.
  1961. */
  1962. void ftrace_kill(void)
  1963. {
  1964. ftrace_disabled = 1;
  1965. ftrace_enabled = 0;
  1966. clear_ftrace_function();
  1967. }
  1968. /**
  1969. * register_ftrace_function - register a function for profiling
  1970. * @ops - ops structure that holds the function for profiling.
  1971. *
  1972. * Register a function to be called by all functions in the
  1973. * kernel.
  1974. *
  1975. * Note: @ops->func and all the functions it calls must be labeled
  1976. * with "notrace", otherwise it will go into a
  1977. * recursive loop.
  1978. */
  1979. int register_ftrace_function(struct ftrace_ops *ops)
  1980. {
  1981. int ret;
  1982. if (unlikely(ftrace_disabled))
  1983. return -1;
  1984. mutex_lock(&ftrace_lock);
  1985. ret = __register_ftrace_function(ops);
  1986. ftrace_startup(0);
  1987. mutex_unlock(&ftrace_lock);
  1988. return ret;
  1989. }
  1990. /**
  1991. * unregister_ftrace_function - unregister a function for profiling.
  1992. * @ops - ops structure that holds the function to unregister
  1993. *
  1994. * Unregister a function that was added to be called by ftrace profiling.
  1995. */
  1996. int unregister_ftrace_function(struct ftrace_ops *ops)
  1997. {
  1998. int ret;
  1999. mutex_lock(&ftrace_lock);
  2000. ret = __unregister_ftrace_function(ops);
  2001. ftrace_shutdown(0);
  2002. mutex_unlock(&ftrace_lock);
  2003. return ret;
  2004. }
  2005. int
  2006. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2007. struct file *file, void __user *buffer, size_t *lenp,
  2008. loff_t *ppos)
  2009. {
  2010. int ret;
  2011. if (unlikely(ftrace_disabled))
  2012. return -ENODEV;
  2013. mutex_lock(&ftrace_lock);
  2014. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  2015. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  2016. goto out;
  2017. last_ftrace_enabled = ftrace_enabled;
  2018. if (ftrace_enabled) {
  2019. ftrace_startup_sysctl();
  2020. /* we are starting ftrace again */
  2021. if (ftrace_list != &ftrace_list_end) {
  2022. if (ftrace_list->next == &ftrace_list_end)
  2023. ftrace_trace_function = ftrace_list->func;
  2024. else
  2025. ftrace_trace_function = ftrace_list_func;
  2026. }
  2027. } else {
  2028. /* stopping ftrace calls (just send to ftrace_stub) */
  2029. ftrace_trace_function = ftrace_stub;
  2030. ftrace_shutdown_sysctl();
  2031. }
  2032. out:
  2033. mutex_unlock(&ftrace_lock);
  2034. return ret;
  2035. }
  2036. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2037. static atomic_t ftrace_graph_active;
  2038. static struct notifier_block ftrace_suspend_notifier;
  2039. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2040. {
  2041. return 0;
  2042. }
  2043. /* The callbacks that hook a function */
  2044. trace_func_graph_ret_t ftrace_graph_return =
  2045. (trace_func_graph_ret_t)ftrace_stub;
  2046. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2047. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2048. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2049. {
  2050. int i;
  2051. int ret = 0;
  2052. unsigned long flags;
  2053. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2054. struct task_struct *g, *t;
  2055. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2056. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2057. * sizeof(struct ftrace_ret_stack),
  2058. GFP_KERNEL);
  2059. if (!ret_stack_list[i]) {
  2060. start = 0;
  2061. end = i;
  2062. ret = -ENOMEM;
  2063. goto free;
  2064. }
  2065. }
  2066. read_lock_irqsave(&tasklist_lock, flags);
  2067. do_each_thread(g, t) {
  2068. if (start == end) {
  2069. ret = -EAGAIN;
  2070. goto unlock;
  2071. }
  2072. if (t->ret_stack == NULL) {
  2073. t->curr_ret_stack = -1;
  2074. /* Make sure IRQs see the -1 first: */
  2075. barrier();
  2076. t->ret_stack = ret_stack_list[start++];
  2077. atomic_set(&t->tracing_graph_pause, 0);
  2078. atomic_set(&t->trace_overrun, 0);
  2079. }
  2080. } while_each_thread(g, t);
  2081. unlock:
  2082. read_unlock_irqrestore(&tasklist_lock, flags);
  2083. free:
  2084. for (i = start; i < end; i++)
  2085. kfree(ret_stack_list[i]);
  2086. return ret;
  2087. }
  2088. /* Allocate a return stack for each task */
  2089. static int start_graph_tracing(void)
  2090. {
  2091. struct ftrace_ret_stack **ret_stack_list;
  2092. int ret, cpu;
  2093. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2094. sizeof(struct ftrace_ret_stack *),
  2095. GFP_KERNEL);
  2096. if (!ret_stack_list)
  2097. return -ENOMEM;
  2098. /* The cpu_boot init_task->ret_stack will never be freed */
  2099. for_each_online_cpu(cpu)
  2100. ftrace_graph_init_task(idle_task(cpu));
  2101. do {
  2102. ret = alloc_retstack_tasklist(ret_stack_list);
  2103. } while (ret == -EAGAIN);
  2104. kfree(ret_stack_list);
  2105. return ret;
  2106. }
  2107. /*
  2108. * Hibernation protection.
  2109. * The state of the current task is too much unstable during
  2110. * suspend/restore to disk. We want to protect against that.
  2111. */
  2112. static int
  2113. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2114. void *unused)
  2115. {
  2116. switch (state) {
  2117. case PM_HIBERNATION_PREPARE:
  2118. pause_graph_tracing();
  2119. break;
  2120. case PM_POST_HIBERNATION:
  2121. unpause_graph_tracing();
  2122. break;
  2123. }
  2124. return NOTIFY_DONE;
  2125. }
  2126. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2127. trace_func_graph_ent_t entryfunc)
  2128. {
  2129. int ret = 0;
  2130. mutex_lock(&ftrace_lock);
  2131. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2132. register_pm_notifier(&ftrace_suspend_notifier);
  2133. atomic_inc(&ftrace_graph_active);
  2134. ret = start_graph_tracing();
  2135. if (ret) {
  2136. atomic_dec(&ftrace_graph_active);
  2137. goto out;
  2138. }
  2139. ftrace_graph_return = retfunc;
  2140. ftrace_graph_entry = entryfunc;
  2141. ftrace_startup(FTRACE_START_FUNC_RET);
  2142. out:
  2143. mutex_unlock(&ftrace_lock);
  2144. return ret;
  2145. }
  2146. void unregister_ftrace_graph(void)
  2147. {
  2148. mutex_lock(&ftrace_lock);
  2149. atomic_dec(&ftrace_graph_active);
  2150. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2151. ftrace_graph_entry = ftrace_graph_entry_stub;
  2152. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2153. unregister_pm_notifier(&ftrace_suspend_notifier);
  2154. mutex_unlock(&ftrace_lock);
  2155. }
  2156. /* Allocate a return stack for newly created task */
  2157. void ftrace_graph_init_task(struct task_struct *t)
  2158. {
  2159. if (atomic_read(&ftrace_graph_active)) {
  2160. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2161. * sizeof(struct ftrace_ret_stack),
  2162. GFP_KERNEL);
  2163. if (!t->ret_stack)
  2164. return;
  2165. t->curr_ret_stack = -1;
  2166. atomic_set(&t->tracing_graph_pause, 0);
  2167. atomic_set(&t->trace_overrun, 0);
  2168. } else
  2169. t->ret_stack = NULL;
  2170. }
  2171. void ftrace_graph_exit_task(struct task_struct *t)
  2172. {
  2173. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2174. t->ret_stack = NULL;
  2175. /* NULL must become visible to IRQs before we free it: */
  2176. barrier();
  2177. kfree(ret_stack);
  2178. }
  2179. void ftrace_graph_stop(void)
  2180. {
  2181. ftrace_stop();
  2182. }
  2183. #endif