trace_functions_graph.c 38 KB

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