trace.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 William Lee Irwin III
  13. */
  14. #include <linux/utsrelease.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/linkage.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/module.h>
  24. #include <linux/percpu.h>
  25. #include <linux/ctype.h>
  26. #include <linux/init.h>
  27. #include <linux/poll.h>
  28. #include <linux/gfp.h>
  29. #include <linux/fs.h>
  30. #include <linux/stacktrace.h>
  31. #include "trace.h"
  32. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  33. unsigned long __read_mostly tracing_thresh;
  34. static int tracing_disabled = 1;
  35. long
  36. ns2usecs(cycle_t nsec)
  37. {
  38. nsec += 500;
  39. do_div(nsec, 1000);
  40. return nsec;
  41. }
  42. cycle_t ftrace_now(int cpu)
  43. {
  44. return cpu_clock(cpu);
  45. }
  46. static struct trace_array global_trace;
  47. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  48. static struct trace_array max_tr;
  49. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  50. static int tracer_enabled = 1;
  51. static unsigned long trace_nr_entries = 65536UL;
  52. static struct tracer *trace_types __read_mostly;
  53. static struct tracer *current_trace __read_mostly;
  54. static int max_tracer_type_len;
  55. static DEFINE_MUTEX(trace_types_lock);
  56. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  57. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
  58. void trace_wake_up(void)
  59. {
  60. /*
  61. * The runqueue_is_locked() can fail, but this is the best we
  62. * have for now:
  63. */
  64. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  65. wake_up(&trace_wait);
  66. }
  67. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  68. static int __init set_nr_entries(char *str)
  69. {
  70. if (!str)
  71. return 0;
  72. trace_nr_entries = simple_strtoul(str, &str, 0);
  73. return 1;
  74. }
  75. __setup("trace_entries=", set_nr_entries);
  76. unsigned long nsecs_to_usecs(unsigned long nsecs)
  77. {
  78. return nsecs / 1000;
  79. }
  80. enum trace_flag_type {
  81. TRACE_FLAG_IRQS_OFF = 0x01,
  82. TRACE_FLAG_NEED_RESCHED = 0x02,
  83. TRACE_FLAG_HARDIRQ = 0x04,
  84. TRACE_FLAG_SOFTIRQ = 0x08,
  85. };
  86. #define TRACE_ITER_SYM_MASK \
  87. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  88. /* These must match the bit postions above */
  89. static const char *trace_options[] = {
  90. "print-parent",
  91. "sym-offset",
  92. "sym-addr",
  93. "verbose",
  94. "raw",
  95. "hex",
  96. "bin",
  97. "block",
  98. "stacktrace",
  99. "sched-tree",
  100. NULL
  101. };
  102. static raw_spinlock_t ftrace_max_lock =
  103. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  104. /*
  105. * Copy the new maximum trace into the separate maximum-trace
  106. * structure. (this way the maximum trace is permanently saved,
  107. * for later retrieval via /debugfs/tracing/latency_trace)
  108. */
  109. static void
  110. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  111. {
  112. struct trace_array_cpu *data = tr->data[cpu];
  113. max_tr.cpu = cpu;
  114. max_tr.time_start = data->preempt_timestamp;
  115. data = max_tr.data[cpu];
  116. data->saved_latency = tracing_max_latency;
  117. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  118. data->pid = tsk->pid;
  119. data->uid = tsk->uid;
  120. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  121. data->policy = tsk->policy;
  122. data->rt_priority = tsk->rt_priority;
  123. /* record this tasks comm */
  124. tracing_record_cmdline(current);
  125. }
  126. void check_pages(struct trace_array_cpu *data)
  127. {
  128. struct page *page, *tmp;
  129. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  130. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  131. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  132. BUG_ON(page->lru.next->prev != &page->lru);
  133. BUG_ON(page->lru.prev->next != &page->lru);
  134. }
  135. }
  136. void *head_page(struct trace_array_cpu *data)
  137. {
  138. struct page *page;
  139. check_pages(data);
  140. if (list_empty(&data->trace_pages))
  141. return NULL;
  142. page = list_entry(data->trace_pages.next, struct page, lru);
  143. BUG_ON(&page->lru == &data->trace_pages);
  144. return page_address(page);
  145. }
  146. int
  147. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  148. {
  149. int len = (PAGE_SIZE - 1) - s->len;
  150. va_list ap;
  151. int ret;
  152. if (!len)
  153. return 0;
  154. va_start(ap, fmt);
  155. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  156. va_end(ap);
  157. /* If we can't write it all, don't bother writing anything */
  158. if (ret >= len)
  159. return 0;
  160. s->len += ret;
  161. return len;
  162. }
  163. static int
  164. trace_seq_puts(struct trace_seq *s, const char *str)
  165. {
  166. int len = strlen(str);
  167. if (len > ((PAGE_SIZE - 1) - s->len))
  168. return 0;
  169. memcpy(s->buffer + s->len, str, len);
  170. s->len += len;
  171. return len;
  172. }
  173. static int
  174. trace_seq_putc(struct trace_seq *s, unsigned char c)
  175. {
  176. if (s->len >= (PAGE_SIZE - 1))
  177. return 0;
  178. s->buffer[s->len++] = c;
  179. return 1;
  180. }
  181. static int
  182. trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
  183. {
  184. if (len > ((PAGE_SIZE - 1) - s->len))
  185. return 0;
  186. memcpy(s->buffer + s->len, mem, len);
  187. s->len += len;
  188. return len;
  189. }
  190. #define HEX_CHARS 17
  191. static int
  192. trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
  193. {
  194. unsigned char hex[HEX_CHARS];
  195. unsigned char *data;
  196. unsigned char byte;
  197. int i, j;
  198. BUG_ON(len >= HEX_CHARS);
  199. data = mem;
  200. #ifdef __BIG_ENDIAN
  201. for (i = 0, j = 0; i < len; i++) {
  202. #else
  203. for (i = len-1, j = 0; i >= 0; i--) {
  204. #endif
  205. byte = data[i];
  206. hex[j] = byte & 0x0f;
  207. if (hex[j] >= 10)
  208. hex[j] += 'a' - 10;
  209. else
  210. hex[j] += '0';
  211. j++;
  212. hex[j] = byte >> 4;
  213. if (hex[j] >= 10)
  214. hex[j] += 'a' - 10;
  215. else
  216. hex[j] += '0';
  217. j++;
  218. }
  219. hex[j] = ' ';
  220. j++;
  221. return trace_seq_putmem(s, hex, j);
  222. }
  223. static void
  224. trace_seq_reset(struct trace_seq *s)
  225. {
  226. s->len = 0;
  227. }
  228. static void
  229. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  230. {
  231. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  232. s->buffer[len] = 0;
  233. seq_puts(m, s->buffer);
  234. trace_seq_reset(s);
  235. }
  236. static void
  237. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  238. {
  239. struct list_head flip_pages;
  240. INIT_LIST_HEAD(&flip_pages);
  241. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  242. sizeof(struct trace_array_cpu) -
  243. offsetof(struct trace_array_cpu, trace_head_idx));
  244. check_pages(tr1);
  245. check_pages(tr2);
  246. list_splice_init(&tr1->trace_pages, &flip_pages);
  247. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  248. list_splice_init(&flip_pages, &tr2->trace_pages);
  249. BUG_ON(!list_empty(&flip_pages));
  250. check_pages(tr1);
  251. check_pages(tr2);
  252. }
  253. void
  254. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  255. {
  256. struct trace_array_cpu *data;
  257. int i;
  258. WARN_ON_ONCE(!irqs_disabled());
  259. __raw_spin_lock(&ftrace_max_lock);
  260. /* clear out all the previous traces */
  261. for_each_possible_cpu(i) {
  262. data = tr->data[i];
  263. flip_trace(max_tr.data[i], data);
  264. tracing_reset(data);
  265. }
  266. __update_max_tr(tr, tsk, cpu);
  267. __raw_spin_unlock(&ftrace_max_lock);
  268. }
  269. /**
  270. * update_max_tr_single - only copy one trace over, and reset the rest
  271. * @tr - tracer
  272. * @tsk - task with the latency
  273. * @cpu - the cpu of the buffer to copy.
  274. */
  275. void
  276. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  277. {
  278. struct trace_array_cpu *data = tr->data[cpu];
  279. int i;
  280. WARN_ON_ONCE(!irqs_disabled());
  281. __raw_spin_lock(&ftrace_max_lock);
  282. for_each_possible_cpu(i)
  283. tracing_reset(max_tr.data[i]);
  284. flip_trace(max_tr.data[cpu], data);
  285. tracing_reset(data);
  286. __update_max_tr(tr, tsk, cpu);
  287. __raw_spin_unlock(&ftrace_max_lock);
  288. }
  289. int register_tracer(struct tracer *type)
  290. {
  291. struct tracer *t;
  292. int len;
  293. int ret = 0;
  294. if (!type->name) {
  295. pr_info("Tracer must have a name\n");
  296. return -1;
  297. }
  298. mutex_lock(&trace_types_lock);
  299. for (t = trace_types; t; t = t->next) {
  300. if (strcmp(type->name, t->name) == 0) {
  301. /* already found */
  302. pr_info("Trace %s already registered\n",
  303. type->name);
  304. ret = -1;
  305. goto out;
  306. }
  307. }
  308. #ifdef CONFIG_FTRACE_STARTUP_TEST
  309. if (type->selftest) {
  310. struct tracer *saved_tracer = current_trace;
  311. struct trace_array_cpu *data;
  312. struct trace_array *tr = &global_trace;
  313. int saved_ctrl = tr->ctrl;
  314. int i;
  315. /*
  316. * Run a selftest on this tracer.
  317. * Here we reset the trace buffer, and set the current
  318. * tracer to be this tracer. The tracer can then run some
  319. * internal tracing to verify that everything is in order.
  320. * If we fail, we do not register this tracer.
  321. */
  322. for_each_possible_cpu(i) {
  323. data = tr->data[i];
  324. if (!head_page(data))
  325. continue;
  326. tracing_reset(data);
  327. }
  328. current_trace = type;
  329. tr->ctrl = 0;
  330. /* the test is responsible for initializing and enabling */
  331. pr_info("Testing tracer %s: ", type->name);
  332. ret = type->selftest(type, tr);
  333. /* the test is responsible for resetting too */
  334. current_trace = saved_tracer;
  335. tr->ctrl = saved_ctrl;
  336. if (ret) {
  337. printk(KERN_CONT "FAILED!\n");
  338. goto out;
  339. }
  340. /* Only reset on passing, to avoid touching corrupted buffers */
  341. for_each_possible_cpu(i) {
  342. data = tr->data[i];
  343. if (!head_page(data))
  344. continue;
  345. tracing_reset(data);
  346. }
  347. printk(KERN_CONT "PASSED\n");
  348. }
  349. #endif
  350. type->next = trace_types;
  351. trace_types = type;
  352. len = strlen(type->name);
  353. if (len > max_tracer_type_len)
  354. max_tracer_type_len = len;
  355. out:
  356. mutex_unlock(&trace_types_lock);
  357. return ret;
  358. }
  359. void unregister_tracer(struct tracer *type)
  360. {
  361. struct tracer **t;
  362. int len;
  363. mutex_lock(&trace_types_lock);
  364. for (t = &trace_types; *t; t = &(*t)->next) {
  365. if (*t == type)
  366. goto found;
  367. }
  368. pr_info("Trace %s not registered\n", type->name);
  369. goto out;
  370. found:
  371. *t = (*t)->next;
  372. if (strlen(type->name) != max_tracer_type_len)
  373. goto out;
  374. max_tracer_type_len = 0;
  375. for (t = &trace_types; *t; t = &(*t)->next) {
  376. len = strlen((*t)->name);
  377. if (len > max_tracer_type_len)
  378. max_tracer_type_len = len;
  379. }
  380. out:
  381. mutex_unlock(&trace_types_lock);
  382. }
  383. void tracing_reset(struct trace_array_cpu *data)
  384. {
  385. data->trace_idx = 0;
  386. data->trace_head = data->trace_tail = head_page(data);
  387. data->trace_head_idx = 0;
  388. data->trace_tail_idx = 0;
  389. }
  390. #define SAVED_CMDLINES 128
  391. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  392. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  393. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  394. static int cmdline_idx;
  395. static DEFINE_SPINLOCK(trace_cmdline_lock);
  396. atomic_t trace_record_cmdline_disabled;
  397. static void trace_init_cmdlines(void)
  398. {
  399. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  400. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  401. cmdline_idx = 0;
  402. }
  403. void trace_stop_cmdline_recording(void);
  404. static void trace_save_cmdline(struct task_struct *tsk)
  405. {
  406. unsigned map;
  407. unsigned idx;
  408. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  409. return;
  410. /*
  411. * It's not the end of the world if we don't get
  412. * the lock, but we also don't want to spin
  413. * nor do we want to disable interrupts,
  414. * so if we miss here, then better luck next time.
  415. */
  416. if (!spin_trylock(&trace_cmdline_lock))
  417. return;
  418. idx = map_pid_to_cmdline[tsk->pid];
  419. if (idx >= SAVED_CMDLINES) {
  420. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  421. map = map_cmdline_to_pid[idx];
  422. if (map <= PID_MAX_DEFAULT)
  423. map_pid_to_cmdline[map] = (unsigned)-1;
  424. map_pid_to_cmdline[tsk->pid] = idx;
  425. cmdline_idx = idx;
  426. }
  427. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  428. spin_unlock(&trace_cmdline_lock);
  429. }
  430. static char *trace_find_cmdline(int pid)
  431. {
  432. char *cmdline = "<...>";
  433. unsigned map;
  434. if (!pid)
  435. return "<idle>";
  436. if (pid > PID_MAX_DEFAULT)
  437. goto out;
  438. map = map_pid_to_cmdline[pid];
  439. if (map >= SAVED_CMDLINES)
  440. goto out;
  441. cmdline = saved_cmdlines[map];
  442. out:
  443. return cmdline;
  444. }
  445. void tracing_record_cmdline(struct task_struct *tsk)
  446. {
  447. if (atomic_read(&trace_record_cmdline_disabled))
  448. return;
  449. trace_save_cmdline(tsk);
  450. }
  451. static inline struct list_head *
  452. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  453. {
  454. /*
  455. * Roundrobin - but skip the head (which is not a real page):
  456. */
  457. next = next->next;
  458. if (unlikely(next == &data->trace_pages))
  459. next = next->next;
  460. BUG_ON(next == &data->trace_pages);
  461. return next;
  462. }
  463. static inline void *
  464. trace_next_page(struct trace_array_cpu *data, void *addr)
  465. {
  466. struct list_head *next;
  467. struct page *page;
  468. page = virt_to_page(addr);
  469. next = trace_next_list(data, &page->lru);
  470. page = list_entry(next, struct page, lru);
  471. return page_address(page);
  472. }
  473. static inline struct trace_entry *
  474. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  475. {
  476. unsigned long idx, idx_next;
  477. struct trace_entry *entry;
  478. data->trace_idx++;
  479. idx = data->trace_head_idx;
  480. idx_next = idx + 1;
  481. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  482. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  483. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  484. data->trace_head = trace_next_page(data, data->trace_head);
  485. idx_next = 0;
  486. }
  487. if (data->trace_head == data->trace_tail &&
  488. idx_next == data->trace_tail_idx) {
  489. /* overrun */
  490. data->trace_tail_idx++;
  491. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  492. data->trace_tail =
  493. trace_next_page(data, data->trace_tail);
  494. data->trace_tail_idx = 0;
  495. }
  496. }
  497. data->trace_head_idx = idx_next;
  498. return entry;
  499. }
  500. static inline void
  501. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  502. {
  503. struct task_struct *tsk = current;
  504. unsigned long pc;
  505. pc = preempt_count();
  506. entry->preempt_count = pc & 0xff;
  507. entry->pid = (tsk) ? tsk->pid : 0;
  508. entry->t = ftrace_now(raw_smp_processor_id());
  509. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  510. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  511. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  512. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  513. }
  514. void
  515. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  516. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  517. {
  518. struct trace_entry *entry;
  519. unsigned long irq_flags;
  520. raw_local_irq_save(irq_flags);
  521. __raw_spin_lock(&data->lock);
  522. entry = tracing_get_trace_entry(tr, data);
  523. tracing_generic_entry_update(entry, flags);
  524. entry->type = TRACE_FN;
  525. entry->fn.ip = ip;
  526. entry->fn.parent_ip = parent_ip;
  527. __raw_spin_unlock(&data->lock);
  528. raw_local_irq_restore(irq_flags);
  529. }
  530. void
  531. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  532. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  533. {
  534. if (likely(!atomic_read(&data->disabled)))
  535. trace_function(tr, data, ip, parent_ip, flags);
  536. }
  537. void
  538. __trace_special(void *__tr, void *__data,
  539. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  540. {
  541. struct trace_array_cpu *data = __data;
  542. struct trace_array *tr = __tr;
  543. struct trace_entry *entry;
  544. unsigned long irq_flags;
  545. raw_local_irq_save(irq_flags);
  546. __raw_spin_lock(&data->lock);
  547. entry = tracing_get_trace_entry(tr, data);
  548. tracing_generic_entry_update(entry, 0);
  549. entry->type = TRACE_SPECIAL;
  550. entry->special.arg1 = arg1;
  551. entry->special.arg2 = arg2;
  552. entry->special.arg3 = arg3;
  553. __raw_spin_unlock(&data->lock);
  554. raw_local_irq_restore(irq_flags);
  555. trace_wake_up();
  556. }
  557. void __trace_stack(struct trace_array *tr,
  558. struct trace_array_cpu *data,
  559. unsigned long flags,
  560. int skip)
  561. {
  562. struct trace_entry *entry;
  563. struct stack_trace trace;
  564. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  565. return;
  566. entry = tracing_get_trace_entry(tr, data);
  567. tracing_generic_entry_update(entry, flags);
  568. entry->type = TRACE_STACK;
  569. memset(&entry->stack, 0, sizeof(entry->stack));
  570. trace.nr_entries = 0;
  571. trace.max_entries = FTRACE_STACK_ENTRIES;
  572. trace.skip = skip;
  573. trace.entries = entry->stack.caller;
  574. save_stack_trace(&trace);
  575. }
  576. void
  577. tracing_sched_switch_trace(struct trace_array *tr,
  578. struct trace_array_cpu *data,
  579. struct task_struct *prev,
  580. struct task_struct *next,
  581. unsigned long flags)
  582. {
  583. struct trace_entry *entry;
  584. unsigned long irq_flags;
  585. raw_local_irq_save(irq_flags);
  586. __raw_spin_lock(&data->lock);
  587. entry = tracing_get_trace_entry(tr, data);
  588. tracing_generic_entry_update(entry, flags);
  589. entry->type = TRACE_CTX;
  590. entry->ctx.prev_pid = prev->pid;
  591. entry->ctx.prev_prio = prev->prio;
  592. entry->ctx.prev_state = prev->state;
  593. entry->ctx.next_pid = next->pid;
  594. entry->ctx.next_prio = next->prio;
  595. entry->ctx.next_state = next->state;
  596. __trace_stack(tr, data, flags, 4);
  597. __raw_spin_unlock(&data->lock);
  598. raw_local_irq_restore(irq_flags);
  599. }
  600. void
  601. tracing_sched_wakeup_trace(struct trace_array *tr,
  602. struct trace_array_cpu *data,
  603. struct task_struct *wakee,
  604. struct task_struct *curr,
  605. unsigned long flags)
  606. {
  607. struct trace_entry *entry;
  608. unsigned long irq_flags;
  609. raw_local_irq_save(irq_flags);
  610. __raw_spin_lock(&data->lock);
  611. entry = tracing_get_trace_entry(tr, data);
  612. tracing_generic_entry_update(entry, flags);
  613. entry->type = TRACE_WAKE;
  614. entry->ctx.prev_pid = curr->pid;
  615. entry->ctx.prev_prio = curr->prio;
  616. entry->ctx.prev_state = curr->state;
  617. entry->ctx.next_pid = wakee->pid;
  618. entry->ctx.next_prio = wakee->prio;
  619. entry->ctx.next_state = wakee->state;
  620. __trace_stack(tr, data, flags, 5);
  621. __raw_spin_unlock(&data->lock);
  622. raw_local_irq_restore(irq_flags);
  623. trace_wake_up();
  624. }
  625. #ifdef CONFIG_FTRACE
  626. static void
  627. function_trace_call(unsigned long ip, unsigned long parent_ip)
  628. {
  629. struct trace_array *tr = &global_trace;
  630. struct trace_array_cpu *data;
  631. unsigned long flags;
  632. long disabled;
  633. int cpu;
  634. if (unlikely(!tracer_enabled))
  635. return;
  636. local_irq_save(flags);
  637. cpu = raw_smp_processor_id();
  638. data = tr->data[cpu];
  639. disabled = atomic_inc_return(&data->disabled);
  640. if (likely(disabled == 1))
  641. trace_function(tr, data, ip, parent_ip, flags);
  642. atomic_dec(&data->disabled);
  643. local_irq_restore(flags);
  644. }
  645. static struct ftrace_ops trace_ops __read_mostly =
  646. {
  647. .func = function_trace_call,
  648. };
  649. void tracing_start_function_trace(void)
  650. {
  651. register_ftrace_function(&trace_ops);
  652. }
  653. void tracing_stop_function_trace(void)
  654. {
  655. unregister_ftrace_function(&trace_ops);
  656. }
  657. #endif
  658. enum trace_file_type {
  659. TRACE_FILE_LAT_FMT = 1,
  660. };
  661. static struct trace_entry *
  662. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  663. struct trace_iterator *iter, int cpu)
  664. {
  665. struct page *page;
  666. struct trace_entry *array;
  667. if (iter->next_idx[cpu] >= tr->entries ||
  668. iter->next_idx[cpu] >= data->trace_idx ||
  669. (data->trace_head == data->trace_tail &&
  670. data->trace_head_idx == data->trace_tail_idx))
  671. return NULL;
  672. if (!iter->next_page[cpu]) {
  673. /* Initialize the iterator for this cpu trace buffer */
  674. WARN_ON(!data->trace_tail);
  675. page = virt_to_page(data->trace_tail);
  676. iter->next_page[cpu] = &page->lru;
  677. iter->next_page_idx[cpu] = data->trace_tail_idx;
  678. }
  679. page = list_entry(iter->next_page[cpu], struct page, lru);
  680. BUG_ON(&data->trace_pages == &page->lru);
  681. array = page_address(page);
  682. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  683. return &array[iter->next_page_idx[cpu]];
  684. }
  685. static struct trace_entry *
  686. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  687. {
  688. struct trace_array *tr = iter->tr;
  689. struct trace_entry *ent, *next = NULL;
  690. int next_cpu = -1;
  691. int cpu;
  692. for_each_possible_cpu(cpu) {
  693. if (!head_page(tr->data[cpu]))
  694. continue;
  695. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  696. /*
  697. * Pick the entry with the smallest timestamp:
  698. */
  699. if (ent && (!next || ent->t < next->t)) {
  700. next = ent;
  701. next_cpu = cpu;
  702. }
  703. }
  704. if (ent_cpu)
  705. *ent_cpu = next_cpu;
  706. return next;
  707. }
  708. static void trace_iterator_increment(struct trace_iterator *iter)
  709. {
  710. iter->idx++;
  711. iter->next_idx[iter->cpu]++;
  712. iter->next_page_idx[iter->cpu]++;
  713. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  714. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  715. iter->next_page_idx[iter->cpu] = 0;
  716. iter->next_page[iter->cpu] =
  717. trace_next_list(data, iter->next_page[iter->cpu]);
  718. }
  719. }
  720. static void trace_consume(struct trace_iterator *iter)
  721. {
  722. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  723. data->trace_tail_idx++;
  724. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  725. data->trace_tail = trace_next_page(data, data->trace_tail);
  726. data->trace_tail_idx = 0;
  727. }
  728. /* Check if we empty it, then reset the index */
  729. if (data->trace_head == data->trace_tail &&
  730. data->trace_head_idx == data->trace_tail_idx)
  731. data->trace_idx = 0;
  732. }
  733. static void *find_next_entry_inc(struct trace_iterator *iter)
  734. {
  735. struct trace_entry *next;
  736. int next_cpu = -1;
  737. next = find_next_entry(iter, &next_cpu);
  738. iter->prev_ent = iter->ent;
  739. iter->prev_cpu = iter->cpu;
  740. iter->ent = next;
  741. iter->cpu = next_cpu;
  742. if (next)
  743. trace_iterator_increment(iter);
  744. return next ? iter : NULL;
  745. }
  746. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  747. {
  748. struct trace_iterator *iter = m->private;
  749. void *last_ent = iter->ent;
  750. int i = (int)*pos;
  751. void *ent;
  752. (*pos)++;
  753. /* can't go backwards */
  754. if (iter->idx > i)
  755. return NULL;
  756. if (iter->idx < 0)
  757. ent = find_next_entry_inc(iter);
  758. else
  759. ent = iter;
  760. while (ent && iter->idx < i)
  761. ent = find_next_entry_inc(iter);
  762. iter->pos = *pos;
  763. if (last_ent && !ent)
  764. seq_puts(m, "\n\nvim:ft=help\n");
  765. return ent;
  766. }
  767. static void *s_start(struct seq_file *m, loff_t *pos)
  768. {
  769. struct trace_iterator *iter = m->private;
  770. void *p = NULL;
  771. loff_t l = 0;
  772. int i;
  773. mutex_lock(&trace_types_lock);
  774. if (!current_trace || current_trace != iter->trace) {
  775. mutex_unlock(&trace_types_lock);
  776. return NULL;
  777. }
  778. atomic_inc(&trace_record_cmdline_disabled);
  779. /* let the tracer grab locks here if needed */
  780. if (current_trace->start)
  781. current_trace->start(iter);
  782. if (*pos != iter->pos) {
  783. iter->ent = NULL;
  784. iter->cpu = 0;
  785. iter->idx = -1;
  786. iter->prev_ent = NULL;
  787. iter->prev_cpu = -1;
  788. for_each_possible_cpu(i) {
  789. iter->next_idx[i] = 0;
  790. iter->next_page[i] = NULL;
  791. }
  792. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  793. ;
  794. } else {
  795. l = *pos - 1;
  796. p = s_next(m, p, &l);
  797. }
  798. return p;
  799. }
  800. static void s_stop(struct seq_file *m, void *p)
  801. {
  802. struct trace_iterator *iter = m->private;
  803. atomic_dec(&trace_record_cmdline_disabled);
  804. /* let the tracer release locks here if needed */
  805. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  806. iter->trace->stop(iter);
  807. mutex_unlock(&trace_types_lock);
  808. }
  809. static int
  810. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  811. {
  812. #ifdef CONFIG_KALLSYMS
  813. char str[KSYM_SYMBOL_LEN];
  814. kallsyms_lookup(address, NULL, NULL, NULL, str);
  815. return trace_seq_printf(s, fmt, str);
  816. #endif
  817. return 1;
  818. }
  819. static int
  820. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  821. unsigned long address)
  822. {
  823. #ifdef CONFIG_KALLSYMS
  824. char str[KSYM_SYMBOL_LEN];
  825. sprint_symbol(str, address);
  826. return trace_seq_printf(s, fmt, str);
  827. #endif
  828. return 1;
  829. }
  830. #ifndef CONFIG_64BIT
  831. # define IP_FMT "%08lx"
  832. #else
  833. # define IP_FMT "%016lx"
  834. #endif
  835. static int
  836. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  837. {
  838. int ret;
  839. if (!ip)
  840. return trace_seq_printf(s, "0");
  841. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  842. ret = seq_print_sym_offset(s, "%s", ip);
  843. else
  844. ret = seq_print_sym_short(s, "%s", ip);
  845. if (!ret)
  846. return 0;
  847. if (sym_flags & TRACE_ITER_SYM_ADDR)
  848. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  849. return ret;
  850. }
  851. static void print_lat_help_header(struct seq_file *m)
  852. {
  853. seq_puts(m, "# _------=> CPU# \n");
  854. seq_puts(m, "# / _-----=> irqs-off \n");
  855. seq_puts(m, "# | / _----=> need-resched \n");
  856. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  857. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  858. seq_puts(m, "# |||| / \n");
  859. seq_puts(m, "# ||||| delay \n");
  860. seq_puts(m, "# cmd pid ||||| time | caller \n");
  861. seq_puts(m, "# \\ / ||||| \\ | / \n");
  862. }
  863. static void print_func_help_header(struct seq_file *m)
  864. {
  865. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  866. seq_puts(m, "# | | | | |\n");
  867. }
  868. static void
  869. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  870. {
  871. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  872. struct trace_array *tr = iter->tr;
  873. struct trace_array_cpu *data = tr->data[tr->cpu];
  874. struct tracer *type = current_trace;
  875. unsigned long total = 0;
  876. unsigned long entries = 0;
  877. int cpu;
  878. const char *name = "preemption";
  879. if (type)
  880. name = type->name;
  881. for_each_possible_cpu(cpu) {
  882. if (head_page(tr->data[cpu])) {
  883. total += tr->data[cpu]->trace_idx;
  884. if (tr->data[cpu]->trace_idx > tr->entries)
  885. entries += tr->entries;
  886. else
  887. entries += tr->data[cpu]->trace_idx;
  888. }
  889. }
  890. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  891. name, UTS_RELEASE);
  892. seq_puts(m, "-----------------------------------"
  893. "---------------------------------\n");
  894. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  895. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  896. nsecs_to_usecs(data->saved_latency),
  897. entries,
  898. total,
  899. tr->cpu,
  900. #if defined(CONFIG_PREEMPT_NONE)
  901. "server",
  902. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  903. "desktop",
  904. #elif defined(CONFIG_PREEMPT_DESKTOP)
  905. "preempt",
  906. #else
  907. "unknown",
  908. #endif
  909. /* These are reserved for later use */
  910. 0, 0, 0, 0);
  911. #ifdef CONFIG_SMP
  912. seq_printf(m, " #P:%d)\n", num_online_cpus());
  913. #else
  914. seq_puts(m, ")\n");
  915. #endif
  916. seq_puts(m, " -----------------\n");
  917. seq_printf(m, " | task: %.16s-%d "
  918. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  919. data->comm, data->pid, data->uid, data->nice,
  920. data->policy, data->rt_priority);
  921. seq_puts(m, " -----------------\n");
  922. if (data->critical_start) {
  923. seq_puts(m, " => started at: ");
  924. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  925. trace_print_seq(m, &iter->seq);
  926. seq_puts(m, "\n => ended at: ");
  927. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  928. trace_print_seq(m, &iter->seq);
  929. seq_puts(m, "\n");
  930. }
  931. seq_puts(m, "\n");
  932. }
  933. static void
  934. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  935. {
  936. int hardirq, softirq;
  937. char *comm;
  938. comm = trace_find_cmdline(entry->pid);
  939. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  940. trace_seq_printf(s, "%d", cpu);
  941. trace_seq_printf(s, "%c%c",
  942. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  943. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  944. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  945. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  946. if (hardirq && softirq)
  947. trace_seq_putc(s, 'H');
  948. else {
  949. if (hardirq)
  950. trace_seq_putc(s, 'h');
  951. else {
  952. if (softirq)
  953. trace_seq_putc(s, 's');
  954. else
  955. trace_seq_putc(s, '.');
  956. }
  957. }
  958. if (entry->preempt_count)
  959. trace_seq_printf(s, "%x", entry->preempt_count);
  960. else
  961. trace_seq_puts(s, ".");
  962. }
  963. unsigned long preempt_mark_thresh = 100;
  964. static void
  965. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  966. unsigned long rel_usecs)
  967. {
  968. trace_seq_printf(s, " %4lldus", abs_usecs);
  969. if (rel_usecs > preempt_mark_thresh)
  970. trace_seq_puts(s, "!: ");
  971. else if (rel_usecs > 1)
  972. trace_seq_puts(s, "+: ");
  973. else
  974. trace_seq_puts(s, " : ");
  975. }
  976. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  977. static int
  978. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  979. {
  980. struct trace_seq *s = &iter->seq;
  981. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  982. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  983. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  984. struct trace_entry *entry = iter->ent;
  985. unsigned long abs_usecs;
  986. unsigned long rel_usecs;
  987. char *comm;
  988. int S, T;
  989. int i;
  990. unsigned state;
  991. if (!next_entry)
  992. next_entry = entry;
  993. rel_usecs = ns2usecs(next_entry->t - entry->t);
  994. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  995. if (verbose) {
  996. comm = trace_find_cmdline(entry->pid);
  997. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  998. " %ld.%03ldms (+%ld.%03ldms): ",
  999. comm,
  1000. entry->pid, cpu, entry->flags,
  1001. entry->preempt_count, trace_idx,
  1002. ns2usecs(entry->t),
  1003. abs_usecs/1000,
  1004. abs_usecs % 1000, rel_usecs/1000,
  1005. rel_usecs % 1000);
  1006. } else {
  1007. lat_print_generic(s, entry, cpu);
  1008. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1009. }
  1010. switch (entry->type) {
  1011. case TRACE_FN:
  1012. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1013. trace_seq_puts(s, " (");
  1014. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1015. trace_seq_puts(s, ")\n");
  1016. break;
  1017. case TRACE_CTX:
  1018. case TRACE_WAKE:
  1019. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1020. state_to_char[entry->ctx.next_state] : 'X';
  1021. state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
  1022. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1023. comm = trace_find_cmdline(entry->ctx.next_pid);
  1024. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1025. entry->ctx.prev_pid,
  1026. entry->ctx.prev_prio,
  1027. S, entry->type == TRACE_CTX ? "==>" : " +",
  1028. entry->ctx.next_pid,
  1029. entry->ctx.next_prio,
  1030. T, comm);
  1031. break;
  1032. case TRACE_SPECIAL:
  1033. trace_seq_printf(s, "# %ld %ld %ld\n",
  1034. entry->special.arg1,
  1035. entry->special.arg2,
  1036. entry->special.arg3);
  1037. break;
  1038. case TRACE_STACK:
  1039. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1040. if (i)
  1041. trace_seq_puts(s, " <= ");
  1042. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1043. }
  1044. trace_seq_puts(s, "\n");
  1045. break;
  1046. default:
  1047. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1048. }
  1049. return 1;
  1050. }
  1051. static int print_trace_fmt(struct trace_iterator *iter)
  1052. {
  1053. struct trace_seq *s = &iter->seq;
  1054. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1055. struct trace_entry *entry;
  1056. unsigned long usec_rem;
  1057. unsigned long long t;
  1058. unsigned long secs;
  1059. char *comm;
  1060. int ret;
  1061. int S, T;
  1062. int i;
  1063. entry = iter->ent;
  1064. comm = trace_find_cmdline(iter->ent->pid);
  1065. t = ns2usecs(entry->t);
  1066. usec_rem = do_div(t, 1000000ULL);
  1067. secs = (unsigned long)t;
  1068. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1069. if (!ret)
  1070. return 0;
  1071. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1072. if (!ret)
  1073. return 0;
  1074. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1075. if (!ret)
  1076. return 0;
  1077. switch (entry->type) {
  1078. case TRACE_FN:
  1079. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1080. if (!ret)
  1081. return 0;
  1082. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1083. entry->fn.parent_ip) {
  1084. ret = trace_seq_printf(s, " <-");
  1085. if (!ret)
  1086. return 0;
  1087. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1088. sym_flags);
  1089. if (!ret)
  1090. return 0;
  1091. }
  1092. ret = trace_seq_printf(s, "\n");
  1093. if (!ret)
  1094. return 0;
  1095. break;
  1096. case TRACE_CTX:
  1097. case TRACE_WAKE:
  1098. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1099. state_to_char[entry->ctx.prev_state] : 'X';
  1100. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1101. state_to_char[entry->ctx.next_state] : 'X';
  1102. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1103. entry->ctx.prev_pid,
  1104. entry->ctx.prev_prio,
  1105. S,
  1106. entry->type == TRACE_CTX ? "==>" : " +",
  1107. entry->ctx.next_pid,
  1108. entry->ctx.next_prio,
  1109. T);
  1110. if (!ret)
  1111. return 0;
  1112. break;
  1113. case TRACE_SPECIAL:
  1114. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1115. entry->special.arg1,
  1116. entry->special.arg2,
  1117. entry->special.arg3);
  1118. if (!ret)
  1119. return 0;
  1120. break;
  1121. case TRACE_STACK:
  1122. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1123. if (i) {
  1124. ret = trace_seq_puts(s, " <= ");
  1125. if (!ret)
  1126. return 0;
  1127. }
  1128. ret = seq_print_ip_sym(s, entry->stack.caller[i],
  1129. sym_flags);
  1130. if (!ret)
  1131. return 0;
  1132. }
  1133. ret = trace_seq_puts(s, "\n");
  1134. if (!ret)
  1135. return 0;
  1136. break;
  1137. }
  1138. return 1;
  1139. }
  1140. static int print_raw_fmt(struct trace_iterator *iter)
  1141. {
  1142. struct trace_seq *s = &iter->seq;
  1143. struct trace_entry *entry;
  1144. int ret;
  1145. int S, T;
  1146. entry = iter->ent;
  1147. ret = trace_seq_printf(s, "%d %d %llu ",
  1148. entry->pid, iter->cpu, entry->t);
  1149. if (!ret)
  1150. return 0;
  1151. switch (entry->type) {
  1152. case TRACE_FN:
  1153. ret = trace_seq_printf(s, "%x %x\n",
  1154. entry->fn.ip, entry->fn.parent_ip);
  1155. if (!ret)
  1156. return 0;
  1157. break;
  1158. case TRACE_CTX:
  1159. case TRACE_WAKE:
  1160. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1161. state_to_char[entry->ctx.prev_state] : 'X';
  1162. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1163. state_to_char[entry->ctx.next_state] : 'X';
  1164. if (entry->type == TRACE_WAKE)
  1165. S = '+';
  1166. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1167. entry->ctx.prev_pid,
  1168. entry->ctx.prev_prio,
  1169. S,
  1170. entry->ctx.next_pid,
  1171. entry->ctx.next_prio,
  1172. T);
  1173. if (!ret)
  1174. return 0;
  1175. break;
  1176. case TRACE_SPECIAL:
  1177. case TRACE_STACK:
  1178. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1179. entry->special.arg1,
  1180. entry->special.arg2,
  1181. entry->special.arg3);
  1182. if (!ret)
  1183. return 0;
  1184. break;
  1185. }
  1186. return 1;
  1187. }
  1188. #define SEQ_PUT_FIELD_RET(s, x) \
  1189. do { \
  1190. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1191. return 0; \
  1192. } while (0)
  1193. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1194. do { \
  1195. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1196. return 0; \
  1197. } while (0)
  1198. static int print_hex_fmt(struct trace_iterator *iter)
  1199. {
  1200. struct trace_seq *s = &iter->seq;
  1201. unsigned char newline = '\n';
  1202. struct trace_entry *entry;
  1203. int S, T;
  1204. entry = iter->ent;
  1205. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1206. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1207. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1208. switch (entry->type) {
  1209. case TRACE_FN:
  1210. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1211. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1212. break;
  1213. case TRACE_CTX:
  1214. case TRACE_WAKE:
  1215. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1216. state_to_char[entry->ctx.prev_state] : 'X';
  1217. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1218. state_to_char[entry->ctx.next_state] : 'X';
  1219. if (entry->type == TRACE_WAKE)
  1220. S = '+';
  1221. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1222. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1223. SEQ_PUT_HEX_FIELD_RET(s, S);
  1224. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1225. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1226. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1227. SEQ_PUT_HEX_FIELD_RET(s, T);
  1228. break;
  1229. case TRACE_SPECIAL:
  1230. case TRACE_STACK:
  1231. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1232. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1233. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1234. break;
  1235. }
  1236. SEQ_PUT_FIELD_RET(s, newline);
  1237. return 1;
  1238. }
  1239. static int print_bin_fmt(struct trace_iterator *iter)
  1240. {
  1241. struct trace_seq *s = &iter->seq;
  1242. struct trace_entry *entry;
  1243. entry = iter->ent;
  1244. SEQ_PUT_FIELD_RET(s, entry->pid);
  1245. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1246. SEQ_PUT_FIELD_RET(s, entry->t);
  1247. switch (entry->type) {
  1248. case TRACE_FN:
  1249. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1250. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1251. break;
  1252. case TRACE_CTX:
  1253. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1254. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1255. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1256. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1257. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1258. SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
  1259. break;
  1260. case TRACE_SPECIAL:
  1261. case TRACE_STACK:
  1262. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1263. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1264. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1265. break;
  1266. }
  1267. return 1;
  1268. }
  1269. static int trace_empty(struct trace_iterator *iter)
  1270. {
  1271. struct trace_array_cpu *data;
  1272. int cpu;
  1273. for_each_possible_cpu(cpu) {
  1274. data = iter->tr->data[cpu];
  1275. if (head_page(data) && data->trace_idx &&
  1276. (data->trace_tail != data->trace_head ||
  1277. data->trace_tail_idx != data->trace_head_idx))
  1278. return 0;
  1279. }
  1280. return 1;
  1281. }
  1282. static int print_trace_line(struct trace_iterator *iter)
  1283. {
  1284. if (iter->trace && iter->trace->print_line)
  1285. return iter->trace->print_line(iter);
  1286. if (trace_flags & TRACE_ITER_BIN)
  1287. return print_bin_fmt(iter);
  1288. if (trace_flags & TRACE_ITER_HEX)
  1289. return print_hex_fmt(iter);
  1290. if (trace_flags & TRACE_ITER_RAW)
  1291. return print_raw_fmt(iter);
  1292. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1293. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1294. return print_trace_fmt(iter);
  1295. }
  1296. static int s_show(struct seq_file *m, void *v)
  1297. {
  1298. struct trace_iterator *iter = v;
  1299. if (iter->ent == NULL) {
  1300. if (iter->tr) {
  1301. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1302. seq_puts(m, "#\n");
  1303. }
  1304. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1305. /* print nothing if the buffers are empty */
  1306. if (trace_empty(iter))
  1307. return 0;
  1308. print_trace_header(m, iter);
  1309. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1310. print_lat_help_header(m);
  1311. } else {
  1312. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1313. print_func_help_header(m);
  1314. }
  1315. } else {
  1316. print_trace_line(iter);
  1317. trace_print_seq(m, &iter->seq);
  1318. }
  1319. return 0;
  1320. }
  1321. static struct seq_operations tracer_seq_ops = {
  1322. .start = s_start,
  1323. .next = s_next,
  1324. .stop = s_stop,
  1325. .show = s_show,
  1326. };
  1327. static struct trace_iterator *
  1328. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1329. {
  1330. struct trace_iterator *iter;
  1331. if (tracing_disabled) {
  1332. *ret = -ENODEV;
  1333. return NULL;
  1334. }
  1335. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1336. if (!iter) {
  1337. *ret = -ENOMEM;
  1338. goto out;
  1339. }
  1340. mutex_lock(&trace_types_lock);
  1341. if (current_trace && current_trace->print_max)
  1342. iter->tr = &max_tr;
  1343. else
  1344. iter->tr = inode->i_private;
  1345. iter->trace = current_trace;
  1346. iter->pos = -1;
  1347. /* TODO stop tracer */
  1348. *ret = seq_open(file, &tracer_seq_ops);
  1349. if (!*ret) {
  1350. struct seq_file *m = file->private_data;
  1351. m->private = iter;
  1352. /* stop the trace while dumping */
  1353. if (iter->tr->ctrl)
  1354. tracer_enabled = 0;
  1355. if (iter->trace && iter->trace->open)
  1356. iter->trace->open(iter);
  1357. } else {
  1358. kfree(iter);
  1359. iter = NULL;
  1360. }
  1361. mutex_unlock(&trace_types_lock);
  1362. out:
  1363. return iter;
  1364. }
  1365. int tracing_open_generic(struct inode *inode, struct file *filp)
  1366. {
  1367. if (tracing_disabled)
  1368. return -ENODEV;
  1369. filp->private_data = inode->i_private;
  1370. return 0;
  1371. }
  1372. int tracing_release(struct inode *inode, struct file *file)
  1373. {
  1374. struct seq_file *m = (struct seq_file *)file->private_data;
  1375. struct trace_iterator *iter = m->private;
  1376. mutex_lock(&trace_types_lock);
  1377. if (iter->trace && iter->trace->close)
  1378. iter->trace->close(iter);
  1379. /* reenable tracing if it was previously enabled */
  1380. if (iter->tr->ctrl)
  1381. tracer_enabled = 1;
  1382. mutex_unlock(&trace_types_lock);
  1383. seq_release(inode, file);
  1384. kfree(iter);
  1385. return 0;
  1386. }
  1387. static int tracing_open(struct inode *inode, struct file *file)
  1388. {
  1389. int ret;
  1390. __tracing_open(inode, file, &ret);
  1391. return ret;
  1392. }
  1393. static int tracing_lt_open(struct inode *inode, struct file *file)
  1394. {
  1395. struct trace_iterator *iter;
  1396. int ret;
  1397. iter = __tracing_open(inode, file, &ret);
  1398. if (!ret)
  1399. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1400. return ret;
  1401. }
  1402. static void *
  1403. t_next(struct seq_file *m, void *v, loff_t *pos)
  1404. {
  1405. struct tracer *t = m->private;
  1406. (*pos)++;
  1407. if (t)
  1408. t = t->next;
  1409. m->private = t;
  1410. return t;
  1411. }
  1412. static void *t_start(struct seq_file *m, loff_t *pos)
  1413. {
  1414. struct tracer *t = m->private;
  1415. loff_t l = 0;
  1416. mutex_lock(&trace_types_lock);
  1417. for (; t && l < *pos; t = t_next(m, t, &l))
  1418. ;
  1419. return t;
  1420. }
  1421. static void t_stop(struct seq_file *m, void *p)
  1422. {
  1423. mutex_unlock(&trace_types_lock);
  1424. }
  1425. static int t_show(struct seq_file *m, void *v)
  1426. {
  1427. struct tracer *t = v;
  1428. if (!t)
  1429. return 0;
  1430. seq_printf(m, "%s", t->name);
  1431. if (t->next)
  1432. seq_putc(m, ' ');
  1433. else
  1434. seq_putc(m, '\n');
  1435. return 0;
  1436. }
  1437. static struct seq_operations show_traces_seq_ops = {
  1438. .start = t_start,
  1439. .next = t_next,
  1440. .stop = t_stop,
  1441. .show = t_show,
  1442. };
  1443. static int show_traces_open(struct inode *inode, struct file *file)
  1444. {
  1445. int ret;
  1446. if (tracing_disabled)
  1447. return -ENODEV;
  1448. ret = seq_open(file, &show_traces_seq_ops);
  1449. if (!ret) {
  1450. struct seq_file *m = file->private_data;
  1451. m->private = trace_types;
  1452. }
  1453. return ret;
  1454. }
  1455. static struct file_operations tracing_fops = {
  1456. .open = tracing_open,
  1457. .read = seq_read,
  1458. .llseek = seq_lseek,
  1459. .release = tracing_release,
  1460. };
  1461. static struct file_operations tracing_lt_fops = {
  1462. .open = tracing_lt_open,
  1463. .read = seq_read,
  1464. .llseek = seq_lseek,
  1465. .release = tracing_release,
  1466. };
  1467. static struct file_operations show_traces_fops = {
  1468. .open = show_traces_open,
  1469. .read = seq_read,
  1470. .release = seq_release,
  1471. };
  1472. /*
  1473. * Only trace on a CPU if the bitmask is set:
  1474. */
  1475. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1476. /*
  1477. * When tracing/tracing_cpu_mask is modified then this holds
  1478. * the new bitmask we are about to install:
  1479. */
  1480. static cpumask_t tracing_cpumask_new;
  1481. /*
  1482. * The tracer itself will not take this lock, but still we want
  1483. * to provide a consistent cpumask to user-space:
  1484. */
  1485. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1486. /*
  1487. * Temporary storage for the character representation of the
  1488. * CPU bitmask (and one more byte for the newline):
  1489. */
  1490. static char mask_str[NR_CPUS + 1];
  1491. static ssize_t
  1492. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1493. size_t count, loff_t *ppos)
  1494. {
  1495. int len;
  1496. mutex_lock(&tracing_cpumask_update_lock);
  1497. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1498. if (count - len < 2) {
  1499. count = -EINVAL;
  1500. goto out_err;
  1501. }
  1502. len += sprintf(mask_str + len, "\n");
  1503. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1504. out_err:
  1505. mutex_unlock(&tracing_cpumask_update_lock);
  1506. return count;
  1507. }
  1508. static ssize_t
  1509. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1510. size_t count, loff_t *ppos)
  1511. {
  1512. int err, cpu;
  1513. mutex_lock(&tracing_cpumask_update_lock);
  1514. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1515. if (err)
  1516. goto err_unlock;
  1517. raw_local_irq_disable();
  1518. __raw_spin_lock(&ftrace_max_lock);
  1519. for_each_possible_cpu(cpu) {
  1520. /*
  1521. * Increase/decrease the disabled counter if we are
  1522. * about to flip a bit in the cpumask:
  1523. */
  1524. if (cpu_isset(cpu, tracing_cpumask) &&
  1525. !cpu_isset(cpu, tracing_cpumask_new)) {
  1526. atomic_inc(&global_trace.data[cpu]->disabled);
  1527. }
  1528. if (!cpu_isset(cpu, tracing_cpumask) &&
  1529. cpu_isset(cpu, tracing_cpumask_new)) {
  1530. atomic_dec(&global_trace.data[cpu]->disabled);
  1531. }
  1532. }
  1533. __raw_spin_unlock(&ftrace_max_lock);
  1534. raw_local_irq_enable();
  1535. tracing_cpumask = tracing_cpumask_new;
  1536. mutex_unlock(&tracing_cpumask_update_lock);
  1537. return count;
  1538. err_unlock:
  1539. mutex_unlock(&tracing_cpumask_update_lock);
  1540. return err;
  1541. }
  1542. static struct file_operations tracing_cpumask_fops = {
  1543. .open = tracing_open_generic,
  1544. .read = tracing_cpumask_read,
  1545. .write = tracing_cpumask_write,
  1546. };
  1547. static ssize_t
  1548. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1549. size_t cnt, loff_t *ppos)
  1550. {
  1551. char *buf;
  1552. int r = 0;
  1553. int len = 0;
  1554. int i;
  1555. /* calulate max size */
  1556. for (i = 0; trace_options[i]; i++) {
  1557. len += strlen(trace_options[i]);
  1558. len += 3; /* "no" and space */
  1559. }
  1560. /* +2 for \n and \0 */
  1561. buf = kmalloc(len + 2, GFP_KERNEL);
  1562. if (!buf)
  1563. return -ENOMEM;
  1564. for (i = 0; trace_options[i]; i++) {
  1565. if (trace_flags & (1 << i))
  1566. r += sprintf(buf + r, "%s ", trace_options[i]);
  1567. else
  1568. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1569. }
  1570. r += sprintf(buf + r, "\n");
  1571. WARN_ON(r >= len + 2);
  1572. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1573. kfree(buf);
  1574. return r;
  1575. }
  1576. static ssize_t
  1577. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1578. size_t cnt, loff_t *ppos)
  1579. {
  1580. char buf[64];
  1581. char *cmp = buf;
  1582. int neg = 0;
  1583. int i;
  1584. if (cnt > 63)
  1585. cnt = 63;
  1586. if (copy_from_user(&buf, ubuf, cnt))
  1587. return -EFAULT;
  1588. buf[cnt] = 0;
  1589. if (strncmp(buf, "no", 2) == 0) {
  1590. neg = 1;
  1591. cmp += 2;
  1592. }
  1593. for (i = 0; trace_options[i]; i++) {
  1594. int len = strlen(trace_options[i]);
  1595. if (strncmp(cmp, trace_options[i], len) == 0) {
  1596. if (neg)
  1597. trace_flags &= ~(1 << i);
  1598. else
  1599. trace_flags |= (1 << i);
  1600. break;
  1601. }
  1602. }
  1603. /*
  1604. * If no option could be set, return an error:
  1605. */
  1606. if (!trace_options[i])
  1607. return -EINVAL;
  1608. filp->f_pos += cnt;
  1609. return cnt;
  1610. }
  1611. static struct file_operations tracing_iter_fops = {
  1612. .open = tracing_open_generic,
  1613. .read = tracing_iter_ctrl_read,
  1614. .write = tracing_iter_ctrl_write,
  1615. };
  1616. static const char readme_msg[] =
  1617. "tracing mini-HOWTO:\n\n"
  1618. "# mkdir /debug\n"
  1619. "# mount -t debugfs nodev /debug\n\n"
  1620. "# cat /debug/tracing/available_tracers\n"
  1621. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1622. "# cat /debug/tracing/current_tracer\n"
  1623. "none\n"
  1624. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1625. "# cat /debug/tracing/current_tracer\n"
  1626. "sched_switch\n"
  1627. "# cat /debug/tracing/iter_ctrl\n"
  1628. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1629. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1630. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1631. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1632. "echo 0 > /debug/tracing/tracing_enabled\n"
  1633. ;
  1634. static ssize_t
  1635. tracing_readme_read(struct file *filp, char __user *ubuf,
  1636. size_t cnt, loff_t *ppos)
  1637. {
  1638. return simple_read_from_buffer(ubuf, cnt, ppos,
  1639. readme_msg, strlen(readme_msg));
  1640. }
  1641. static struct file_operations tracing_readme_fops = {
  1642. .open = tracing_open_generic,
  1643. .read = tracing_readme_read,
  1644. };
  1645. static ssize_t
  1646. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1647. size_t cnt, loff_t *ppos)
  1648. {
  1649. struct trace_array *tr = filp->private_data;
  1650. char buf[64];
  1651. int r;
  1652. r = sprintf(buf, "%ld\n", tr->ctrl);
  1653. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1654. }
  1655. static ssize_t
  1656. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1657. size_t cnt, loff_t *ppos)
  1658. {
  1659. struct trace_array *tr = filp->private_data;
  1660. long val;
  1661. char buf[64];
  1662. if (cnt > 63)
  1663. cnt = 63;
  1664. if (copy_from_user(&buf, ubuf, cnt))
  1665. return -EFAULT;
  1666. buf[cnt] = 0;
  1667. val = simple_strtoul(buf, NULL, 10);
  1668. val = !!val;
  1669. mutex_lock(&trace_types_lock);
  1670. if (tr->ctrl ^ val) {
  1671. if (val)
  1672. tracer_enabled = 1;
  1673. else
  1674. tracer_enabled = 0;
  1675. tr->ctrl = val;
  1676. if (current_trace && current_trace->ctrl_update)
  1677. current_trace->ctrl_update(tr);
  1678. }
  1679. mutex_unlock(&trace_types_lock);
  1680. filp->f_pos += cnt;
  1681. return cnt;
  1682. }
  1683. static ssize_t
  1684. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1685. size_t cnt, loff_t *ppos)
  1686. {
  1687. char buf[max_tracer_type_len+2];
  1688. int r;
  1689. mutex_lock(&trace_types_lock);
  1690. if (current_trace)
  1691. r = sprintf(buf, "%s\n", current_trace->name);
  1692. else
  1693. r = sprintf(buf, "\n");
  1694. mutex_unlock(&trace_types_lock);
  1695. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1696. }
  1697. static ssize_t
  1698. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1699. size_t cnt, loff_t *ppos)
  1700. {
  1701. struct trace_array *tr = &global_trace;
  1702. struct tracer *t;
  1703. char buf[max_tracer_type_len+1];
  1704. int i;
  1705. if (cnt > max_tracer_type_len)
  1706. cnt = max_tracer_type_len;
  1707. if (copy_from_user(&buf, ubuf, cnt))
  1708. return -EFAULT;
  1709. buf[cnt] = 0;
  1710. /* strip ending whitespace. */
  1711. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1712. buf[i] = 0;
  1713. mutex_lock(&trace_types_lock);
  1714. for (t = trace_types; t; t = t->next) {
  1715. if (strcmp(t->name, buf) == 0)
  1716. break;
  1717. }
  1718. if (!t || t == current_trace)
  1719. goto out;
  1720. if (current_trace && current_trace->reset)
  1721. current_trace->reset(tr);
  1722. current_trace = t;
  1723. if (t->init)
  1724. t->init(tr);
  1725. out:
  1726. mutex_unlock(&trace_types_lock);
  1727. filp->f_pos += cnt;
  1728. return cnt;
  1729. }
  1730. static ssize_t
  1731. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1732. size_t cnt, loff_t *ppos)
  1733. {
  1734. unsigned long *ptr = filp->private_data;
  1735. char buf[64];
  1736. int r;
  1737. r = snprintf(buf, 64, "%ld\n",
  1738. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1739. if (r > 64)
  1740. r = 64;
  1741. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1742. }
  1743. static ssize_t
  1744. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1745. size_t cnt, loff_t *ppos)
  1746. {
  1747. long *ptr = filp->private_data;
  1748. long val;
  1749. char buf[64];
  1750. if (cnt > 63)
  1751. cnt = 63;
  1752. if (copy_from_user(&buf, ubuf, cnt))
  1753. return -EFAULT;
  1754. buf[cnt] = 0;
  1755. val = simple_strtoul(buf, NULL, 10);
  1756. *ptr = val * 1000;
  1757. return cnt;
  1758. }
  1759. static atomic_t tracing_reader;
  1760. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1761. {
  1762. struct trace_iterator *iter;
  1763. if (tracing_disabled)
  1764. return -ENODEV;
  1765. /* We only allow for reader of the pipe */
  1766. if (atomic_inc_return(&tracing_reader) != 1) {
  1767. atomic_dec(&tracing_reader);
  1768. return -EBUSY;
  1769. }
  1770. /* create a buffer to store the information to pass to userspace */
  1771. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1772. if (!iter)
  1773. return -ENOMEM;
  1774. iter->tr = &global_trace;
  1775. iter->trace = current_trace;
  1776. filp->private_data = iter;
  1777. return 0;
  1778. }
  1779. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1780. {
  1781. struct trace_iterator *iter = file->private_data;
  1782. kfree(iter);
  1783. atomic_dec(&tracing_reader);
  1784. return 0;
  1785. }
  1786. static unsigned int
  1787. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1788. {
  1789. struct trace_iterator *iter = filp->private_data;
  1790. if (trace_flags & TRACE_ITER_BLOCK) {
  1791. /*
  1792. * Always select as readable when in blocking mode
  1793. */
  1794. return POLLIN | POLLRDNORM;
  1795. }
  1796. else {
  1797. if (!trace_empty(iter))
  1798. return POLLIN | POLLRDNORM;
  1799. poll_wait(filp, &trace_wait, poll_table);
  1800. if (!trace_empty(iter))
  1801. return POLLIN | POLLRDNORM;
  1802. return 0;
  1803. }
  1804. }
  1805. /*
  1806. * Consumer reader.
  1807. */
  1808. static ssize_t
  1809. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1810. size_t cnt, loff_t *ppos)
  1811. {
  1812. struct trace_iterator *iter = filp->private_data;
  1813. struct trace_array_cpu *data;
  1814. static cpumask_t mask;
  1815. static int start;
  1816. unsigned long flags;
  1817. #ifdef CONFIG_FTRACE
  1818. int ftrace_save;
  1819. #endif
  1820. int read = 0;
  1821. int cpu;
  1822. int len;
  1823. int ret;
  1824. /* return any leftover data */
  1825. if (iter->seq.len > start) {
  1826. len = iter->seq.len - start;
  1827. if (cnt > len)
  1828. cnt = len;
  1829. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1830. if (ret)
  1831. cnt = -EFAULT;
  1832. start += len;
  1833. return cnt;
  1834. }
  1835. trace_seq_reset(&iter->seq);
  1836. start = 0;
  1837. while (trace_empty(iter)) {
  1838. /*
  1839. * This is a make-shift waitqueue. The reason we don't use
  1840. * an actual wait queue is because:
  1841. * 1) we only ever have one waiter
  1842. * 2) the tracing, traces all functions, we don't want
  1843. * the overhead of calling wake_up and friends
  1844. * (and tracing them too)
  1845. * Anyway, this is really very primitive wakeup.
  1846. */
  1847. set_current_state(TASK_INTERRUPTIBLE);
  1848. iter->tr->waiter = current;
  1849. /* sleep for one second, and try again. */
  1850. schedule_timeout(HZ);
  1851. iter->tr->waiter = NULL;
  1852. if (signal_pending(current))
  1853. return -EINTR;
  1854. /*
  1855. * We block until we read something and tracing is disabled.
  1856. * We still block if tracing is disabled, but we have never
  1857. * read anything. This allows a user to cat this file, and
  1858. * then enable tracing. But after we have read something,
  1859. * we give an EOF when tracing is again disabled.
  1860. *
  1861. * iter->pos will be 0 if we haven't read anything.
  1862. */
  1863. if (!tracer_enabled && iter->pos)
  1864. break;
  1865. continue;
  1866. }
  1867. /* stop when tracing is finished */
  1868. if (trace_empty(iter))
  1869. return 0;
  1870. if (cnt >= PAGE_SIZE)
  1871. cnt = PAGE_SIZE - 1;
  1872. memset(iter, 0, sizeof(*iter));
  1873. iter->tr = &global_trace;
  1874. iter->pos = -1;
  1875. /*
  1876. * We need to stop all tracing on all CPUS to read the
  1877. * the next buffer. This is a bit expensive, but is
  1878. * not done often. We fill all what we can read,
  1879. * and then release the locks again.
  1880. */
  1881. cpus_clear(mask);
  1882. local_irq_save(flags);
  1883. #ifdef CONFIG_FTRACE
  1884. ftrace_save = ftrace_enabled;
  1885. ftrace_enabled = 0;
  1886. #endif
  1887. smp_wmb();
  1888. for_each_possible_cpu(cpu) {
  1889. data = iter->tr->data[cpu];
  1890. if (!head_page(data) || !data->trace_idx)
  1891. continue;
  1892. atomic_inc(&data->disabled);
  1893. cpu_set(cpu, mask);
  1894. }
  1895. for_each_cpu_mask(cpu, mask) {
  1896. data = iter->tr->data[cpu];
  1897. __raw_spin_lock(&data->lock);
  1898. }
  1899. while (find_next_entry_inc(iter) != NULL) {
  1900. int len = iter->seq.len;
  1901. ret = print_trace_line(iter);
  1902. if (!ret) {
  1903. /* don't print partial lines */
  1904. iter->seq.len = len;
  1905. break;
  1906. }
  1907. trace_consume(iter);
  1908. if (iter->seq.len >= cnt)
  1909. break;
  1910. }
  1911. for_each_cpu_mask(cpu, mask) {
  1912. data = iter->tr->data[cpu];
  1913. __raw_spin_unlock(&data->lock);
  1914. }
  1915. for_each_cpu_mask(cpu, mask) {
  1916. data = iter->tr->data[cpu];
  1917. atomic_dec(&data->disabled);
  1918. }
  1919. #ifdef CONFIG_FTRACE
  1920. ftrace_enabled = ftrace_save;
  1921. #endif
  1922. local_irq_restore(flags);
  1923. /* Now copy what we have to the user */
  1924. read = iter->seq.len;
  1925. if (read > cnt)
  1926. read = cnt;
  1927. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1928. if (read < iter->seq.len)
  1929. start = read;
  1930. else
  1931. trace_seq_reset(&iter->seq);
  1932. if (ret)
  1933. read = -EFAULT;
  1934. return read;
  1935. }
  1936. static struct file_operations tracing_max_lat_fops = {
  1937. .open = tracing_open_generic,
  1938. .read = tracing_max_lat_read,
  1939. .write = tracing_max_lat_write,
  1940. };
  1941. static struct file_operations tracing_ctrl_fops = {
  1942. .open = tracing_open_generic,
  1943. .read = tracing_ctrl_read,
  1944. .write = tracing_ctrl_write,
  1945. };
  1946. static struct file_operations set_tracer_fops = {
  1947. .open = tracing_open_generic,
  1948. .read = tracing_set_trace_read,
  1949. .write = tracing_set_trace_write,
  1950. };
  1951. static struct file_operations tracing_pipe_fops = {
  1952. .open = tracing_open_pipe,
  1953. .poll = tracing_poll_pipe,
  1954. .read = tracing_read_pipe,
  1955. .release = tracing_release_pipe,
  1956. };
  1957. #ifdef CONFIG_DYNAMIC_FTRACE
  1958. static ssize_t
  1959. tracing_read_long(struct file *filp, char __user *ubuf,
  1960. size_t cnt, loff_t *ppos)
  1961. {
  1962. unsigned long *p = filp->private_data;
  1963. char buf[64];
  1964. int r;
  1965. r = sprintf(buf, "%ld\n", *p);
  1966. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1967. }
  1968. static struct file_operations tracing_read_long_fops = {
  1969. .open = tracing_open_generic,
  1970. .read = tracing_read_long,
  1971. };
  1972. #endif
  1973. static struct dentry *d_tracer;
  1974. struct dentry *tracing_init_dentry(void)
  1975. {
  1976. static int once;
  1977. if (d_tracer)
  1978. return d_tracer;
  1979. d_tracer = debugfs_create_dir("tracing", NULL);
  1980. if (!d_tracer && !once) {
  1981. once = 1;
  1982. pr_warning("Could not create debugfs directory 'tracing'\n");
  1983. return NULL;
  1984. }
  1985. return d_tracer;
  1986. }
  1987. #ifdef CONFIG_FTRACE_SELFTEST
  1988. /* Let selftest have access to static functions in this file */
  1989. #include "trace_selftest.c"
  1990. #endif
  1991. static __init void tracer_init_debugfs(void)
  1992. {
  1993. struct dentry *d_tracer;
  1994. struct dentry *entry;
  1995. d_tracer = tracing_init_dentry();
  1996. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1997. &global_trace, &tracing_ctrl_fops);
  1998. if (!entry)
  1999. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2000. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2001. NULL, &tracing_iter_fops);
  2002. if (!entry)
  2003. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2004. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2005. NULL, &tracing_cpumask_fops);
  2006. if (!entry)
  2007. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2008. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2009. &global_trace, &tracing_lt_fops);
  2010. if (!entry)
  2011. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2012. entry = debugfs_create_file("trace", 0444, d_tracer,
  2013. &global_trace, &tracing_fops);
  2014. if (!entry)
  2015. pr_warning("Could not create debugfs 'trace' entry\n");
  2016. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2017. &global_trace, &show_traces_fops);
  2018. if (!entry)
  2019. pr_warning("Could not create debugfs 'trace' entry\n");
  2020. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2021. &global_trace, &set_tracer_fops);
  2022. if (!entry)
  2023. pr_warning("Could not create debugfs 'trace' entry\n");
  2024. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2025. &tracing_max_latency,
  2026. &tracing_max_lat_fops);
  2027. if (!entry)
  2028. pr_warning("Could not create debugfs "
  2029. "'tracing_max_latency' entry\n");
  2030. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2031. &tracing_thresh, &tracing_max_lat_fops);
  2032. if (!entry)
  2033. pr_warning("Could not create debugfs "
  2034. "'tracing_threash' entry\n");
  2035. entry = debugfs_create_file("README", 0644, d_tracer,
  2036. NULL, &tracing_readme_fops);
  2037. if (!entry)
  2038. pr_warning("Could not create debugfs 'README' entry\n");
  2039. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2040. NULL, &tracing_pipe_fops);
  2041. if (!entry)
  2042. pr_warning("Could not create debugfs "
  2043. "'tracing_threash' entry\n");
  2044. #ifdef CONFIG_DYNAMIC_FTRACE
  2045. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2046. &ftrace_update_tot_cnt,
  2047. &tracing_read_long_fops);
  2048. if (!entry)
  2049. pr_warning("Could not create debugfs "
  2050. "'dyn_ftrace_total_info' entry\n");
  2051. #endif
  2052. }
  2053. /* dummy trace to disable tracing */
  2054. static struct tracer no_tracer __read_mostly =
  2055. {
  2056. .name = "none",
  2057. };
  2058. static int trace_alloc_page(void)
  2059. {
  2060. struct trace_array_cpu *data;
  2061. struct page *page, *tmp;
  2062. LIST_HEAD(pages);
  2063. void *array;
  2064. int i;
  2065. /* first allocate a page for each CPU */
  2066. for_each_possible_cpu(i) {
  2067. array = (void *)__get_free_page(GFP_KERNEL);
  2068. if (array == NULL) {
  2069. printk(KERN_ERR "tracer: failed to allocate page"
  2070. "for trace buffer!\n");
  2071. goto free_pages;
  2072. }
  2073. page = virt_to_page(array);
  2074. list_add(&page->lru, &pages);
  2075. /* Only allocate if we are actually using the max trace */
  2076. #ifdef CONFIG_TRACER_MAX_TRACE
  2077. array = (void *)__get_free_page(GFP_KERNEL);
  2078. if (array == NULL) {
  2079. printk(KERN_ERR "tracer: failed to allocate page"
  2080. "for trace buffer!\n");
  2081. goto free_pages;
  2082. }
  2083. page = virt_to_page(array);
  2084. list_add(&page->lru, &pages);
  2085. #endif
  2086. }
  2087. /* Now that we successfully allocate a page per CPU, add them */
  2088. for_each_possible_cpu(i) {
  2089. data = global_trace.data[i];
  2090. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2091. page = list_entry(pages.next, struct page, lru);
  2092. list_del_init(&page->lru);
  2093. list_add_tail(&page->lru, &data->trace_pages);
  2094. ClearPageLRU(page);
  2095. #ifdef CONFIG_TRACER_MAX_TRACE
  2096. data = max_tr.data[i];
  2097. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2098. page = list_entry(pages.next, struct page, lru);
  2099. list_del_init(&page->lru);
  2100. list_add_tail(&page->lru, &data->trace_pages);
  2101. SetPageLRU(page);
  2102. #endif
  2103. }
  2104. global_trace.entries += ENTRIES_PER_PAGE;
  2105. return 0;
  2106. free_pages:
  2107. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2108. list_del_init(&page->lru);
  2109. __free_page(page);
  2110. }
  2111. return -ENOMEM;
  2112. }
  2113. __init static int tracer_alloc_buffers(void)
  2114. {
  2115. struct trace_array_cpu *data;
  2116. void *array;
  2117. struct page *page;
  2118. int pages = 0;
  2119. int ret = -ENOMEM;
  2120. int i;
  2121. global_trace.ctrl = tracer_enabled;
  2122. /* Allocate the first page for all buffers */
  2123. for_each_possible_cpu(i) {
  2124. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2125. max_tr.data[i] = &per_cpu(max_data, i);
  2126. array = (void *)__get_free_page(GFP_KERNEL);
  2127. if (array == NULL) {
  2128. printk(KERN_ERR "tracer: failed to allocate page"
  2129. "for trace buffer!\n");
  2130. goto free_buffers;
  2131. }
  2132. /* set the array to the list */
  2133. INIT_LIST_HEAD(&data->trace_pages);
  2134. page = virt_to_page(array);
  2135. list_add(&page->lru, &data->trace_pages);
  2136. /* use the LRU flag to differentiate the two buffers */
  2137. ClearPageLRU(page);
  2138. /* Only allocate if we are actually using the max trace */
  2139. #ifdef CONFIG_TRACER_MAX_TRACE
  2140. array = (void *)__get_free_page(GFP_KERNEL);
  2141. if (array == NULL) {
  2142. printk(KERN_ERR "tracer: failed to allocate page"
  2143. "for trace buffer!\n");
  2144. goto free_buffers;
  2145. }
  2146. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2147. page = virt_to_page(array);
  2148. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2149. SetPageLRU(page);
  2150. #endif
  2151. }
  2152. /*
  2153. * Since we allocate by orders of pages, we may be able to
  2154. * round up a bit.
  2155. */
  2156. global_trace.entries = ENTRIES_PER_PAGE;
  2157. pages++;
  2158. while (global_trace.entries < trace_nr_entries) {
  2159. if (trace_alloc_page())
  2160. break;
  2161. pages++;
  2162. }
  2163. max_tr.entries = global_trace.entries;
  2164. pr_info("tracer: %d pages allocated for %ld",
  2165. pages, trace_nr_entries);
  2166. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2167. pr_info(" actual entries %ld\n", global_trace.entries);
  2168. tracer_init_debugfs();
  2169. trace_init_cmdlines();
  2170. register_tracer(&no_tracer);
  2171. current_trace = &no_tracer;
  2172. /* All seems OK, enable tracing */
  2173. tracing_disabled = 0;
  2174. return 0;
  2175. free_buffers:
  2176. for (i-- ; i >= 0; i--) {
  2177. struct page *page, *tmp;
  2178. struct trace_array_cpu *data = global_trace.data[i];
  2179. if (data) {
  2180. list_for_each_entry_safe(page, tmp,
  2181. &data->trace_pages, lru) {
  2182. list_del_init(&page->lru);
  2183. __free_page(page);
  2184. }
  2185. }
  2186. #ifdef CONFIG_TRACER_MAX_TRACE
  2187. data = max_tr.data[i];
  2188. if (data) {
  2189. list_for_each_entry_safe(page, tmp,
  2190. &data->trace_pages, lru) {
  2191. list_del_init(&page->lru);
  2192. __free_page(page);
  2193. }
  2194. }
  2195. #endif
  2196. }
  2197. return ret;
  2198. }
  2199. fs_initcall(tracer_alloc_buffers);