trace_functions_graph.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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. trace_buffer_unlock_commit_nostack(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. trace_buffer_unlock_commit_nostack(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 i;
  703. graph_ret = &ret_entry->ret;
  704. call = &entry->graph_ent;
  705. duration = graph_ret->rettime - graph_ret->calltime;
  706. if (data) {
  707. struct fgraph_cpu_data *cpu_data;
  708. int cpu = iter->cpu;
  709. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  710. /*
  711. * Comments display at + 1 to depth. Since
  712. * this is a leaf function, keep the comments
  713. * equal to this depth.
  714. */
  715. cpu_data->depth = call->depth - 1;
  716. /* No need to keep this function around for this depth */
  717. if (call->depth < FTRACE_RETFUNC_DEPTH)
  718. cpu_data->enter_funcs[call->depth] = 0;
  719. }
  720. /* Overhead and duration */
  721. print_graph_duration(tr, duration, s, flags);
  722. /* Function */
  723. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  724. trace_seq_putc(s, ' ');
  725. trace_seq_printf(s, "%ps();\n", (void *)call->func);
  726. return trace_handle_return(s);
  727. }
  728. static enum print_line_t
  729. print_graph_entry_nested(struct trace_iterator *iter,
  730. struct ftrace_graph_ent_entry *entry,
  731. struct trace_seq *s, int cpu, u32 flags)
  732. {
  733. struct ftrace_graph_ent *call = &entry->graph_ent;
  734. struct fgraph_data *data = iter->private;
  735. struct trace_array *tr = iter->tr;
  736. int i;
  737. if (data) {
  738. struct fgraph_cpu_data *cpu_data;
  739. int cpu = iter->cpu;
  740. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  741. cpu_data->depth = call->depth;
  742. /* Save this function pointer to see if the exit matches */
  743. if (call->depth < FTRACE_RETFUNC_DEPTH)
  744. cpu_data->enter_funcs[call->depth] = call->func;
  745. }
  746. /* No time */
  747. print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
  748. /* Function */
  749. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  750. trace_seq_putc(s, ' ');
  751. trace_seq_printf(s, "%ps() {\n", (void *)call->func);
  752. if (trace_seq_has_overflowed(s))
  753. return TRACE_TYPE_PARTIAL_LINE;
  754. /*
  755. * we already consumed the current entry to check the next one
  756. * and see if this is a leaf.
  757. */
  758. return TRACE_TYPE_NO_CONSUME;
  759. }
  760. static void
  761. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  762. int type, unsigned long addr, u32 flags)
  763. {
  764. struct fgraph_data *data = iter->private;
  765. struct trace_entry *ent = iter->ent;
  766. struct trace_array *tr = iter->tr;
  767. int cpu = iter->cpu;
  768. /* Pid */
  769. verif_pid(s, ent->pid, cpu, data);
  770. if (type)
  771. /* Interrupt */
  772. print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
  773. if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
  774. return;
  775. /* Absolute time */
  776. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  777. print_graph_abs_time(iter->ts, s);
  778. /* Cpu */
  779. if (flags & TRACE_GRAPH_PRINT_CPU)
  780. print_graph_cpu(s, cpu);
  781. /* Proc */
  782. if (flags & TRACE_GRAPH_PRINT_PROC) {
  783. print_graph_proc(s, ent->pid);
  784. trace_seq_puts(s, " | ");
  785. }
  786. /* Latency format */
  787. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  788. print_graph_lat_fmt(s, ent);
  789. return;
  790. }
  791. /*
  792. * Entry check for irq code
  793. *
  794. * returns 1 if
  795. * - we are inside irq code
  796. * - we just entered irq code
  797. *
  798. * retunns 0 if
  799. * - funcgraph-interrupts option is set
  800. * - we are not inside irq code
  801. */
  802. static int
  803. check_irq_entry(struct trace_iterator *iter, u32 flags,
  804. unsigned long addr, int depth)
  805. {
  806. int cpu = iter->cpu;
  807. int *depth_irq;
  808. struct fgraph_data *data = iter->private;
  809. /*
  810. * If we are either displaying irqs, or we got called as
  811. * a graph event and private data does not exist,
  812. * then we bypass the irq check.
  813. */
  814. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  815. (!data))
  816. return 0;
  817. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  818. /*
  819. * We are inside the irq code
  820. */
  821. if (*depth_irq >= 0)
  822. return 1;
  823. if ((addr < (unsigned long)__irqentry_text_start) ||
  824. (addr >= (unsigned long)__irqentry_text_end))
  825. return 0;
  826. /*
  827. * We are entering irq code.
  828. */
  829. *depth_irq = depth;
  830. return 1;
  831. }
  832. /*
  833. * Return check for irq code
  834. *
  835. * returns 1 if
  836. * - we are inside irq code
  837. * - we just left irq code
  838. *
  839. * returns 0 if
  840. * - funcgraph-interrupts option is set
  841. * - we are not inside irq code
  842. */
  843. static int
  844. check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
  845. {
  846. int cpu = iter->cpu;
  847. int *depth_irq;
  848. struct fgraph_data *data = iter->private;
  849. /*
  850. * If we are either displaying irqs, or we got called as
  851. * a graph event and private data does not exist,
  852. * then we bypass the irq check.
  853. */
  854. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  855. (!data))
  856. return 0;
  857. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  858. /*
  859. * We are not inside the irq code.
  860. */
  861. if (*depth_irq == -1)
  862. return 0;
  863. /*
  864. * We are inside the irq code, and this is returning entry.
  865. * Let's not trace it and clear the entry depth, since
  866. * we are out of irq code.
  867. *
  868. * This condition ensures that we 'leave the irq code' once
  869. * we are out of the entry depth. Thus protecting us from
  870. * the RETURN entry loss.
  871. */
  872. if (*depth_irq >= depth) {
  873. *depth_irq = -1;
  874. return 1;
  875. }
  876. /*
  877. * We are inside the irq code, and this is not the entry.
  878. */
  879. return 1;
  880. }
  881. static enum print_line_t
  882. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  883. struct trace_iterator *iter, u32 flags)
  884. {
  885. struct fgraph_data *data = iter->private;
  886. struct ftrace_graph_ent *call = &field->graph_ent;
  887. struct ftrace_graph_ret_entry *leaf_ret;
  888. static enum print_line_t ret;
  889. int cpu = iter->cpu;
  890. if (check_irq_entry(iter, flags, call->func, call->depth))
  891. return TRACE_TYPE_HANDLED;
  892. print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
  893. leaf_ret = get_return_for_leaf(iter, field);
  894. if (leaf_ret)
  895. ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
  896. else
  897. ret = print_graph_entry_nested(iter, field, s, cpu, flags);
  898. if (data) {
  899. /*
  900. * If we failed to write our output, then we need to make
  901. * note of it. Because we already consumed our entry.
  902. */
  903. if (s->full) {
  904. data->failed = 1;
  905. data->cpu = cpu;
  906. } else
  907. data->failed = 0;
  908. }
  909. return ret;
  910. }
  911. static enum print_line_t
  912. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  913. struct trace_entry *ent, struct trace_iterator *iter,
  914. u32 flags)
  915. {
  916. unsigned long long duration = trace->rettime - trace->calltime;
  917. struct fgraph_data *data = iter->private;
  918. struct trace_array *tr = iter->tr;
  919. pid_t pid = ent->pid;
  920. int cpu = iter->cpu;
  921. int func_match = 1;
  922. int i;
  923. if (check_irq_return(iter, flags, trace->depth))
  924. return TRACE_TYPE_HANDLED;
  925. if (data) {
  926. struct fgraph_cpu_data *cpu_data;
  927. int cpu = iter->cpu;
  928. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  929. /*
  930. * Comments display at + 1 to depth. This is the
  931. * return from a function, we now want the comments
  932. * to display at the same level of the bracket.
  933. */
  934. cpu_data->depth = trace->depth - 1;
  935. if (trace->depth < FTRACE_RETFUNC_DEPTH) {
  936. if (cpu_data->enter_funcs[trace->depth] != trace->func)
  937. func_match = 0;
  938. cpu_data->enter_funcs[trace->depth] = 0;
  939. }
  940. }
  941. print_graph_prologue(iter, s, 0, 0, flags);
  942. /* Overhead and duration */
  943. print_graph_duration(tr, duration, s, flags);
  944. /* Closing brace */
  945. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
  946. trace_seq_putc(s, ' ');
  947. /*
  948. * If the return function does not have a matching entry,
  949. * then the entry was lost. Instead of just printing
  950. * the '}' and letting the user guess what function this
  951. * belongs to, write out the function name. Always do
  952. * that if the funcgraph-tail option is enabled.
  953. */
  954. if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
  955. trace_seq_puts(s, "}\n");
  956. else
  957. trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
  958. /* Overrun */
  959. if (flags & TRACE_GRAPH_PRINT_OVERRUN)
  960. trace_seq_printf(s, " (Overruns: %lu)\n",
  961. trace->overrun);
  962. print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
  963. cpu, pid, flags);
  964. return trace_handle_return(s);
  965. }
  966. static enum print_line_t
  967. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  968. struct trace_iterator *iter, u32 flags)
  969. {
  970. struct trace_array *tr = iter->tr;
  971. unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
  972. struct fgraph_data *data = iter->private;
  973. struct trace_event *event;
  974. int depth = 0;
  975. int ret;
  976. int i;
  977. if (data)
  978. depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
  979. print_graph_prologue(iter, s, 0, 0, flags);
  980. /* No time */
  981. print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
  982. /* Indentation */
  983. if (depth > 0)
  984. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
  985. trace_seq_putc(s, ' ');
  986. /* The comment */
  987. trace_seq_puts(s, "/* ");
  988. switch (iter->ent->type) {
  989. case TRACE_BPUTS:
  990. ret = trace_print_bputs_msg_only(iter);
  991. if (ret != TRACE_TYPE_HANDLED)
  992. return ret;
  993. break;
  994. case TRACE_BPRINT:
  995. ret = trace_print_bprintk_msg_only(iter);
  996. if (ret != TRACE_TYPE_HANDLED)
  997. return ret;
  998. break;
  999. case TRACE_PRINT:
  1000. ret = trace_print_printk_msg_only(iter);
  1001. if (ret != TRACE_TYPE_HANDLED)
  1002. return ret;
  1003. break;
  1004. default:
  1005. event = ftrace_find_event(ent->type);
  1006. if (!event)
  1007. return TRACE_TYPE_UNHANDLED;
  1008. ret = event->funcs->trace(iter, sym_flags, event);
  1009. if (ret != TRACE_TYPE_HANDLED)
  1010. return ret;
  1011. }
  1012. if (trace_seq_has_overflowed(s))
  1013. goto out;
  1014. /* Strip ending newline */
  1015. if (s->buffer[s->seq.len - 1] == '\n') {
  1016. s->buffer[s->seq.len - 1] = '\0';
  1017. s->seq.len--;
  1018. }
  1019. trace_seq_puts(s, " */\n");
  1020. out:
  1021. return trace_handle_return(s);
  1022. }
  1023. enum print_line_t
  1024. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  1025. {
  1026. struct ftrace_graph_ent_entry *field;
  1027. struct fgraph_data *data = iter->private;
  1028. struct trace_entry *entry = iter->ent;
  1029. struct trace_seq *s = &iter->seq;
  1030. int cpu = iter->cpu;
  1031. int ret;
  1032. if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
  1033. per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
  1034. return TRACE_TYPE_HANDLED;
  1035. }
  1036. /*
  1037. * If the last output failed, there's a possibility we need
  1038. * to print out the missing entry which would never go out.
  1039. */
  1040. if (data && data->failed) {
  1041. field = &data->ent;
  1042. iter->cpu = data->cpu;
  1043. ret = print_graph_entry(field, s, iter, flags);
  1044. if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
  1045. per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
  1046. ret = TRACE_TYPE_NO_CONSUME;
  1047. }
  1048. iter->cpu = cpu;
  1049. return ret;
  1050. }
  1051. switch (entry->type) {
  1052. case TRACE_GRAPH_ENT: {
  1053. /*
  1054. * print_graph_entry() may consume the current event,
  1055. * thus @field may become invalid, so we need to save it.
  1056. * sizeof(struct ftrace_graph_ent_entry) is very small,
  1057. * it can be safely saved at the stack.
  1058. */
  1059. struct ftrace_graph_ent_entry saved;
  1060. trace_assign_type(field, entry);
  1061. saved = *field;
  1062. return print_graph_entry(&saved, s, iter, flags);
  1063. }
  1064. case TRACE_GRAPH_RET: {
  1065. struct ftrace_graph_ret_entry *field;
  1066. trace_assign_type(field, entry);
  1067. return print_graph_return(&field->ret, s, entry, iter, flags);
  1068. }
  1069. case TRACE_STACK:
  1070. case TRACE_FN:
  1071. /* dont trace stack and functions as comments */
  1072. return TRACE_TYPE_UNHANDLED;
  1073. default:
  1074. return print_graph_comment(s, entry, iter, flags);
  1075. }
  1076. return TRACE_TYPE_HANDLED;
  1077. }
  1078. static enum print_line_t
  1079. print_graph_function(struct trace_iterator *iter)
  1080. {
  1081. return print_graph_function_flags(iter, tracer_flags.val);
  1082. }
  1083. static enum print_line_t
  1084. print_graph_function_event(struct trace_iterator *iter, int flags,
  1085. struct trace_event *event)
  1086. {
  1087. return print_graph_function(iter);
  1088. }
  1089. static void print_lat_header(struct seq_file *s, u32 flags)
  1090. {
  1091. static const char spaces[] = " " /* 16 spaces */
  1092. " " /* 4 spaces */
  1093. " "; /* 17 spaces */
  1094. int size = 0;
  1095. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1096. size += 16;
  1097. if (flags & TRACE_GRAPH_PRINT_CPU)
  1098. size += 4;
  1099. if (flags & TRACE_GRAPH_PRINT_PROC)
  1100. size += 17;
  1101. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  1102. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  1103. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  1104. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  1105. seq_printf(s, "#%.*s||| / \n", size, spaces);
  1106. }
  1107. static void __print_graph_headers_flags(struct trace_array *tr,
  1108. struct seq_file *s, u32 flags)
  1109. {
  1110. int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
  1111. if (lat)
  1112. print_lat_header(s, flags);
  1113. /* 1st line */
  1114. seq_putc(s, '#');
  1115. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1116. seq_puts(s, " TIME ");
  1117. if (flags & TRACE_GRAPH_PRINT_CPU)
  1118. seq_puts(s, " CPU");
  1119. if (flags & TRACE_GRAPH_PRINT_PROC)
  1120. seq_puts(s, " TASK/PID ");
  1121. if (lat)
  1122. seq_puts(s, "||||");
  1123. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1124. seq_puts(s, " DURATION ");
  1125. seq_puts(s, " FUNCTION CALLS\n");
  1126. /* 2nd line */
  1127. seq_putc(s, '#');
  1128. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1129. seq_puts(s, " | ");
  1130. if (flags & TRACE_GRAPH_PRINT_CPU)
  1131. seq_puts(s, " | ");
  1132. if (flags & TRACE_GRAPH_PRINT_PROC)
  1133. seq_puts(s, " | | ");
  1134. if (lat)
  1135. seq_puts(s, "||||");
  1136. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1137. seq_puts(s, " | | ");
  1138. seq_puts(s, " | | | |\n");
  1139. }
  1140. static void print_graph_headers(struct seq_file *s)
  1141. {
  1142. print_graph_headers_flags(s, tracer_flags.val);
  1143. }
  1144. void print_graph_headers_flags(struct seq_file *s, u32 flags)
  1145. {
  1146. struct trace_iterator *iter = s->private;
  1147. struct trace_array *tr = iter->tr;
  1148. if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
  1149. return;
  1150. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
  1151. /* print nothing if the buffers are empty */
  1152. if (trace_empty(iter))
  1153. return;
  1154. print_trace_header(s, iter);
  1155. }
  1156. __print_graph_headers_flags(tr, s, flags);
  1157. }
  1158. void graph_trace_open(struct trace_iterator *iter)
  1159. {
  1160. /* pid and depth on the last trace processed */
  1161. struct fgraph_data *data;
  1162. gfp_t gfpflags;
  1163. int cpu;
  1164. iter->private = NULL;
  1165. /* We can be called in atomic context via ftrace_dump() */
  1166. gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
  1167. data = kzalloc(sizeof(*data), gfpflags);
  1168. if (!data)
  1169. goto out_err;
  1170. data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
  1171. if (!data->cpu_data)
  1172. goto out_err_free;
  1173. for_each_possible_cpu(cpu) {
  1174. pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  1175. int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
  1176. int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
  1177. int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  1178. *pid = -1;
  1179. *depth = 0;
  1180. *ignore = 0;
  1181. *depth_irq = -1;
  1182. }
  1183. iter->private = data;
  1184. return;
  1185. out_err_free:
  1186. kfree(data);
  1187. out_err:
  1188. pr_warn("function graph tracer: not enough memory\n");
  1189. }
  1190. void graph_trace_close(struct trace_iterator *iter)
  1191. {
  1192. struct fgraph_data *data = iter->private;
  1193. if (data) {
  1194. free_percpu(data->cpu_data);
  1195. kfree(data);
  1196. }
  1197. }
  1198. static int
  1199. func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  1200. {
  1201. if (bit == TRACE_GRAPH_PRINT_IRQS)
  1202. ftrace_graph_skip_irqs = !set;
  1203. if (bit == TRACE_GRAPH_SLEEP_TIME)
  1204. ftrace_graph_sleep_time_control(set);
  1205. if (bit == TRACE_GRAPH_GRAPH_TIME)
  1206. ftrace_graph_graph_time_control(set);
  1207. return 0;
  1208. }
  1209. static struct trace_event_functions graph_functions = {
  1210. .trace = print_graph_function_event,
  1211. };
  1212. static struct trace_event graph_trace_entry_event = {
  1213. .type = TRACE_GRAPH_ENT,
  1214. .funcs = &graph_functions,
  1215. };
  1216. static struct trace_event graph_trace_ret_event = {
  1217. .type = TRACE_GRAPH_RET,
  1218. .funcs = &graph_functions
  1219. };
  1220. static struct tracer graph_trace __tracer_data = {
  1221. .name = "function_graph",
  1222. .update_thresh = graph_trace_update_thresh,
  1223. .open = graph_trace_open,
  1224. .pipe_open = graph_trace_open,
  1225. .close = graph_trace_close,
  1226. .pipe_close = graph_trace_close,
  1227. .init = graph_trace_init,
  1228. .reset = graph_trace_reset,
  1229. .print_line = print_graph_function,
  1230. .print_header = print_graph_headers,
  1231. .flags = &tracer_flags,
  1232. .set_flag = func_graph_set_flag,
  1233. #ifdef CONFIG_FTRACE_SELFTEST
  1234. .selftest = trace_selftest_startup_function_graph,
  1235. #endif
  1236. };
  1237. static ssize_t
  1238. graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1239. loff_t *ppos)
  1240. {
  1241. unsigned long val;
  1242. int ret;
  1243. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1244. if (ret)
  1245. return ret;
  1246. max_depth = val;
  1247. *ppos += cnt;
  1248. return cnt;
  1249. }
  1250. static ssize_t
  1251. graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
  1252. loff_t *ppos)
  1253. {
  1254. char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
  1255. int n;
  1256. n = sprintf(buf, "%d\n", max_depth);
  1257. return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
  1258. }
  1259. static const struct file_operations graph_depth_fops = {
  1260. .open = tracing_open_generic,
  1261. .write = graph_depth_write,
  1262. .read = graph_depth_read,
  1263. .llseek = generic_file_llseek,
  1264. };
  1265. static __init int init_graph_tracefs(void)
  1266. {
  1267. struct dentry *d_tracer;
  1268. d_tracer = tracing_init_dentry();
  1269. if (IS_ERR(d_tracer))
  1270. return 0;
  1271. trace_create_file("max_graph_depth", 0644, d_tracer,
  1272. NULL, &graph_depth_fops);
  1273. return 0;
  1274. }
  1275. fs_initcall(init_graph_tracefs);
  1276. static __init int init_graph_trace(void)
  1277. {
  1278. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  1279. if (!register_trace_event(&graph_trace_entry_event)) {
  1280. pr_warn("Warning: could not register graph trace events\n");
  1281. return 1;
  1282. }
  1283. if (!register_trace_event(&graph_trace_ret_event)) {
  1284. pr_warn("Warning: could not register graph trace events\n");
  1285. return 1;
  1286. }
  1287. return register_tracer(&graph_trace);
  1288. }
  1289. core_initcall(init_graph_trace);