ftrace.c 55 KB

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