trace.c 59 KB

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