trace_functions_graph.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. *
  3. * Function graph tracer.
  4. * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
  5. * Mostly borrowed from function tracer which
  6. * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
  7. *
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include "trace.h"
  15. #include "trace_output.h"
  16. static bool kill_ftrace_graph;
  17. /**
  18. * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
  19. *
  20. * ftrace_graph_stop() is called when a severe error is detected in
  21. * the function graph tracing. This function is called by the critical
  22. * paths of function graph to keep those paths from doing any more harm.
  23. */
  24. bool ftrace_graph_is_dead(void)
  25. {
  26. return kill_ftrace_graph;
  27. }
  28. /**
  29. * ftrace_graph_stop - set to permanently disable function graph tracincg
  30. *
  31. * In case of an error int function graph tracing, this is called
  32. * to try to keep function graph tracing from causing any more harm.
  33. * Usually this is pretty severe and this is called to try to at least
  34. * get a warning out to the user.
  35. */
  36. void ftrace_graph_stop(void)
  37. {
  38. kill_ftrace_graph = true;
  39. }
  40. /* When set, irq functions will be ignored */
  41. static int ftrace_graph_skip_irqs;
  42. struct fgraph_cpu_data {
  43. pid_t last_pid;
  44. int depth;
  45. int depth_irq;
  46. int ignore;
  47. unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
  48. };
  49. struct fgraph_data {
  50. struct fgraph_cpu_data __percpu *cpu_data;
  51. /* Place to preserve last processed entry. */
  52. struct ftrace_graph_ent_entry ent;
  53. struct ftrace_graph_ret_entry ret;
  54. int failed;
  55. int cpu;
  56. };
  57. #define TRACE_GRAPH_INDENT 2
  58. static unsigned int max_depth;
  59. static struct tracer_opt trace_opts[] = {
  60. /* Display overruns? (for self-debug purpose) */
  61. { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
  62. /* Display CPU ? */
  63. { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
  64. /* Display Overhead ? */
  65. { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
  66. /* Display proc name/pid */
  67. { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
  68. /* Display duration of execution */
  69. { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
  70. /* Display absolute time of an entry */
  71. { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
  72. /* Display interrupts */
  73. { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
  74. /* Display function name after trailing } */
  75. { TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
  76. { } /* Empty entry */
  77. };
  78. static struct tracer_flags tracer_flags = {
  79. /* Don't display overruns, proc, or tail by default */
  80. .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
  81. TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS,
  82. .opts = trace_opts
  83. };
  84. static struct trace_array *graph_array;
  85. /*
  86. * DURATION column is being also used to display IRQ signs,
  87. * following values are used by print_graph_irq and others
  88. * to fill in space into DURATION column.
  89. */
  90. enum {
  91. FLAGS_FILL_FULL = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  92. FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  93. FLAGS_FILL_END = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  94. };
  95. static void
  96. print_graph_duration(unsigned long long duration, struct trace_seq *s,
  97. u32 flags);
  98. /* Add a function return address to the trace stack on thread info.*/
  99. int
  100. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  101. unsigned long frame_pointer)
  102. {
  103. unsigned long long calltime;
  104. int index;
  105. if (unlikely(ftrace_graph_is_dead()))
  106. return -EBUSY;
  107. if (!current->ret_stack)
  108. return -EBUSY;
  109. /*
  110. * We must make sure the ret_stack is tested before we read
  111. * anything else.
  112. */
  113. smp_rmb();
  114. /* The return trace stack is full */
  115. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  116. atomic_inc(&current->trace_overrun);
  117. return -EBUSY;
  118. }
  119. /*
  120. * The curr_ret_stack is an index to ftrace return stack of
  121. * current task. Its value should be in [0, FTRACE_RETFUNC_
  122. * DEPTH) when the function graph tracer is used. To support
  123. * filtering out specific functions, it makes the index
  124. * negative by subtracting huge value (FTRACE_NOTRACE_DEPTH)
  125. * so when it sees a negative index the ftrace will ignore
  126. * the record. And the index gets recovered when returning
  127. * from the filtered function by adding the FTRACE_NOTRACE_
  128. * DEPTH and then it'll continue to record functions normally.
  129. *
  130. * The curr_ret_stack is initialized to -1 and get increased
  131. * in this function. So it can be less than -1 only if it was
  132. * filtered out via ftrace_graph_notrace_addr() which can be
  133. * set from set_graph_notrace file in debugfs by user.
  134. */
  135. if (current->curr_ret_stack < -1)
  136. return -EBUSY;
  137. calltime = trace_clock_local();
  138. index = ++current->curr_ret_stack;
  139. if (ftrace_graph_notrace_addr(func))
  140. current->curr_ret_stack -= FTRACE_NOTRACE_DEPTH;
  141. barrier();
  142. current->ret_stack[index].ret = ret;
  143. current->ret_stack[index].func = func;
  144. current->ret_stack[index].calltime = calltime;
  145. current->ret_stack[index].subtime = 0;
  146. current->ret_stack[index].fp = frame_pointer;
  147. *depth = current->curr_ret_stack;
  148. return 0;
  149. }
  150. /* Retrieve a function return address to the trace stack on thread info.*/
  151. static void
  152. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
  153. unsigned long frame_pointer)
  154. {
  155. int index;
  156. index = current->curr_ret_stack;
  157. /*
  158. * A negative index here means that it's just returned from a
  159. * notrace'd function. Recover index to get an original
  160. * return address. See ftrace_push_return_trace().
  161. *
  162. * TODO: Need to check whether the stack gets corrupted.
  163. */
  164. if (index < 0)
  165. index += FTRACE_NOTRACE_DEPTH;
  166. if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
  167. ftrace_graph_stop();
  168. WARN_ON(1);
  169. /* Might as well panic, otherwise we have no where to go */
  170. *ret = (unsigned long)panic;
  171. return;
  172. }
  173. #if defined(CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST) && !defined(CC_USING_FENTRY)
  174. /*
  175. * The arch may choose to record the frame pointer used
  176. * and check it here to make sure that it is what we expect it
  177. * to be. If gcc does not set the place holder of the return
  178. * address in the frame pointer, and does a copy instead, then
  179. * the function graph trace will fail. This test detects this
  180. * case.
  181. *
  182. * Currently, x86_32 with optimize for size (-Os) makes the latest
  183. * gcc do the above.
  184. *
  185. * Note, -mfentry does not use frame pointers, and this test
  186. * is not needed if CC_USING_FENTRY is set.
  187. */
  188. if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
  189. ftrace_graph_stop();
  190. WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
  191. " from func %ps return to %lx\n",
  192. current->ret_stack[index].fp,
  193. frame_pointer,
  194. (void *)current->ret_stack[index].func,
  195. current->ret_stack[index].ret);
  196. *ret = (unsigned long)panic;
  197. return;
  198. }
  199. #endif
  200. *ret = current->ret_stack[index].ret;
  201. trace->func = current->ret_stack[index].func;
  202. trace->calltime = current->ret_stack[index].calltime;
  203. trace->overrun = atomic_read(&current->trace_overrun);
  204. trace->depth = index;
  205. }
  206. /*
  207. * Send the trace to the ring-buffer.
  208. * @return the original return address.
  209. */
  210. unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
  211. {
  212. struct ftrace_graph_ret trace;
  213. unsigned long ret;
  214. ftrace_pop_return_trace(&trace, &ret, frame_pointer);
  215. trace.rettime = trace_clock_local();
  216. barrier();
  217. current->curr_ret_stack--;
  218. /*
  219. * The curr_ret_stack can be less than -1 only if it was
  220. * filtered out and it's about to return from the function.
  221. * Recover the index and continue to trace normal functions.
  222. */
  223. if (current->curr_ret_stack < -1) {
  224. current->curr_ret_stack += FTRACE_NOTRACE_DEPTH;
  225. return ret;
  226. }
  227. /*
  228. * The trace should run after decrementing the ret counter
  229. * in case an interrupt were to come in. We don't want to
  230. * lose the interrupt if max_depth is set.
  231. */
  232. ftrace_graph_return(&trace);
  233. if (unlikely(!ret)) {
  234. ftrace_graph_stop();
  235. WARN_ON(1);
  236. /* Might as well panic. What else to do? */
  237. ret = (unsigned long)panic;
  238. }
  239. return ret;
  240. }
  241. int __trace_graph_entry(struct trace_array *tr,
  242. struct ftrace_graph_ent *trace,
  243. unsigned long flags,
  244. int pc)
  245. {
  246. struct ftrace_event_call *call = &event_funcgraph_entry;
  247. struct ring_buffer_event *event;
  248. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  249. struct ftrace_graph_ent_entry *entry;
  250. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  251. return 0;
  252. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
  253. sizeof(*entry), flags, pc);
  254. if (!event)
  255. return 0;
  256. entry = ring_buffer_event_data(event);
  257. entry->graph_ent = *trace;
  258. if (!call_filter_check_discard(call, entry, buffer, event))
  259. __buffer_unlock_commit(buffer, event);
  260. return 1;
  261. }
  262. static inline int ftrace_graph_ignore_irqs(void)
  263. {
  264. if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
  265. return 0;
  266. return in_irq();
  267. }
  268. int trace_graph_entry(struct ftrace_graph_ent *trace)
  269. {
  270. struct trace_array *tr = graph_array;
  271. struct trace_array_cpu *data;
  272. unsigned long flags;
  273. long disabled;
  274. int ret;
  275. int cpu;
  276. int pc;
  277. if (!ftrace_trace_task(current))
  278. return 0;
  279. /* trace it when it is-nested-in or is a function enabled. */
  280. if ((!(trace->depth || ftrace_graph_addr(trace->func)) ||
  281. ftrace_graph_ignore_irqs()) || (trace->depth < 0) ||
  282. (max_depth && trace->depth >= max_depth))
  283. return 0;
  284. /*
  285. * Do not trace a function if it's filtered by set_graph_notrace.
  286. * Make the index of ret stack negative to indicate that it should
  287. * ignore further functions. But it needs its own ret stack entry
  288. * to recover the original index in order to continue tracing after
  289. * returning from the function.
  290. */
  291. if (ftrace_graph_notrace_addr(trace->func))
  292. return 1;
  293. local_irq_save(flags);
  294. cpu = raw_smp_processor_id();
  295. data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  296. disabled = atomic_inc_return(&data->disabled);
  297. if (likely(disabled == 1)) {
  298. pc = preempt_count();
  299. ret = __trace_graph_entry(tr, trace, flags, pc);
  300. } else {
  301. ret = 0;
  302. }
  303. atomic_dec(&data->disabled);
  304. local_irq_restore(flags);
  305. return ret;
  306. }
  307. static int trace_graph_thresh_entry(struct ftrace_graph_ent *trace)
  308. {
  309. if (tracing_thresh)
  310. return 1;
  311. else
  312. return trace_graph_entry(trace);
  313. }
  314. static void
  315. __trace_graph_function(struct trace_array *tr,
  316. unsigned long ip, unsigned long flags, int pc)
  317. {
  318. u64 time = trace_clock_local();
  319. struct ftrace_graph_ent ent = {
  320. .func = ip,
  321. .depth = 0,
  322. };
  323. struct ftrace_graph_ret ret = {
  324. .func = ip,
  325. .depth = 0,
  326. .calltime = time,
  327. .rettime = time,
  328. };
  329. __trace_graph_entry(tr, &ent, flags, pc);
  330. __trace_graph_return(tr, &ret, flags, pc);
  331. }
  332. void
  333. trace_graph_function(struct trace_array *tr,
  334. unsigned long ip, unsigned long parent_ip,
  335. unsigned long flags, int pc)
  336. {
  337. __trace_graph_function(tr, ip, flags, pc);
  338. }
  339. void __trace_graph_return(struct trace_array *tr,
  340. struct ftrace_graph_ret *trace,
  341. unsigned long flags,
  342. int pc)
  343. {
  344. struct ftrace_event_call *call = &event_funcgraph_exit;
  345. struct ring_buffer_event *event;
  346. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  347. struct ftrace_graph_ret_entry *entry;
  348. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  349. return;
  350. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
  351. sizeof(*entry), flags, pc);
  352. if (!event)
  353. return;
  354. entry = ring_buffer_event_data(event);
  355. entry->ret = *trace;
  356. if (!call_filter_check_discard(call, entry, buffer, event))
  357. __buffer_unlock_commit(buffer, event);
  358. }
  359. void trace_graph_return(struct ftrace_graph_ret *trace)
  360. {
  361. struct trace_array *tr = graph_array;
  362. struct trace_array_cpu *data;
  363. unsigned long flags;
  364. long disabled;
  365. int cpu;
  366. int pc;
  367. local_irq_save(flags);
  368. cpu = raw_smp_processor_id();
  369. data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  370. disabled = atomic_inc_return(&data->disabled);
  371. if (likely(disabled == 1)) {
  372. pc = preempt_count();
  373. __trace_graph_return(tr, trace, flags, pc);
  374. }
  375. atomic_dec(&data->disabled);
  376. local_irq_restore(flags);
  377. }
  378. void set_graph_array(struct trace_array *tr)
  379. {
  380. graph_array = tr;
  381. /* Make graph_array visible before we start tracing */
  382. smp_mb();
  383. }
  384. static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
  385. {
  386. if (tracing_thresh &&
  387. (trace->rettime - trace->calltime < tracing_thresh))
  388. return;
  389. else
  390. trace_graph_return(trace);
  391. }
  392. static int graph_trace_init(struct trace_array *tr)
  393. {
  394. int ret;
  395. set_graph_array(tr);
  396. if (tracing_thresh)
  397. ret = register_ftrace_graph(&trace_graph_thresh_return,
  398. &trace_graph_thresh_entry);
  399. else
  400. ret = register_ftrace_graph(&trace_graph_return,
  401. &trace_graph_entry);
  402. if (ret)
  403. return ret;
  404. tracing_start_cmdline_record();
  405. return 0;
  406. }
  407. static void graph_trace_reset(struct trace_array *tr)
  408. {
  409. tracing_stop_cmdline_record();
  410. unregister_ftrace_graph();
  411. }
  412. static int graph_trace_update_thresh(struct trace_array *tr)
  413. {
  414. graph_trace_reset(tr);
  415. return graph_trace_init(tr);
  416. }
  417. static int max_bytes_for_cpu;
  418. static void print_graph_cpu(struct trace_seq *s, int cpu)
  419. {
  420. /*
  421. * Start with a space character - to make it stand out
  422. * to the right a bit when trace output is pasted into
  423. * email:
  424. */
  425. trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
  426. }
  427. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  428. static void print_graph_proc(struct trace_seq *s, pid_t pid)
  429. {
  430. char comm[TASK_COMM_LEN];
  431. /* sign + log10(MAX_INT) + '\0' */
  432. char pid_str[11];
  433. int spaces = 0;
  434. int len;
  435. int i;
  436. trace_find_cmdline(pid, comm);
  437. comm[7] = '\0';
  438. sprintf(pid_str, "%d", pid);
  439. /* 1 stands for the "-" character */
  440. len = strlen(comm) + strlen(pid_str) + 1;
  441. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  442. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  443. /* First spaces to align center */
  444. for (i = 0; i < spaces / 2; i++)
  445. trace_seq_putc(s, ' ');
  446. trace_seq_printf(s, "%s-%s", comm, pid_str);
  447. /* Last spaces to align center */
  448. for (i = 0; i < spaces - (spaces / 2); i++)
  449. trace_seq_putc(s, ' ');
  450. }
  451. static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  452. {
  453. trace_seq_putc(s, ' ');
  454. trace_print_lat_fmt(s, entry);
  455. }
  456. /* If the pid changed since the last trace, output this event */
  457. static void
  458. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  459. {
  460. pid_t prev_pid;
  461. pid_t *last_pid;
  462. if (!data)
  463. return;
  464. last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  465. if (*last_pid == pid)
  466. return;
  467. prev_pid = *last_pid;
  468. *last_pid = pid;
  469. if (prev_pid == -1)
  470. return;
  471. /*
  472. * Context-switch trace line:
  473. ------------------------------------------
  474. | 1) migration/0--1 => sshd-1755
  475. ------------------------------------------
  476. */
  477. trace_seq_puts(s, " ------------------------------------------\n");
  478. print_graph_cpu(s, cpu);
  479. print_graph_proc(s, prev_pid);
  480. trace_seq_puts(s, " => ");
  481. print_graph_proc(s, pid);
  482. trace_seq_puts(s, "\n ------------------------------------------\n\n");
  483. }
  484. static struct ftrace_graph_ret_entry *
  485. get_return_for_leaf(struct trace_iterator *iter,
  486. struct ftrace_graph_ent_entry *curr)
  487. {
  488. struct fgraph_data *data = iter->private;
  489. struct ring_buffer_iter *ring_iter = NULL;
  490. struct ring_buffer_event *event;
  491. struct ftrace_graph_ret_entry *next;
  492. /*
  493. * If the previous output failed to write to the seq buffer,
  494. * then we just reuse the data from before.
  495. */
  496. if (data && data->failed) {
  497. curr = &data->ent;
  498. next = &data->ret;
  499. } else {
  500. ring_iter = trace_buffer_iter(iter, iter->cpu);
  501. /* First peek to compare current entry and the next one */
  502. if (ring_iter)
  503. event = ring_buffer_iter_peek(ring_iter, NULL);
  504. else {
  505. /*
  506. * We need to consume the current entry to see
  507. * the next one.
  508. */
  509. ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
  510. NULL, NULL);
  511. event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
  512. NULL, NULL);
  513. }
  514. if (!event)
  515. return NULL;
  516. next = ring_buffer_event_data(event);
  517. if (data) {
  518. /*
  519. * Save current and next entries for later reference
  520. * if the output fails.
  521. */
  522. data->ent = *curr;
  523. /*
  524. * If the next event is not a return type, then
  525. * we only care about what type it is. Otherwise we can
  526. * safely copy the entire event.
  527. */
  528. if (next->ent.type == TRACE_GRAPH_RET)
  529. data->ret = *next;
  530. else
  531. data->ret.ent.type = next->ent.type;
  532. }
  533. }
  534. if (next->ent.type != TRACE_GRAPH_RET)
  535. return NULL;
  536. if (curr->ent.pid != next->ent.pid ||
  537. curr->graph_ent.func != next->ret.func)
  538. return NULL;
  539. /* this is a leaf, now advance the iterator */
  540. if (ring_iter)
  541. ring_buffer_read(ring_iter, NULL);
  542. return next;
  543. }
  544. static void print_graph_abs_time(u64 t, struct trace_seq *s)
  545. {
  546. unsigned long usecs_rem;
  547. usecs_rem = do_div(t, NSEC_PER_SEC);
  548. usecs_rem /= 1000;
  549. trace_seq_printf(s, "%5lu.%06lu | ",
  550. (unsigned long)t, usecs_rem);
  551. }
  552. static void
  553. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  554. enum trace_type type, int cpu, pid_t pid, u32 flags)
  555. {
  556. struct trace_seq *s = &iter->seq;
  557. struct trace_entry *ent = iter->ent;
  558. if (addr < (unsigned long)__irqentry_text_start ||
  559. addr >= (unsigned long)__irqentry_text_end)
  560. return;
  561. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  562. /* Absolute time */
  563. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  564. print_graph_abs_time(iter->ts, s);
  565. /* Cpu */
  566. if (flags & TRACE_GRAPH_PRINT_CPU)
  567. print_graph_cpu(s, cpu);
  568. /* Proc */
  569. if (flags & TRACE_GRAPH_PRINT_PROC) {
  570. print_graph_proc(s, pid);
  571. trace_seq_puts(s, " | ");
  572. }
  573. /* Latency format */
  574. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  575. print_graph_lat_fmt(s, ent);
  576. }
  577. /* No overhead */
  578. print_graph_duration(0, s, flags | FLAGS_FILL_START);
  579. if (type == TRACE_GRAPH_ENT)
  580. trace_seq_puts(s, "==========>");
  581. else
  582. trace_seq_puts(s, "<==========");
  583. print_graph_duration(0, s, flags | FLAGS_FILL_END);
  584. trace_seq_putc(s, '\n');
  585. }
  586. void
  587. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  588. {
  589. unsigned long nsecs_rem = do_div(duration, 1000);
  590. /* log10(ULONG_MAX) + '\0' */
  591. char usecs_str[21];
  592. char nsecs_str[5];
  593. int len;
  594. int i;
  595. sprintf(usecs_str, "%lu", (unsigned long) duration);
  596. /* Print msecs */
  597. trace_seq_printf(s, "%s", usecs_str);
  598. len = strlen(usecs_str);
  599. /* Print nsecs (we don't want to exceed 7 numbers) */
  600. if (len < 7) {
  601. size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
  602. snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
  603. trace_seq_printf(s, ".%s", nsecs_str);
  604. len += strlen(nsecs_str);
  605. }
  606. trace_seq_puts(s, " us ");
  607. /* Print remaining spaces to fit the row's width */
  608. for (i = len; i < 7; i++)
  609. trace_seq_putc(s, ' ');
  610. }
  611. static void
  612. print_graph_duration(unsigned long long duration, struct trace_seq *s,
  613. u32 flags)
  614. {
  615. bool duration_printed = false;
  616. if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
  617. !(trace_flags & TRACE_ITER_CONTEXT_INFO))
  618. return;
  619. /* No real adata, just filling the column with spaces */
  620. switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
  621. case FLAGS_FILL_FULL:
  622. trace_seq_puts(s, " | ");
  623. return;
  624. case FLAGS_FILL_START:
  625. trace_seq_puts(s, " ");
  626. return;
  627. case FLAGS_FILL_END:
  628. trace_seq_puts(s, " |");
  629. return;
  630. }
  631. /* Signal a overhead of time execution to the output */
  632. if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
  633. /* Duration exceeded 100 usecs */
  634. if (duration > 100000ULL) {
  635. trace_seq_puts(s, "! ");
  636. duration_printed = true;
  637. /* Duration exceeded 10 usecs */
  638. } else if (duration > 10000ULL) {
  639. trace_seq_puts(s, "+ ");
  640. duration_printed = true;
  641. }
  642. }
  643. /*
  644. * If we did not exceed the duration tresholds or we dont want
  645. * to print out the overhead. Either way we need to fill out the space.
  646. */
  647. if (!duration_printed)
  648. trace_seq_puts(s, " ");
  649. trace_print_graph_duration(duration, s);
  650. trace_seq_puts(s, "| ");
  651. }
  652. /* Case of a leaf function on its call entry */
  653. static enum print_line_t
  654. print_graph_entry_leaf(struct trace_iterator *iter,
  655. struct ftrace_graph_ent_entry *entry,
  656. struct ftrace_graph_ret_entry *ret_entry,
  657. struct trace_seq *s, u32 flags)
  658. {
  659. struct fgraph_data *data = iter->private;
  660. struct ftrace_graph_ret *graph_ret;
  661. struct ftrace_graph_ent *call;
  662. unsigned long long duration;
  663. int i;
  664. graph_ret = &ret_entry->ret;
  665. call = &entry->graph_ent;
  666. duration = graph_ret->rettime - graph_ret->calltime;
  667. if (data) {
  668. struct fgraph_cpu_data *cpu_data;
  669. int cpu = iter->cpu;
  670. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  671. /*
  672. * Comments display at + 1 to depth. Since
  673. * this is a leaf function, keep the comments
  674. * equal to this depth.
  675. */
  676. cpu_data->depth = call->depth - 1;
  677. /* No need to keep this function around for this depth */
  678. if (call->depth < FTRACE_RETFUNC_DEPTH)
  679. cpu_data->enter_funcs[call->depth] = 0;
  680. }
  681. /* Overhead and duration */
  682. print_graph_duration(duration, s, flags);
  683. /* Function */
  684. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  685. trace_seq_putc(s, ' ');
  686. trace_seq_printf(s, "%ps();\n", (void *)call->func);
  687. return trace_handle_return(s);
  688. }
  689. static enum print_line_t
  690. print_graph_entry_nested(struct trace_iterator *iter,
  691. struct ftrace_graph_ent_entry *entry,
  692. struct trace_seq *s, int cpu, u32 flags)
  693. {
  694. struct ftrace_graph_ent *call = &entry->graph_ent;
  695. struct fgraph_data *data = iter->private;
  696. int i;
  697. if (data) {
  698. struct fgraph_cpu_data *cpu_data;
  699. int cpu = iter->cpu;
  700. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  701. cpu_data->depth = call->depth;
  702. /* Save this function pointer to see if the exit matches */
  703. if (call->depth < FTRACE_RETFUNC_DEPTH)
  704. cpu_data->enter_funcs[call->depth] = call->func;
  705. }
  706. /* No time */
  707. print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
  708. /* Function */
  709. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  710. trace_seq_putc(s, ' ');
  711. trace_seq_printf(s, "%ps() {\n", (void *)call->func);
  712. if (trace_seq_has_overflowed(s))
  713. return TRACE_TYPE_PARTIAL_LINE;
  714. /*
  715. * we already consumed the current entry to check the next one
  716. * and see if this is a leaf.
  717. */
  718. return TRACE_TYPE_NO_CONSUME;
  719. }
  720. static void
  721. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  722. int type, unsigned long addr, u32 flags)
  723. {
  724. struct fgraph_data *data = iter->private;
  725. struct trace_entry *ent = iter->ent;
  726. int cpu = iter->cpu;
  727. /* Pid */
  728. verif_pid(s, ent->pid, cpu, data);
  729. if (type)
  730. /* Interrupt */
  731. print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
  732. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  733. return;
  734. /* Absolute time */
  735. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  736. print_graph_abs_time(iter->ts, s);
  737. /* Cpu */
  738. if (flags & TRACE_GRAPH_PRINT_CPU)
  739. print_graph_cpu(s, cpu);
  740. /* Proc */
  741. if (flags & TRACE_GRAPH_PRINT_PROC) {
  742. print_graph_proc(s, ent->pid);
  743. trace_seq_puts(s, " | ");
  744. }
  745. /* Latency format */
  746. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  747. print_graph_lat_fmt(s, ent);
  748. return;
  749. }
  750. /*
  751. * Entry check for irq code
  752. *
  753. * returns 1 if
  754. * - we are inside irq code
  755. * - we just entered irq code
  756. *
  757. * retunns 0 if
  758. * - funcgraph-interrupts option is set
  759. * - we are not inside irq code
  760. */
  761. static int
  762. check_irq_entry(struct trace_iterator *iter, u32 flags,
  763. unsigned long addr, int depth)
  764. {
  765. int cpu = iter->cpu;
  766. int *depth_irq;
  767. struct fgraph_data *data = iter->private;
  768. /*
  769. * If we are either displaying irqs, or we got called as
  770. * a graph event and private data does not exist,
  771. * then we bypass the irq check.
  772. */
  773. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  774. (!data))
  775. return 0;
  776. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  777. /*
  778. * We are inside the irq code
  779. */
  780. if (*depth_irq >= 0)
  781. return 1;
  782. if ((addr < (unsigned long)__irqentry_text_start) ||
  783. (addr >= (unsigned long)__irqentry_text_end))
  784. return 0;
  785. /*
  786. * We are entering irq code.
  787. */
  788. *depth_irq = depth;
  789. return 1;
  790. }
  791. /*
  792. * Return check for irq code
  793. *
  794. * returns 1 if
  795. * - we are inside irq code
  796. * - we just left irq code
  797. *
  798. * returns 0 if
  799. * - funcgraph-interrupts option is set
  800. * - we are not inside irq code
  801. */
  802. static int
  803. check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
  804. {
  805. int cpu = iter->cpu;
  806. int *depth_irq;
  807. struct fgraph_data *data = iter->private;
  808. /*
  809. * If we are either displaying irqs, or we got called as
  810. * a graph event and private data does not exist,
  811. * then we bypass the irq check.
  812. */
  813. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  814. (!data))
  815. return 0;
  816. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  817. /*
  818. * We are not inside the irq code.
  819. */
  820. if (*depth_irq == -1)
  821. return 0;
  822. /*
  823. * We are inside the irq code, and this is returning entry.
  824. * Let's not trace it and clear the entry depth, since
  825. * we are out of irq code.
  826. *
  827. * This condition ensures that we 'leave the irq code' once
  828. * we are out of the entry depth. Thus protecting us from
  829. * the RETURN entry loss.
  830. */
  831. if (*depth_irq >= depth) {
  832. *depth_irq = -1;
  833. return 1;
  834. }
  835. /*
  836. * We are inside the irq code, and this is not the entry.
  837. */
  838. return 1;
  839. }
  840. static enum print_line_t
  841. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  842. struct trace_iterator *iter, u32 flags)
  843. {
  844. struct fgraph_data *data = iter->private;
  845. struct ftrace_graph_ent *call = &field->graph_ent;
  846. struct ftrace_graph_ret_entry *leaf_ret;
  847. static enum print_line_t ret;
  848. int cpu = iter->cpu;
  849. if (check_irq_entry(iter, flags, call->func, call->depth))
  850. return TRACE_TYPE_HANDLED;
  851. print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
  852. leaf_ret = get_return_for_leaf(iter, field);
  853. if (leaf_ret)
  854. ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
  855. else
  856. ret = print_graph_entry_nested(iter, field, s, cpu, flags);
  857. if (data) {
  858. /*
  859. * If we failed to write our output, then we need to make
  860. * note of it. Because we already consumed our entry.
  861. */
  862. if (s->full) {
  863. data->failed = 1;
  864. data->cpu = cpu;
  865. } else
  866. data->failed = 0;
  867. }
  868. return ret;
  869. }
  870. static enum print_line_t
  871. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  872. struct trace_entry *ent, struct trace_iterator *iter,
  873. u32 flags)
  874. {
  875. unsigned long long duration = trace->rettime - trace->calltime;
  876. struct fgraph_data *data = iter->private;
  877. pid_t pid = ent->pid;
  878. int cpu = iter->cpu;
  879. int func_match = 1;
  880. int i;
  881. if (check_irq_return(iter, flags, trace->depth))
  882. return TRACE_TYPE_HANDLED;
  883. if (data) {
  884. struct fgraph_cpu_data *cpu_data;
  885. int cpu = iter->cpu;
  886. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  887. /*
  888. * Comments display at + 1 to depth. This is the
  889. * return from a function, we now want the comments
  890. * to display at the same level of the bracket.
  891. */
  892. cpu_data->depth = trace->depth - 1;
  893. if (trace->depth < FTRACE_RETFUNC_DEPTH) {
  894. if (cpu_data->enter_funcs[trace->depth] != trace->func)
  895. func_match = 0;
  896. cpu_data->enter_funcs[trace->depth] = 0;
  897. }
  898. }
  899. print_graph_prologue(iter, s, 0, 0, flags);
  900. /* Overhead and duration */
  901. print_graph_duration(duration, s, flags);
  902. /* Closing brace */
  903. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
  904. trace_seq_putc(s, ' ');
  905. /*
  906. * If the return function does not have a matching entry,
  907. * then the entry was lost. Instead of just printing
  908. * the '}' and letting the user guess what function this
  909. * belongs to, write out the function name. Always do
  910. * that if the funcgraph-tail option is enabled.
  911. */
  912. if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
  913. trace_seq_puts(s, "}\n");
  914. else
  915. trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
  916. /* Overrun */
  917. if (flags & TRACE_GRAPH_PRINT_OVERRUN)
  918. trace_seq_printf(s, " (Overruns: %lu)\n",
  919. trace->overrun);
  920. print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
  921. cpu, pid, flags);
  922. return trace_handle_return(s);
  923. }
  924. static enum print_line_t
  925. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  926. struct trace_iterator *iter, u32 flags)
  927. {
  928. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  929. struct fgraph_data *data = iter->private;
  930. struct trace_event *event;
  931. int depth = 0;
  932. int ret;
  933. int i;
  934. if (data)
  935. depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
  936. print_graph_prologue(iter, s, 0, 0, flags);
  937. /* No time */
  938. print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
  939. /* Indentation */
  940. if (depth > 0)
  941. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
  942. trace_seq_putc(s, ' ');
  943. /* The comment */
  944. trace_seq_puts(s, "/* ");
  945. switch (iter->ent->type) {
  946. case TRACE_BPRINT:
  947. ret = trace_print_bprintk_msg_only(iter);
  948. if (ret != TRACE_TYPE_HANDLED)
  949. return ret;
  950. break;
  951. case TRACE_PRINT:
  952. ret = trace_print_printk_msg_only(iter);
  953. if (ret != TRACE_TYPE_HANDLED)
  954. return ret;
  955. break;
  956. default:
  957. event = ftrace_find_event(ent->type);
  958. if (!event)
  959. return TRACE_TYPE_UNHANDLED;
  960. ret = event->funcs->trace(iter, sym_flags, event);
  961. if (ret != TRACE_TYPE_HANDLED)
  962. return ret;
  963. }
  964. /* Strip ending newline */
  965. if (s->buffer[s->seq.len - 1] == '\n') {
  966. s->buffer[s->seq.len - 1] = '\0';
  967. s->seq.len--;
  968. }
  969. trace_seq_puts(s, " */\n");
  970. return trace_handle_return(s);
  971. }
  972. enum print_line_t
  973. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  974. {
  975. struct ftrace_graph_ent_entry *field;
  976. struct fgraph_data *data = iter->private;
  977. struct trace_entry *entry = iter->ent;
  978. struct trace_seq *s = &iter->seq;
  979. int cpu = iter->cpu;
  980. int ret;
  981. if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
  982. per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
  983. return TRACE_TYPE_HANDLED;
  984. }
  985. /*
  986. * If the last output failed, there's a possibility we need
  987. * to print out the missing entry which would never go out.
  988. */
  989. if (data && data->failed) {
  990. field = &data->ent;
  991. iter->cpu = data->cpu;
  992. ret = print_graph_entry(field, s, iter, flags);
  993. if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
  994. per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
  995. ret = TRACE_TYPE_NO_CONSUME;
  996. }
  997. iter->cpu = cpu;
  998. return ret;
  999. }
  1000. switch (entry->type) {
  1001. case TRACE_GRAPH_ENT: {
  1002. /*
  1003. * print_graph_entry() may consume the current event,
  1004. * thus @field may become invalid, so we need to save it.
  1005. * sizeof(struct ftrace_graph_ent_entry) is very small,
  1006. * it can be safely saved at the stack.
  1007. */
  1008. struct ftrace_graph_ent_entry saved;
  1009. trace_assign_type(field, entry);
  1010. saved = *field;
  1011. return print_graph_entry(&saved, s, iter, flags);
  1012. }
  1013. case TRACE_GRAPH_RET: {
  1014. struct ftrace_graph_ret_entry *field;
  1015. trace_assign_type(field, entry);
  1016. return print_graph_return(&field->ret, s, entry, iter, flags);
  1017. }
  1018. case TRACE_STACK:
  1019. case TRACE_FN:
  1020. /* dont trace stack and functions as comments */
  1021. return TRACE_TYPE_UNHANDLED;
  1022. default:
  1023. return print_graph_comment(s, entry, iter, flags);
  1024. }
  1025. return TRACE_TYPE_HANDLED;
  1026. }
  1027. static enum print_line_t
  1028. print_graph_function(struct trace_iterator *iter)
  1029. {
  1030. return print_graph_function_flags(iter, tracer_flags.val);
  1031. }
  1032. static enum print_line_t
  1033. print_graph_function_event(struct trace_iterator *iter, int flags,
  1034. struct trace_event *event)
  1035. {
  1036. return print_graph_function(iter);
  1037. }
  1038. static void print_lat_header(struct seq_file *s, u32 flags)
  1039. {
  1040. static const char spaces[] = " " /* 16 spaces */
  1041. " " /* 4 spaces */
  1042. " "; /* 17 spaces */
  1043. int size = 0;
  1044. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1045. size += 16;
  1046. if (flags & TRACE_GRAPH_PRINT_CPU)
  1047. size += 4;
  1048. if (flags & TRACE_GRAPH_PRINT_PROC)
  1049. size += 17;
  1050. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  1051. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  1052. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  1053. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  1054. seq_printf(s, "#%.*s||| / \n", size, spaces);
  1055. }
  1056. static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
  1057. {
  1058. int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
  1059. if (lat)
  1060. print_lat_header(s, flags);
  1061. /* 1st line */
  1062. seq_putc(s, '#');
  1063. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1064. seq_puts(s, " TIME ");
  1065. if (flags & TRACE_GRAPH_PRINT_CPU)
  1066. seq_puts(s, " CPU");
  1067. if (flags & TRACE_GRAPH_PRINT_PROC)
  1068. seq_puts(s, " TASK/PID ");
  1069. if (lat)
  1070. seq_puts(s, "||||");
  1071. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1072. seq_puts(s, " DURATION ");
  1073. seq_puts(s, " FUNCTION CALLS\n");
  1074. /* 2nd line */
  1075. seq_putc(s, '#');
  1076. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1077. seq_puts(s, " | ");
  1078. if (flags & TRACE_GRAPH_PRINT_CPU)
  1079. seq_puts(s, " | ");
  1080. if (flags & TRACE_GRAPH_PRINT_PROC)
  1081. seq_puts(s, " | | ");
  1082. if (lat)
  1083. seq_puts(s, "||||");
  1084. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1085. seq_puts(s, " | | ");
  1086. seq_puts(s, " | | | |\n");
  1087. }
  1088. static void print_graph_headers(struct seq_file *s)
  1089. {
  1090. print_graph_headers_flags(s, tracer_flags.val);
  1091. }
  1092. void print_graph_headers_flags(struct seq_file *s, u32 flags)
  1093. {
  1094. struct trace_iterator *iter = s->private;
  1095. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  1096. return;
  1097. if (trace_flags & TRACE_ITER_LATENCY_FMT) {
  1098. /* print nothing if the buffers are empty */
  1099. if (trace_empty(iter))
  1100. return;
  1101. print_trace_header(s, iter);
  1102. }
  1103. __print_graph_headers_flags(s, flags);
  1104. }
  1105. void graph_trace_open(struct trace_iterator *iter)
  1106. {
  1107. /* pid and depth on the last trace processed */
  1108. struct fgraph_data *data;
  1109. int cpu;
  1110. iter->private = NULL;
  1111. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1112. if (!data)
  1113. goto out_err;
  1114. data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
  1115. if (!data->cpu_data)
  1116. goto out_err_free;
  1117. for_each_possible_cpu(cpu) {
  1118. pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  1119. int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
  1120. int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
  1121. int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  1122. *pid = -1;
  1123. *depth = 0;
  1124. *ignore = 0;
  1125. *depth_irq = -1;
  1126. }
  1127. iter->private = data;
  1128. return;
  1129. out_err_free:
  1130. kfree(data);
  1131. out_err:
  1132. pr_warning("function graph tracer: not enough memory\n");
  1133. }
  1134. void graph_trace_close(struct trace_iterator *iter)
  1135. {
  1136. struct fgraph_data *data = iter->private;
  1137. if (data) {
  1138. free_percpu(data->cpu_data);
  1139. kfree(data);
  1140. }
  1141. }
  1142. static int
  1143. func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  1144. {
  1145. if (bit == TRACE_GRAPH_PRINT_IRQS)
  1146. ftrace_graph_skip_irqs = !set;
  1147. return 0;
  1148. }
  1149. static struct trace_event_functions graph_functions = {
  1150. .trace = print_graph_function_event,
  1151. };
  1152. static struct trace_event graph_trace_entry_event = {
  1153. .type = TRACE_GRAPH_ENT,
  1154. .funcs = &graph_functions,
  1155. };
  1156. static struct trace_event graph_trace_ret_event = {
  1157. .type = TRACE_GRAPH_RET,
  1158. .funcs = &graph_functions
  1159. };
  1160. static struct tracer graph_trace __tracer_data = {
  1161. .name = "function_graph",
  1162. .update_thresh = graph_trace_update_thresh,
  1163. .open = graph_trace_open,
  1164. .pipe_open = graph_trace_open,
  1165. .close = graph_trace_close,
  1166. .pipe_close = graph_trace_close,
  1167. .init = graph_trace_init,
  1168. .reset = graph_trace_reset,
  1169. .print_line = print_graph_function,
  1170. .print_header = print_graph_headers,
  1171. .flags = &tracer_flags,
  1172. .set_flag = func_graph_set_flag,
  1173. #ifdef CONFIG_FTRACE_SELFTEST
  1174. .selftest = trace_selftest_startup_function_graph,
  1175. #endif
  1176. };
  1177. static ssize_t
  1178. graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1179. loff_t *ppos)
  1180. {
  1181. unsigned long val;
  1182. int ret;
  1183. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1184. if (ret)
  1185. return ret;
  1186. max_depth = val;
  1187. *ppos += cnt;
  1188. return cnt;
  1189. }
  1190. static ssize_t
  1191. graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
  1192. loff_t *ppos)
  1193. {
  1194. char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
  1195. int n;
  1196. n = sprintf(buf, "%d\n", max_depth);
  1197. return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
  1198. }
  1199. static const struct file_operations graph_depth_fops = {
  1200. .open = tracing_open_generic,
  1201. .write = graph_depth_write,
  1202. .read = graph_depth_read,
  1203. .llseek = generic_file_llseek,
  1204. };
  1205. static __init int init_graph_debugfs(void)
  1206. {
  1207. struct dentry *d_tracer;
  1208. d_tracer = tracing_init_dentry();
  1209. if (!d_tracer)
  1210. return 0;
  1211. trace_create_file("max_graph_depth", 0644, d_tracer,
  1212. NULL, &graph_depth_fops);
  1213. return 0;
  1214. }
  1215. fs_initcall(init_graph_debugfs);
  1216. static __init int init_graph_trace(void)
  1217. {
  1218. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  1219. if (!register_ftrace_event(&graph_trace_entry_event)) {
  1220. pr_warning("Warning: could not register graph trace events\n");
  1221. return 1;
  1222. }
  1223. if (!register_ftrace_event(&graph_trace_ret_event)) {
  1224. pr_warning("Warning: could not register graph trace events\n");
  1225. return 1;
  1226. }
  1227. return register_tracer(&graph_trace);
  1228. }
  1229. core_initcall(init_graph_trace);