ftrace.c 56 KB

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