ftrace.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  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/debugfs.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/kthread.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/kprobes.h>
  24. #include <linux/ftrace.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/ctype.h>
  27. #include <linux/list.h>
  28. #include <asm/ftrace.h>
  29. #include "trace.h"
  30. #define FTRACE_WARN_ON(cond) \
  31. do { \
  32. if (WARN_ON(cond)) \
  33. ftrace_kill(); \
  34. } while (0)
  35. #define FTRACE_WARN_ON_ONCE(cond) \
  36. do { \
  37. if (WARN_ON_ONCE(cond)) \
  38. ftrace_kill(); \
  39. } while (0)
  40. /* ftrace_enabled is a method to turn ftrace on or off */
  41. int ftrace_enabled __read_mostly;
  42. static int last_ftrace_enabled;
  43. /* ftrace_pid_trace >= 0 will only trace threads with this pid */
  44. static int ftrace_pid_trace = -1;
  45. /* Quick disabling of function tracer. */
  46. int function_trace_stop;
  47. /*
  48. * ftrace_disabled is set when an anomaly is discovered.
  49. * ftrace_disabled is much stronger than ftrace_enabled.
  50. */
  51. static int ftrace_disabled __read_mostly;
  52. static DEFINE_SPINLOCK(ftrace_lock);
  53. static DEFINE_MUTEX(ftrace_sysctl_lock);
  54. static DEFINE_MUTEX(ftrace_start_lock);
  55. static struct ftrace_ops ftrace_list_end __read_mostly =
  56. {
  57. .func = ftrace_stub,
  58. };
  59. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  60. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  61. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  62. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  63. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  64. {
  65. struct ftrace_ops *op = ftrace_list;
  66. /* in case someone actually ports this to alpha! */
  67. read_barrier_depends();
  68. while (op != &ftrace_list_end) {
  69. /* silly alpha */
  70. read_barrier_depends();
  71. op->func(ip, parent_ip);
  72. op = op->next;
  73. };
  74. }
  75. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  76. {
  77. if (current->pid != ftrace_pid_trace)
  78. return;
  79. ftrace_pid_function(ip, parent_ip);
  80. }
  81. static void set_ftrace_pid_function(ftrace_func_t func)
  82. {
  83. /* do not set ftrace_pid_function to itself! */
  84. if (func != ftrace_pid_func)
  85. ftrace_pid_function = func;
  86. }
  87. /**
  88. * clear_ftrace_function - reset the ftrace function
  89. *
  90. * This NULLs the ftrace function and in essence stops
  91. * tracing. There may be lag
  92. */
  93. void clear_ftrace_function(void)
  94. {
  95. ftrace_trace_function = ftrace_stub;
  96. __ftrace_trace_function = ftrace_stub;
  97. ftrace_pid_function = ftrace_stub;
  98. }
  99. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  100. /*
  101. * For those archs that do not test ftrace_trace_stop in their
  102. * mcount call site, we need to do it from C.
  103. */
  104. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  105. {
  106. if (function_trace_stop)
  107. return;
  108. __ftrace_trace_function(ip, parent_ip);
  109. }
  110. #endif
  111. static int __register_ftrace_function(struct ftrace_ops *ops)
  112. {
  113. /* should not be called from interrupt context */
  114. spin_lock(&ftrace_lock);
  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 >= 0) {
  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. spin_unlock(&ftrace_lock);
  146. return 0;
  147. }
  148. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  149. {
  150. struct ftrace_ops **p;
  151. int ret = 0;
  152. /* should not be called from interrupt context */
  153. spin_lock(&ftrace_lock);
  154. /*
  155. * If we are removing the last function, then simply point
  156. * to the ftrace_stub.
  157. */
  158. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  159. ftrace_trace_function = ftrace_stub;
  160. ftrace_list = &ftrace_list_end;
  161. goto out;
  162. }
  163. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  164. if (*p == ops)
  165. break;
  166. if (*p != ops) {
  167. ret = -1;
  168. goto out;
  169. }
  170. *p = (*p)->next;
  171. if (ftrace_enabled) {
  172. /* If we only have one func left, then call that directly */
  173. if (ftrace_list->next == &ftrace_list_end) {
  174. ftrace_func_t func = ftrace_list->func;
  175. if (ftrace_pid_trace >= 0) {
  176. set_ftrace_pid_function(func);
  177. func = ftrace_pid_func;
  178. }
  179. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  180. ftrace_trace_function = func;
  181. #else
  182. __ftrace_trace_function = func;
  183. #endif
  184. }
  185. }
  186. out:
  187. spin_unlock(&ftrace_lock);
  188. return ret;
  189. }
  190. static void ftrace_update_pid_func(void)
  191. {
  192. ftrace_func_t func;
  193. /* should not be called from interrupt context */
  194. spin_lock(&ftrace_lock);
  195. if (ftrace_trace_function == ftrace_stub)
  196. goto out;
  197. func = ftrace_trace_function;
  198. if (ftrace_pid_trace >= 0) {
  199. set_ftrace_pid_function(func);
  200. func = ftrace_pid_func;
  201. } else {
  202. if (func == ftrace_pid_func)
  203. func = ftrace_pid_function;
  204. }
  205. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  206. ftrace_trace_function = func;
  207. #else
  208. __ftrace_trace_function = func;
  209. #endif
  210. out:
  211. spin_unlock(&ftrace_lock);
  212. }
  213. #ifdef CONFIG_DYNAMIC_FTRACE
  214. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  215. # error Dynamic ftrace depends on MCOUNT_RECORD
  216. #endif
  217. /*
  218. * Since MCOUNT_ADDR may point to mcount itself, we do not want
  219. * to get it confused by reading a reference in the code as we
  220. * are parsing on objcopy output of text. Use a variable for
  221. * it instead.
  222. */
  223. static unsigned long mcount_addr = MCOUNT_ADDR;
  224. enum {
  225. FTRACE_ENABLE_CALLS = (1 << 0),
  226. FTRACE_DISABLE_CALLS = (1 << 1),
  227. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  228. FTRACE_ENABLE_MCOUNT = (1 << 3),
  229. FTRACE_DISABLE_MCOUNT = (1 << 4),
  230. FTRACE_START_FUNC_RET = (1 << 5),
  231. FTRACE_STOP_FUNC_RET = (1 << 6),
  232. };
  233. static int ftrace_filtered;
  234. static LIST_HEAD(ftrace_new_addrs);
  235. static DEFINE_MUTEX(ftrace_regex_lock);
  236. struct ftrace_page {
  237. struct ftrace_page *next;
  238. unsigned long index;
  239. struct dyn_ftrace records[];
  240. };
  241. #define ENTRIES_PER_PAGE \
  242. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  243. /* estimate from running different kernels */
  244. #define NR_TO_INIT 10000
  245. static struct ftrace_page *ftrace_pages_start;
  246. static struct ftrace_page *ftrace_pages;
  247. static struct dyn_ftrace *ftrace_free_records;
  248. #ifdef CONFIG_KPROBES
  249. static int frozen_record_count;
  250. static inline void freeze_record(struct dyn_ftrace *rec)
  251. {
  252. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  253. rec->flags |= FTRACE_FL_FROZEN;
  254. frozen_record_count++;
  255. }
  256. }
  257. static inline void unfreeze_record(struct dyn_ftrace *rec)
  258. {
  259. if (rec->flags & FTRACE_FL_FROZEN) {
  260. rec->flags &= ~FTRACE_FL_FROZEN;
  261. frozen_record_count--;
  262. }
  263. }
  264. static inline int record_frozen(struct dyn_ftrace *rec)
  265. {
  266. return rec->flags & FTRACE_FL_FROZEN;
  267. }
  268. #else
  269. # define freeze_record(rec) ({ 0; })
  270. # define unfreeze_record(rec) ({ 0; })
  271. # define record_frozen(rec) ({ 0; })
  272. #endif /* CONFIG_KPROBES */
  273. static void ftrace_free_rec(struct dyn_ftrace *rec)
  274. {
  275. rec->ip = (unsigned long)ftrace_free_records;
  276. ftrace_free_records = rec;
  277. rec->flags |= FTRACE_FL_FREE;
  278. }
  279. void ftrace_release(void *start, unsigned long size)
  280. {
  281. struct dyn_ftrace *rec;
  282. struct ftrace_page *pg;
  283. unsigned long s = (unsigned long)start;
  284. unsigned long e = s + size;
  285. int i;
  286. if (ftrace_disabled || !start)
  287. return;
  288. /* should not be called from interrupt context */
  289. spin_lock(&ftrace_lock);
  290. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  291. for (i = 0; i < pg->index; i++) {
  292. rec = &pg->records[i];
  293. if ((rec->ip >= s) && (rec->ip < e))
  294. ftrace_free_rec(rec);
  295. }
  296. }
  297. spin_unlock(&ftrace_lock);
  298. }
  299. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  300. {
  301. struct dyn_ftrace *rec;
  302. /* First check for freed records */
  303. if (ftrace_free_records) {
  304. rec = ftrace_free_records;
  305. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  306. FTRACE_WARN_ON_ONCE(1);
  307. ftrace_free_records = NULL;
  308. return NULL;
  309. }
  310. ftrace_free_records = (void *)rec->ip;
  311. memset(rec, 0, sizeof(*rec));
  312. return rec;
  313. }
  314. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  315. if (!ftrace_pages->next) {
  316. /* allocate another page */
  317. ftrace_pages->next =
  318. (void *)get_zeroed_page(GFP_KERNEL);
  319. if (!ftrace_pages->next)
  320. return NULL;
  321. }
  322. ftrace_pages = ftrace_pages->next;
  323. }
  324. return &ftrace_pages->records[ftrace_pages->index++];
  325. }
  326. static struct dyn_ftrace *
  327. ftrace_record_ip(unsigned long ip)
  328. {
  329. struct dyn_ftrace *rec;
  330. if (ftrace_disabled)
  331. return NULL;
  332. rec = ftrace_alloc_dyn_node(ip);
  333. if (!rec)
  334. return NULL;
  335. rec->ip = ip;
  336. list_add(&rec->list, &ftrace_new_addrs);
  337. return rec;
  338. }
  339. static void print_ip_ins(const char *fmt, unsigned char *p)
  340. {
  341. int i;
  342. printk(KERN_CONT "%s", fmt);
  343. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  344. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  345. }
  346. static void ftrace_bug(int failed, unsigned long ip)
  347. {
  348. switch (failed) {
  349. case -EFAULT:
  350. FTRACE_WARN_ON_ONCE(1);
  351. pr_info("ftrace faulted on modifying ");
  352. print_ip_sym(ip);
  353. break;
  354. case -EINVAL:
  355. FTRACE_WARN_ON_ONCE(1);
  356. pr_info("ftrace failed to modify ");
  357. print_ip_sym(ip);
  358. print_ip_ins(" actual: ", (unsigned char *)ip);
  359. printk(KERN_CONT "\n");
  360. break;
  361. case -EPERM:
  362. FTRACE_WARN_ON_ONCE(1);
  363. pr_info("ftrace faulted on writing ");
  364. print_ip_sym(ip);
  365. break;
  366. default:
  367. FTRACE_WARN_ON_ONCE(1);
  368. pr_info("ftrace faulted on unknown error ");
  369. print_ip_sym(ip);
  370. }
  371. }
  372. static int
  373. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  374. {
  375. unsigned long ip, fl;
  376. unsigned long ftrace_addr;
  377. ftrace_addr = (unsigned long)ftrace_caller;
  378. ip = rec->ip;
  379. /*
  380. * If this record is not to be traced and
  381. * it is not enabled then do nothing.
  382. *
  383. * If this record is not to be traced and
  384. * it is enabled then disabled it.
  385. *
  386. */
  387. if (rec->flags & FTRACE_FL_NOTRACE) {
  388. if (rec->flags & FTRACE_FL_ENABLED)
  389. rec->flags &= ~FTRACE_FL_ENABLED;
  390. else
  391. return 0;
  392. } else if (ftrace_filtered && enable) {
  393. /*
  394. * Filtering is on:
  395. */
  396. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  397. /* Record is filtered and enabled, do nothing */
  398. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  399. return 0;
  400. /* Record is not filtered and is not enabled do nothing */
  401. if (!fl)
  402. return 0;
  403. /* Record is not filtered but enabled, disable it */
  404. if (fl == FTRACE_FL_ENABLED)
  405. rec->flags &= ~FTRACE_FL_ENABLED;
  406. else
  407. /* Otherwise record is filtered but not enabled, enable it */
  408. rec->flags |= FTRACE_FL_ENABLED;
  409. } else {
  410. /* Disable or not filtered */
  411. if (enable) {
  412. /* if record is enabled, do nothing */
  413. if (rec->flags & FTRACE_FL_ENABLED)
  414. return 0;
  415. rec->flags |= FTRACE_FL_ENABLED;
  416. } else {
  417. /* if record is not enabled do nothing */
  418. if (!(rec->flags & FTRACE_FL_ENABLED))
  419. return 0;
  420. rec->flags &= ~FTRACE_FL_ENABLED;
  421. }
  422. }
  423. if (rec->flags & FTRACE_FL_ENABLED)
  424. return ftrace_make_call(rec, ftrace_addr);
  425. else
  426. return ftrace_make_nop(NULL, rec, ftrace_addr);
  427. }
  428. static void ftrace_replace_code(int enable)
  429. {
  430. int i, failed;
  431. struct dyn_ftrace *rec;
  432. struct ftrace_page *pg;
  433. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  434. for (i = 0; i < pg->index; i++) {
  435. rec = &pg->records[i];
  436. /*
  437. * Skip over free records and records that have
  438. * failed.
  439. */
  440. if (rec->flags & FTRACE_FL_FREE ||
  441. rec->flags & FTRACE_FL_FAILED)
  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 && (rec->flags & FTRACE_FL_CONVERTED)) {
  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. }
  459. }
  460. }
  461. }
  462. static int
  463. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  464. {
  465. unsigned long ip;
  466. int ret;
  467. ip = rec->ip;
  468. ret = ftrace_make_nop(mod, rec, mcount_addr);
  469. if (ret) {
  470. ftrace_bug(ret, ip);
  471. rec->flags |= FTRACE_FL_FAILED;
  472. return 0;
  473. }
  474. return 1;
  475. }
  476. static int __ftrace_modify_code(void *data)
  477. {
  478. int *command = data;
  479. if (*command & FTRACE_ENABLE_CALLS)
  480. ftrace_replace_code(1);
  481. else if (*command & FTRACE_DISABLE_CALLS)
  482. ftrace_replace_code(0);
  483. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  484. ftrace_update_ftrace_func(ftrace_trace_function);
  485. if (*command & FTRACE_START_FUNC_RET)
  486. ftrace_enable_ftrace_graph_caller();
  487. else if (*command & FTRACE_STOP_FUNC_RET)
  488. ftrace_disable_ftrace_graph_caller();
  489. return 0;
  490. }
  491. static void ftrace_run_update_code(int command)
  492. {
  493. stop_machine(__ftrace_modify_code, &command, NULL);
  494. }
  495. static ftrace_func_t saved_ftrace_func;
  496. static int ftrace_start_up;
  497. static void ftrace_startup_enable(int command)
  498. {
  499. if (saved_ftrace_func != ftrace_trace_function) {
  500. saved_ftrace_func = ftrace_trace_function;
  501. command |= FTRACE_UPDATE_TRACE_FUNC;
  502. }
  503. if (!command || !ftrace_enabled)
  504. return;
  505. ftrace_run_update_code(command);
  506. }
  507. static void ftrace_startup(int command)
  508. {
  509. if (unlikely(ftrace_disabled))
  510. return;
  511. mutex_lock(&ftrace_start_lock);
  512. ftrace_start_up++;
  513. command |= FTRACE_ENABLE_CALLS;
  514. ftrace_startup_enable(command);
  515. mutex_unlock(&ftrace_start_lock);
  516. }
  517. static void ftrace_shutdown(int command)
  518. {
  519. if (unlikely(ftrace_disabled))
  520. return;
  521. mutex_lock(&ftrace_start_lock);
  522. ftrace_start_up--;
  523. if (!ftrace_start_up)
  524. command |= FTRACE_DISABLE_CALLS;
  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. goto out;
  531. ftrace_run_update_code(command);
  532. out:
  533. mutex_unlock(&ftrace_start_lock);
  534. }
  535. static void ftrace_startup_sysctl(void)
  536. {
  537. int command = FTRACE_ENABLE_MCOUNT;
  538. if (unlikely(ftrace_disabled))
  539. return;
  540. mutex_lock(&ftrace_start_lock);
  541. /* Force update next time */
  542. saved_ftrace_func = NULL;
  543. /* ftrace_start_up is true if we want ftrace running */
  544. if (ftrace_start_up)
  545. command |= FTRACE_ENABLE_CALLS;
  546. ftrace_run_update_code(command);
  547. mutex_unlock(&ftrace_start_lock);
  548. }
  549. static void ftrace_shutdown_sysctl(void)
  550. {
  551. int command = FTRACE_DISABLE_MCOUNT;
  552. if (unlikely(ftrace_disabled))
  553. return;
  554. mutex_lock(&ftrace_start_lock);
  555. /* ftrace_start_up is true if ftrace is running */
  556. if (ftrace_start_up)
  557. command |= FTRACE_DISABLE_CALLS;
  558. ftrace_run_update_code(command);
  559. mutex_unlock(&ftrace_start_lock);
  560. }
  561. static cycle_t ftrace_update_time;
  562. static unsigned long ftrace_update_cnt;
  563. unsigned long ftrace_update_tot_cnt;
  564. static int ftrace_update_code(struct module *mod)
  565. {
  566. struct dyn_ftrace *p, *t;
  567. cycle_t start, stop;
  568. start = ftrace_now(raw_smp_processor_id());
  569. ftrace_update_cnt = 0;
  570. list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
  571. /* If something went wrong, bail without enabling anything */
  572. if (unlikely(ftrace_disabled))
  573. return -1;
  574. list_del_init(&p->list);
  575. /* convert record (i.e, patch mcount-call with NOP) */
  576. if (ftrace_code_disable(mod, p)) {
  577. p->flags |= FTRACE_FL_CONVERTED;
  578. ftrace_update_cnt++;
  579. } else
  580. ftrace_free_rec(p);
  581. }
  582. stop = ftrace_now(raw_smp_processor_id());
  583. ftrace_update_time = stop - start;
  584. ftrace_update_tot_cnt += ftrace_update_cnt;
  585. return 0;
  586. }
  587. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  588. {
  589. struct ftrace_page *pg;
  590. int cnt;
  591. int i;
  592. /* allocate a few pages */
  593. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  594. if (!ftrace_pages_start)
  595. return -1;
  596. /*
  597. * Allocate a few more pages.
  598. *
  599. * TODO: have some parser search vmlinux before
  600. * final linking to find all calls to ftrace.
  601. * Then we can:
  602. * a) know how many pages to allocate.
  603. * and/or
  604. * b) set up the table then.
  605. *
  606. * The dynamic code is still necessary for
  607. * modules.
  608. */
  609. pg = ftrace_pages = ftrace_pages_start;
  610. cnt = num_to_init / ENTRIES_PER_PAGE;
  611. pr_info("ftrace: allocating %ld entries in %d pages\n",
  612. num_to_init, cnt + 1);
  613. for (i = 0; i < cnt; i++) {
  614. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  615. /* If we fail, we'll try later anyway */
  616. if (!pg->next)
  617. break;
  618. pg = pg->next;
  619. }
  620. return 0;
  621. }
  622. enum {
  623. FTRACE_ITER_FILTER = (1 << 0),
  624. FTRACE_ITER_CONT = (1 << 1),
  625. FTRACE_ITER_NOTRACE = (1 << 2),
  626. FTRACE_ITER_FAILURES = (1 << 3),
  627. };
  628. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  629. struct ftrace_iterator {
  630. struct ftrace_page *pg;
  631. unsigned idx;
  632. unsigned flags;
  633. unsigned char buffer[FTRACE_BUFF_MAX+1];
  634. unsigned buffer_idx;
  635. unsigned filtered;
  636. };
  637. static void *
  638. t_next(struct seq_file *m, void *v, loff_t *pos)
  639. {
  640. struct ftrace_iterator *iter = m->private;
  641. struct dyn_ftrace *rec = NULL;
  642. (*pos)++;
  643. /* should not be called from interrupt context */
  644. spin_lock(&ftrace_lock);
  645. retry:
  646. if (iter->idx >= iter->pg->index) {
  647. if (iter->pg->next) {
  648. iter->pg = iter->pg->next;
  649. iter->idx = 0;
  650. goto retry;
  651. } else {
  652. iter->idx = -1;
  653. }
  654. } else {
  655. rec = &iter->pg->records[iter->idx++];
  656. if ((rec->flags & FTRACE_FL_FREE) ||
  657. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  658. (rec->flags & FTRACE_FL_FAILED)) ||
  659. ((iter->flags & FTRACE_ITER_FAILURES) &&
  660. !(rec->flags & FTRACE_FL_FAILED)) ||
  661. ((iter->flags & FTRACE_ITER_FILTER) &&
  662. !(rec->flags & FTRACE_FL_FILTER)) ||
  663. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  664. !(rec->flags & FTRACE_FL_NOTRACE))) {
  665. rec = NULL;
  666. goto retry;
  667. }
  668. }
  669. spin_unlock(&ftrace_lock);
  670. return rec;
  671. }
  672. static void *t_start(struct seq_file *m, loff_t *pos)
  673. {
  674. struct ftrace_iterator *iter = m->private;
  675. void *p = NULL;
  676. if (*pos > 0) {
  677. if (iter->idx < 0)
  678. return p;
  679. (*pos)--;
  680. iter->idx--;
  681. }
  682. p = t_next(m, p, pos);
  683. return p;
  684. }
  685. static void t_stop(struct seq_file *m, void *p)
  686. {
  687. }
  688. static int t_show(struct seq_file *m, void *v)
  689. {
  690. struct dyn_ftrace *rec = v;
  691. char str[KSYM_SYMBOL_LEN];
  692. if (!rec)
  693. return 0;
  694. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  695. seq_printf(m, "%s\n", str);
  696. return 0;
  697. }
  698. static struct seq_operations show_ftrace_seq_ops = {
  699. .start = t_start,
  700. .next = t_next,
  701. .stop = t_stop,
  702. .show = t_show,
  703. };
  704. static int
  705. ftrace_avail_open(struct inode *inode, struct file *file)
  706. {
  707. struct ftrace_iterator *iter;
  708. int ret;
  709. if (unlikely(ftrace_disabled))
  710. return -ENODEV;
  711. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  712. if (!iter)
  713. return -ENOMEM;
  714. iter->pg = ftrace_pages_start;
  715. ret = seq_open(file, &show_ftrace_seq_ops);
  716. if (!ret) {
  717. struct seq_file *m = file->private_data;
  718. m->private = iter;
  719. } else {
  720. kfree(iter);
  721. }
  722. return ret;
  723. }
  724. int ftrace_avail_release(struct inode *inode, struct file *file)
  725. {
  726. struct seq_file *m = (struct seq_file *)file->private_data;
  727. struct ftrace_iterator *iter = m->private;
  728. seq_release(inode, file);
  729. kfree(iter);
  730. return 0;
  731. }
  732. static int
  733. ftrace_failures_open(struct inode *inode, struct file *file)
  734. {
  735. int ret;
  736. struct seq_file *m;
  737. struct ftrace_iterator *iter;
  738. ret = ftrace_avail_open(inode, file);
  739. if (!ret) {
  740. m = (struct seq_file *)file->private_data;
  741. iter = (struct ftrace_iterator *)m->private;
  742. iter->flags = FTRACE_ITER_FAILURES;
  743. }
  744. return ret;
  745. }
  746. static void ftrace_filter_reset(int enable)
  747. {
  748. struct ftrace_page *pg;
  749. struct dyn_ftrace *rec;
  750. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  751. unsigned i;
  752. /* should not be called from interrupt context */
  753. spin_lock(&ftrace_lock);
  754. if (enable)
  755. ftrace_filtered = 0;
  756. pg = ftrace_pages_start;
  757. while (pg) {
  758. for (i = 0; i < pg->index; i++) {
  759. rec = &pg->records[i];
  760. if (rec->flags & FTRACE_FL_FAILED)
  761. continue;
  762. rec->flags &= ~type;
  763. }
  764. pg = pg->next;
  765. }
  766. spin_unlock(&ftrace_lock);
  767. }
  768. static int
  769. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  770. {
  771. struct ftrace_iterator *iter;
  772. int ret = 0;
  773. if (unlikely(ftrace_disabled))
  774. return -ENODEV;
  775. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  776. if (!iter)
  777. return -ENOMEM;
  778. mutex_lock(&ftrace_regex_lock);
  779. if ((file->f_mode & FMODE_WRITE) &&
  780. !(file->f_flags & O_APPEND))
  781. ftrace_filter_reset(enable);
  782. if (file->f_mode & FMODE_READ) {
  783. iter->pg = ftrace_pages_start;
  784. iter->flags = enable ? FTRACE_ITER_FILTER :
  785. FTRACE_ITER_NOTRACE;
  786. ret = seq_open(file, &show_ftrace_seq_ops);
  787. if (!ret) {
  788. struct seq_file *m = file->private_data;
  789. m->private = iter;
  790. } else
  791. kfree(iter);
  792. } else
  793. file->private_data = iter;
  794. mutex_unlock(&ftrace_regex_lock);
  795. return ret;
  796. }
  797. static int
  798. ftrace_filter_open(struct inode *inode, struct file *file)
  799. {
  800. return ftrace_regex_open(inode, file, 1);
  801. }
  802. static int
  803. ftrace_notrace_open(struct inode *inode, struct file *file)
  804. {
  805. return ftrace_regex_open(inode, file, 0);
  806. }
  807. static ssize_t
  808. ftrace_regex_read(struct file *file, char __user *ubuf,
  809. size_t cnt, loff_t *ppos)
  810. {
  811. if (file->f_mode & FMODE_READ)
  812. return seq_read(file, ubuf, cnt, ppos);
  813. else
  814. return -EPERM;
  815. }
  816. static loff_t
  817. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  818. {
  819. loff_t ret;
  820. if (file->f_mode & FMODE_READ)
  821. ret = seq_lseek(file, offset, origin);
  822. else
  823. file->f_pos = ret = 1;
  824. return ret;
  825. }
  826. enum {
  827. MATCH_FULL,
  828. MATCH_FRONT_ONLY,
  829. MATCH_MIDDLE_ONLY,
  830. MATCH_END_ONLY,
  831. };
  832. static void
  833. ftrace_match(unsigned char *buff, int len, int enable)
  834. {
  835. char str[KSYM_SYMBOL_LEN];
  836. char *search = NULL;
  837. struct ftrace_page *pg;
  838. struct dyn_ftrace *rec;
  839. int type = MATCH_FULL;
  840. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  841. unsigned i, match = 0, search_len = 0;
  842. for (i = 0; i < len; i++) {
  843. if (buff[i] == '*') {
  844. if (!i) {
  845. search = buff + i + 1;
  846. type = MATCH_END_ONLY;
  847. search_len = len - (i + 1);
  848. } else {
  849. if (type == MATCH_END_ONLY) {
  850. type = MATCH_MIDDLE_ONLY;
  851. } else {
  852. match = i;
  853. type = MATCH_FRONT_ONLY;
  854. }
  855. buff[i] = 0;
  856. break;
  857. }
  858. }
  859. }
  860. /* should not be called from interrupt context */
  861. spin_lock(&ftrace_lock);
  862. if (enable)
  863. ftrace_filtered = 1;
  864. pg = ftrace_pages_start;
  865. while (pg) {
  866. for (i = 0; i < pg->index; i++) {
  867. int matched = 0;
  868. char *ptr;
  869. rec = &pg->records[i];
  870. if (rec->flags & FTRACE_FL_FAILED)
  871. continue;
  872. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  873. switch (type) {
  874. case MATCH_FULL:
  875. if (strcmp(str, buff) == 0)
  876. matched = 1;
  877. break;
  878. case MATCH_FRONT_ONLY:
  879. if (memcmp(str, buff, match) == 0)
  880. matched = 1;
  881. break;
  882. case MATCH_MIDDLE_ONLY:
  883. if (strstr(str, search))
  884. matched = 1;
  885. break;
  886. case MATCH_END_ONLY:
  887. ptr = strstr(str, search);
  888. if (ptr && (ptr[search_len] == 0))
  889. matched = 1;
  890. break;
  891. }
  892. if (matched)
  893. rec->flags |= flag;
  894. }
  895. pg = pg->next;
  896. }
  897. spin_unlock(&ftrace_lock);
  898. }
  899. static ssize_t
  900. ftrace_regex_write(struct file *file, const char __user *ubuf,
  901. size_t cnt, loff_t *ppos, int enable)
  902. {
  903. struct ftrace_iterator *iter;
  904. char ch;
  905. size_t read = 0;
  906. ssize_t ret;
  907. if (!cnt || cnt < 0)
  908. return 0;
  909. mutex_lock(&ftrace_regex_lock);
  910. if (file->f_mode & FMODE_READ) {
  911. struct seq_file *m = file->private_data;
  912. iter = m->private;
  913. } else
  914. iter = file->private_data;
  915. if (!*ppos) {
  916. iter->flags &= ~FTRACE_ITER_CONT;
  917. iter->buffer_idx = 0;
  918. }
  919. ret = get_user(ch, ubuf++);
  920. if (ret)
  921. goto out;
  922. read++;
  923. cnt--;
  924. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  925. /* skip white space */
  926. while (cnt && isspace(ch)) {
  927. ret = get_user(ch, ubuf++);
  928. if (ret)
  929. goto out;
  930. read++;
  931. cnt--;
  932. }
  933. if (isspace(ch)) {
  934. file->f_pos += read;
  935. ret = read;
  936. goto out;
  937. }
  938. iter->buffer_idx = 0;
  939. }
  940. while (cnt && !isspace(ch)) {
  941. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  942. iter->buffer[iter->buffer_idx++] = ch;
  943. else {
  944. ret = -EINVAL;
  945. goto out;
  946. }
  947. ret = get_user(ch, ubuf++);
  948. if (ret)
  949. goto out;
  950. read++;
  951. cnt--;
  952. }
  953. if (isspace(ch)) {
  954. iter->filtered++;
  955. iter->buffer[iter->buffer_idx] = 0;
  956. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  957. iter->buffer_idx = 0;
  958. } else
  959. iter->flags |= FTRACE_ITER_CONT;
  960. file->f_pos += read;
  961. ret = read;
  962. out:
  963. mutex_unlock(&ftrace_regex_lock);
  964. return ret;
  965. }
  966. static ssize_t
  967. ftrace_filter_write(struct file *file, const char __user *ubuf,
  968. size_t cnt, loff_t *ppos)
  969. {
  970. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  971. }
  972. static ssize_t
  973. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  974. size_t cnt, loff_t *ppos)
  975. {
  976. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  977. }
  978. static void
  979. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  980. {
  981. if (unlikely(ftrace_disabled))
  982. return;
  983. mutex_lock(&ftrace_regex_lock);
  984. if (reset)
  985. ftrace_filter_reset(enable);
  986. if (buf)
  987. ftrace_match(buf, len, enable);
  988. mutex_unlock(&ftrace_regex_lock);
  989. }
  990. /**
  991. * ftrace_set_filter - set a function to filter on in ftrace
  992. * @buf - the string that holds the function filter text.
  993. * @len - the length of the string.
  994. * @reset - non zero to reset all filters before applying this filter.
  995. *
  996. * Filters denote which functions should be enabled when tracing is enabled.
  997. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  998. */
  999. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1000. {
  1001. ftrace_set_regex(buf, len, reset, 1);
  1002. }
  1003. /**
  1004. * ftrace_set_notrace - set a function to not trace in ftrace
  1005. * @buf - the string that holds the function notrace text.
  1006. * @len - the length of the string.
  1007. * @reset - non zero to reset all filters before applying this filter.
  1008. *
  1009. * Notrace Filters denote which functions should not be enabled when tracing
  1010. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1011. * for tracing.
  1012. */
  1013. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1014. {
  1015. ftrace_set_regex(buf, len, reset, 0);
  1016. }
  1017. static int
  1018. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1019. {
  1020. struct seq_file *m = (struct seq_file *)file->private_data;
  1021. struct ftrace_iterator *iter;
  1022. mutex_lock(&ftrace_regex_lock);
  1023. if (file->f_mode & FMODE_READ) {
  1024. iter = m->private;
  1025. seq_release(inode, file);
  1026. } else
  1027. iter = file->private_data;
  1028. if (iter->buffer_idx) {
  1029. iter->filtered++;
  1030. iter->buffer[iter->buffer_idx] = 0;
  1031. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1032. }
  1033. mutex_lock(&ftrace_sysctl_lock);
  1034. mutex_lock(&ftrace_start_lock);
  1035. if (ftrace_start_up && ftrace_enabled)
  1036. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1037. mutex_unlock(&ftrace_start_lock);
  1038. mutex_unlock(&ftrace_sysctl_lock);
  1039. kfree(iter);
  1040. mutex_unlock(&ftrace_regex_lock);
  1041. return 0;
  1042. }
  1043. static int
  1044. ftrace_filter_release(struct inode *inode, struct file *file)
  1045. {
  1046. return ftrace_regex_release(inode, file, 1);
  1047. }
  1048. static int
  1049. ftrace_notrace_release(struct inode *inode, struct file *file)
  1050. {
  1051. return ftrace_regex_release(inode, file, 0);
  1052. }
  1053. static struct file_operations ftrace_avail_fops = {
  1054. .open = ftrace_avail_open,
  1055. .read = seq_read,
  1056. .llseek = seq_lseek,
  1057. .release = ftrace_avail_release,
  1058. };
  1059. static struct file_operations ftrace_failures_fops = {
  1060. .open = ftrace_failures_open,
  1061. .read = seq_read,
  1062. .llseek = seq_lseek,
  1063. .release = ftrace_avail_release,
  1064. };
  1065. static struct file_operations ftrace_filter_fops = {
  1066. .open = ftrace_filter_open,
  1067. .read = ftrace_regex_read,
  1068. .write = ftrace_filter_write,
  1069. .llseek = ftrace_regex_lseek,
  1070. .release = ftrace_filter_release,
  1071. };
  1072. static struct file_operations ftrace_notrace_fops = {
  1073. .open = ftrace_notrace_open,
  1074. .read = ftrace_regex_read,
  1075. .write = ftrace_notrace_write,
  1076. .llseek = ftrace_regex_lseek,
  1077. .release = ftrace_notrace_release,
  1078. };
  1079. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1080. static DEFINE_MUTEX(graph_lock);
  1081. int ftrace_graph_count;
  1082. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1083. static void *
  1084. g_next(struct seq_file *m, void *v, loff_t *pos)
  1085. {
  1086. unsigned long *array = m->private;
  1087. int index = *pos;
  1088. (*pos)++;
  1089. if (index >= ftrace_graph_count)
  1090. return NULL;
  1091. return &array[index];
  1092. }
  1093. static void *g_start(struct seq_file *m, loff_t *pos)
  1094. {
  1095. void *p = NULL;
  1096. mutex_lock(&graph_lock);
  1097. p = g_next(m, p, pos);
  1098. return p;
  1099. }
  1100. static void g_stop(struct seq_file *m, void *p)
  1101. {
  1102. mutex_unlock(&graph_lock);
  1103. }
  1104. static int g_show(struct seq_file *m, void *v)
  1105. {
  1106. unsigned long *ptr = v;
  1107. char str[KSYM_SYMBOL_LEN];
  1108. if (!ptr)
  1109. return 0;
  1110. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1111. seq_printf(m, "%s\n", str);
  1112. return 0;
  1113. }
  1114. static struct seq_operations ftrace_graph_seq_ops = {
  1115. .start = g_start,
  1116. .next = g_next,
  1117. .stop = g_stop,
  1118. .show = g_show,
  1119. };
  1120. static int
  1121. ftrace_graph_open(struct inode *inode, struct file *file)
  1122. {
  1123. int ret = 0;
  1124. if (unlikely(ftrace_disabled))
  1125. return -ENODEV;
  1126. mutex_lock(&graph_lock);
  1127. if ((file->f_mode & FMODE_WRITE) &&
  1128. !(file->f_flags & O_APPEND)) {
  1129. ftrace_graph_count = 0;
  1130. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1131. }
  1132. if (file->f_mode & FMODE_READ) {
  1133. ret = seq_open(file, &ftrace_graph_seq_ops);
  1134. if (!ret) {
  1135. struct seq_file *m = file->private_data;
  1136. m->private = ftrace_graph_funcs;
  1137. }
  1138. } else
  1139. file->private_data = ftrace_graph_funcs;
  1140. mutex_unlock(&graph_lock);
  1141. return ret;
  1142. }
  1143. static ssize_t
  1144. ftrace_graph_read(struct file *file, char __user *ubuf,
  1145. size_t cnt, loff_t *ppos)
  1146. {
  1147. if (file->f_mode & FMODE_READ)
  1148. return seq_read(file, ubuf, cnt, ppos);
  1149. else
  1150. return -EPERM;
  1151. }
  1152. static int
  1153. ftrace_set_func(unsigned long *array, int idx, char *buffer)
  1154. {
  1155. char str[KSYM_SYMBOL_LEN];
  1156. struct dyn_ftrace *rec;
  1157. struct ftrace_page *pg;
  1158. int found = 0;
  1159. int i;
  1160. if (ftrace_disabled)
  1161. return -ENODEV;
  1162. /* should not be called from interrupt context */
  1163. spin_lock(&ftrace_lock);
  1164. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1165. for (i = 0; i < pg->index; i++) {
  1166. rec = &pg->records[i];
  1167. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1168. continue;
  1169. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1170. if (strcmp(str, buffer) == 0) {
  1171. found = 1;
  1172. array[idx] = rec->ip;
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. spin_unlock(&ftrace_lock);
  1178. return found ? 0 : -EINVAL;
  1179. }
  1180. static ssize_t
  1181. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1182. size_t cnt, loff_t *ppos)
  1183. {
  1184. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1185. unsigned long *array;
  1186. size_t read = 0;
  1187. ssize_t ret;
  1188. int index = 0;
  1189. char ch;
  1190. if (!cnt || cnt < 0)
  1191. return 0;
  1192. mutex_lock(&graph_lock);
  1193. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1194. ret = -EBUSY;
  1195. goto out;
  1196. }
  1197. if (file->f_mode & FMODE_READ) {
  1198. struct seq_file *m = file->private_data;
  1199. array = m->private;
  1200. } else
  1201. array = file->private_data;
  1202. ret = get_user(ch, ubuf++);
  1203. if (ret)
  1204. goto out;
  1205. read++;
  1206. cnt--;
  1207. /* skip white space */
  1208. while (cnt && isspace(ch)) {
  1209. ret = get_user(ch, ubuf++);
  1210. if (ret)
  1211. goto out;
  1212. read++;
  1213. cnt--;
  1214. }
  1215. if (isspace(ch)) {
  1216. *ppos += read;
  1217. ret = read;
  1218. goto out;
  1219. }
  1220. while (cnt && !isspace(ch)) {
  1221. if (index < FTRACE_BUFF_MAX)
  1222. buffer[index++] = ch;
  1223. else {
  1224. ret = -EINVAL;
  1225. goto out;
  1226. }
  1227. ret = get_user(ch, ubuf++);
  1228. if (ret)
  1229. goto out;
  1230. read++;
  1231. cnt--;
  1232. }
  1233. buffer[index] = 0;
  1234. /* we allow only one at a time */
  1235. ret = ftrace_set_func(array, ftrace_graph_count, buffer);
  1236. if (ret)
  1237. goto out;
  1238. ftrace_graph_count++;
  1239. file->f_pos += read;
  1240. ret = read;
  1241. out:
  1242. mutex_unlock(&graph_lock);
  1243. return ret;
  1244. }
  1245. static const struct file_operations ftrace_graph_fops = {
  1246. .open = ftrace_graph_open,
  1247. .read = ftrace_graph_read,
  1248. .write = ftrace_graph_write,
  1249. };
  1250. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1251. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1252. {
  1253. struct dentry *entry;
  1254. entry = debugfs_create_file("available_filter_functions", 0444,
  1255. d_tracer, NULL, &ftrace_avail_fops);
  1256. if (!entry)
  1257. pr_warning("Could not create debugfs "
  1258. "'available_filter_functions' entry\n");
  1259. entry = debugfs_create_file("failures", 0444,
  1260. d_tracer, NULL, &ftrace_failures_fops);
  1261. if (!entry)
  1262. pr_warning("Could not create debugfs 'failures' entry\n");
  1263. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1264. NULL, &ftrace_filter_fops);
  1265. if (!entry)
  1266. pr_warning("Could not create debugfs "
  1267. "'set_ftrace_filter' entry\n");
  1268. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1269. NULL, &ftrace_notrace_fops);
  1270. if (!entry)
  1271. pr_warning("Could not create debugfs "
  1272. "'set_ftrace_notrace' entry\n");
  1273. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1274. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  1275. NULL,
  1276. &ftrace_graph_fops);
  1277. if (!entry)
  1278. pr_warning("Could not create debugfs "
  1279. "'set_graph_function' entry\n");
  1280. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1281. return 0;
  1282. }
  1283. static int ftrace_convert_nops(struct module *mod,
  1284. unsigned long *start,
  1285. unsigned long *end)
  1286. {
  1287. unsigned long *p;
  1288. unsigned long addr;
  1289. unsigned long flags;
  1290. mutex_lock(&ftrace_start_lock);
  1291. p = start;
  1292. while (p < end) {
  1293. addr = ftrace_call_adjust(*p++);
  1294. /*
  1295. * Some architecture linkers will pad between
  1296. * the different mcount_loc sections of different
  1297. * object files to satisfy alignments.
  1298. * Skip any NULL pointers.
  1299. */
  1300. if (!addr)
  1301. continue;
  1302. ftrace_record_ip(addr);
  1303. }
  1304. /* disable interrupts to prevent kstop machine */
  1305. local_irq_save(flags);
  1306. ftrace_update_code(mod);
  1307. local_irq_restore(flags);
  1308. mutex_unlock(&ftrace_start_lock);
  1309. return 0;
  1310. }
  1311. void ftrace_init_module(struct module *mod,
  1312. unsigned long *start, unsigned long *end)
  1313. {
  1314. if (ftrace_disabled || start == end)
  1315. return;
  1316. ftrace_convert_nops(mod, start, end);
  1317. }
  1318. extern unsigned long __start_mcount_loc[];
  1319. extern unsigned long __stop_mcount_loc[];
  1320. void __init ftrace_init(void)
  1321. {
  1322. unsigned long count, addr, flags;
  1323. int ret;
  1324. /* Keep the ftrace pointer to the stub */
  1325. addr = (unsigned long)ftrace_stub;
  1326. local_irq_save(flags);
  1327. ftrace_dyn_arch_init(&addr);
  1328. local_irq_restore(flags);
  1329. /* ftrace_dyn_arch_init places the return code in addr */
  1330. if (addr)
  1331. goto failed;
  1332. count = __stop_mcount_loc - __start_mcount_loc;
  1333. ret = ftrace_dyn_table_alloc(count);
  1334. if (ret)
  1335. goto failed;
  1336. last_ftrace_enabled = ftrace_enabled = 1;
  1337. ret = ftrace_convert_nops(NULL,
  1338. __start_mcount_loc,
  1339. __stop_mcount_loc);
  1340. return;
  1341. failed:
  1342. ftrace_disabled = 1;
  1343. }
  1344. #else
  1345. static int __init ftrace_nodyn_init(void)
  1346. {
  1347. ftrace_enabled = 1;
  1348. return 0;
  1349. }
  1350. device_initcall(ftrace_nodyn_init);
  1351. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1352. static inline void ftrace_startup_enable(int command) { }
  1353. /* Keep as macros so we do not need to define the commands */
  1354. # define ftrace_startup(command) do { } while (0)
  1355. # define ftrace_shutdown(command) do { } while (0)
  1356. # define ftrace_startup_sysctl() do { } while (0)
  1357. # define ftrace_shutdown_sysctl() do { } while (0)
  1358. #endif /* CONFIG_DYNAMIC_FTRACE */
  1359. static ssize_t
  1360. ftrace_pid_read(struct file *file, char __user *ubuf,
  1361. size_t cnt, loff_t *ppos)
  1362. {
  1363. char buf[64];
  1364. int r;
  1365. if (ftrace_pid_trace >= 0)
  1366. r = sprintf(buf, "%u\n", ftrace_pid_trace);
  1367. else
  1368. r = sprintf(buf, "no pid\n");
  1369. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1370. }
  1371. static ssize_t
  1372. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1373. size_t cnt, loff_t *ppos)
  1374. {
  1375. char buf[64];
  1376. long val;
  1377. int ret;
  1378. if (cnt >= sizeof(buf))
  1379. return -EINVAL;
  1380. if (copy_from_user(&buf, ubuf, cnt))
  1381. return -EFAULT;
  1382. buf[cnt] = 0;
  1383. ret = strict_strtol(buf, 10, &val);
  1384. if (ret < 0)
  1385. return ret;
  1386. mutex_lock(&ftrace_start_lock);
  1387. if (ret < 0) {
  1388. /* disable pid tracing */
  1389. if (ftrace_pid_trace < 0)
  1390. goto out;
  1391. ftrace_pid_trace = -1;
  1392. } else {
  1393. if (ftrace_pid_trace == val)
  1394. goto out;
  1395. ftrace_pid_trace = val;
  1396. }
  1397. /* update the function call */
  1398. ftrace_update_pid_func();
  1399. ftrace_startup_enable(0);
  1400. out:
  1401. mutex_unlock(&ftrace_start_lock);
  1402. return cnt;
  1403. }
  1404. static struct file_operations ftrace_pid_fops = {
  1405. .read = ftrace_pid_read,
  1406. .write = ftrace_pid_write,
  1407. };
  1408. static __init int ftrace_init_debugfs(void)
  1409. {
  1410. struct dentry *d_tracer;
  1411. struct dentry *entry;
  1412. d_tracer = tracing_init_dentry();
  1413. if (!d_tracer)
  1414. return 0;
  1415. ftrace_init_dyn_debugfs(d_tracer);
  1416. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1417. NULL, &ftrace_pid_fops);
  1418. if (!entry)
  1419. pr_warning("Could not create debugfs "
  1420. "'set_ftrace_pid' entry\n");
  1421. return 0;
  1422. }
  1423. fs_initcall(ftrace_init_debugfs);
  1424. /**
  1425. * ftrace_kill - kill ftrace
  1426. *
  1427. * This function should be used by panic code. It stops ftrace
  1428. * but in a not so nice way. If you need to simply kill ftrace
  1429. * from a non-atomic section, use ftrace_kill.
  1430. */
  1431. void ftrace_kill(void)
  1432. {
  1433. ftrace_disabled = 1;
  1434. ftrace_enabled = 0;
  1435. clear_ftrace_function();
  1436. }
  1437. /**
  1438. * register_ftrace_function - register a function for profiling
  1439. * @ops - ops structure that holds the function for profiling.
  1440. *
  1441. * Register a function to be called by all functions in the
  1442. * kernel.
  1443. *
  1444. * Note: @ops->func and all the functions it calls must be labeled
  1445. * with "notrace", otherwise it will go into a
  1446. * recursive loop.
  1447. */
  1448. int register_ftrace_function(struct ftrace_ops *ops)
  1449. {
  1450. int ret;
  1451. if (unlikely(ftrace_disabled))
  1452. return -1;
  1453. mutex_lock(&ftrace_sysctl_lock);
  1454. ret = __register_ftrace_function(ops);
  1455. ftrace_startup(0);
  1456. mutex_unlock(&ftrace_sysctl_lock);
  1457. return ret;
  1458. }
  1459. /**
  1460. * unregister_ftrace_function - unresgister a function for profiling.
  1461. * @ops - ops structure that holds the function to unregister
  1462. *
  1463. * Unregister a function that was added to be called by ftrace profiling.
  1464. */
  1465. int unregister_ftrace_function(struct ftrace_ops *ops)
  1466. {
  1467. int ret;
  1468. mutex_lock(&ftrace_sysctl_lock);
  1469. ret = __unregister_ftrace_function(ops);
  1470. ftrace_shutdown(0);
  1471. mutex_unlock(&ftrace_sysctl_lock);
  1472. return ret;
  1473. }
  1474. int
  1475. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1476. struct file *file, void __user *buffer, size_t *lenp,
  1477. loff_t *ppos)
  1478. {
  1479. int ret;
  1480. if (unlikely(ftrace_disabled))
  1481. return -ENODEV;
  1482. mutex_lock(&ftrace_sysctl_lock);
  1483. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1484. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1485. goto out;
  1486. last_ftrace_enabled = ftrace_enabled;
  1487. if (ftrace_enabled) {
  1488. ftrace_startup_sysctl();
  1489. /* we are starting ftrace again */
  1490. if (ftrace_list != &ftrace_list_end) {
  1491. if (ftrace_list->next == &ftrace_list_end)
  1492. ftrace_trace_function = ftrace_list->func;
  1493. else
  1494. ftrace_trace_function = ftrace_list_func;
  1495. }
  1496. } else {
  1497. /* stopping ftrace calls (just send to ftrace_stub) */
  1498. ftrace_trace_function = ftrace_stub;
  1499. ftrace_shutdown_sysctl();
  1500. }
  1501. out:
  1502. mutex_unlock(&ftrace_sysctl_lock);
  1503. return ret;
  1504. }
  1505. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1506. static atomic_t ftrace_graph_active;
  1507. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  1508. {
  1509. return 0;
  1510. }
  1511. /* The callbacks that hook a function */
  1512. trace_func_graph_ret_t ftrace_graph_return =
  1513. (trace_func_graph_ret_t)ftrace_stub;
  1514. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  1515. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  1516. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  1517. {
  1518. int i;
  1519. int ret = 0;
  1520. unsigned long flags;
  1521. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  1522. struct task_struct *g, *t;
  1523. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  1524. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  1525. * sizeof(struct ftrace_ret_stack),
  1526. GFP_KERNEL);
  1527. if (!ret_stack_list[i]) {
  1528. start = 0;
  1529. end = i;
  1530. ret = -ENOMEM;
  1531. goto free;
  1532. }
  1533. }
  1534. read_lock_irqsave(&tasklist_lock, flags);
  1535. do_each_thread(g, t) {
  1536. if (start == end) {
  1537. ret = -EAGAIN;
  1538. goto unlock;
  1539. }
  1540. if (t->ret_stack == NULL) {
  1541. t->curr_ret_stack = -1;
  1542. /* Make sure IRQs see the -1 first: */
  1543. barrier();
  1544. t->ret_stack = ret_stack_list[start++];
  1545. atomic_set(&t->trace_overrun, 0);
  1546. }
  1547. } while_each_thread(g, t);
  1548. unlock:
  1549. read_unlock_irqrestore(&tasklist_lock, flags);
  1550. free:
  1551. for (i = start; i < end; i++)
  1552. kfree(ret_stack_list[i]);
  1553. return ret;
  1554. }
  1555. /* Allocate a return stack for each task */
  1556. static int start_graph_tracing(void)
  1557. {
  1558. struct ftrace_ret_stack **ret_stack_list;
  1559. int ret;
  1560. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  1561. sizeof(struct ftrace_ret_stack *),
  1562. GFP_KERNEL);
  1563. if (!ret_stack_list)
  1564. return -ENOMEM;
  1565. do {
  1566. ret = alloc_retstack_tasklist(ret_stack_list);
  1567. } while (ret == -EAGAIN);
  1568. kfree(ret_stack_list);
  1569. return ret;
  1570. }
  1571. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  1572. trace_func_graph_ent_t entryfunc)
  1573. {
  1574. int ret = 0;
  1575. mutex_lock(&ftrace_sysctl_lock);
  1576. atomic_inc(&ftrace_graph_active);
  1577. ret = start_graph_tracing();
  1578. if (ret) {
  1579. atomic_dec(&ftrace_graph_active);
  1580. goto out;
  1581. }
  1582. ftrace_graph_return = retfunc;
  1583. ftrace_graph_entry = entryfunc;
  1584. ftrace_startup(FTRACE_START_FUNC_RET);
  1585. out:
  1586. mutex_unlock(&ftrace_sysctl_lock);
  1587. return ret;
  1588. }
  1589. void unregister_ftrace_graph(void)
  1590. {
  1591. mutex_lock(&ftrace_sysctl_lock);
  1592. atomic_dec(&ftrace_graph_active);
  1593. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  1594. ftrace_graph_entry = ftrace_graph_entry_stub;
  1595. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  1596. mutex_unlock(&ftrace_sysctl_lock);
  1597. }
  1598. /* Allocate a return stack for newly created task */
  1599. void ftrace_graph_init_task(struct task_struct *t)
  1600. {
  1601. if (atomic_read(&ftrace_graph_active)) {
  1602. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  1603. * sizeof(struct ftrace_ret_stack),
  1604. GFP_KERNEL);
  1605. if (!t->ret_stack)
  1606. return;
  1607. t->curr_ret_stack = -1;
  1608. atomic_set(&t->trace_overrun, 0);
  1609. } else
  1610. t->ret_stack = NULL;
  1611. }
  1612. void ftrace_graph_exit_task(struct task_struct *t)
  1613. {
  1614. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  1615. t->ret_stack = NULL;
  1616. /* NULL must become visible to IRQs before we free it: */
  1617. barrier();
  1618. kfree(ret_stack);
  1619. }
  1620. void ftrace_graph_stop(void)
  1621. {
  1622. ftrace_stop();
  1623. }
  1624. #endif