trace-event-parse.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. /*
  2. * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License (not later!)
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. *
  21. * The parts for function graph printing was taken and modified from the
  22. * Linux Kernel that were written by Frederic Weisbecker.
  23. */
  24. #define _GNU_SOURCE
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #undef _GNU_SOURCE
  31. #include "../perf.h"
  32. #include "util.h"
  33. #include "trace-event.h"
  34. int header_page_ts_offset;
  35. int header_page_ts_size;
  36. int header_page_size_offset;
  37. int header_page_size_size;
  38. int header_page_data_offset;
  39. int header_page_data_size;
  40. static char *input_buf;
  41. static unsigned long long input_buf_ptr;
  42. static unsigned long long input_buf_siz;
  43. static int cpus;
  44. static int long_size;
  45. static void init_input_buf(char *buf, unsigned long long size)
  46. {
  47. input_buf = buf;
  48. input_buf_siz = size;
  49. input_buf_ptr = 0;
  50. }
  51. struct cmdline {
  52. char *comm;
  53. int pid;
  54. };
  55. static struct cmdline *cmdlines;
  56. static int cmdline_count;
  57. static int cmdline_cmp(const void *a, const void *b)
  58. {
  59. const struct cmdline *ca = a;
  60. const struct cmdline *cb = b;
  61. if (ca->pid < cb->pid)
  62. return -1;
  63. if (ca->pid > cb->pid)
  64. return 1;
  65. return 0;
  66. }
  67. void parse_cmdlines(char *file, int size __unused)
  68. {
  69. struct cmdline_list {
  70. struct cmdline_list *next;
  71. char *comm;
  72. int pid;
  73. } *list = NULL, *item;
  74. char *line;
  75. char *next = NULL;
  76. int i;
  77. line = strtok_r(file, "\n", &next);
  78. while (line) {
  79. item = malloc_or_die(sizeof(*item));
  80. sscanf(line, "%d %as", &item->pid,
  81. (float *)&item->comm); /* workaround gcc warning */
  82. item->next = list;
  83. list = item;
  84. line = strtok_r(NULL, "\n", &next);
  85. cmdline_count++;
  86. }
  87. cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
  88. i = 0;
  89. while (list) {
  90. cmdlines[i].pid = list->pid;
  91. cmdlines[i].comm = list->comm;
  92. i++;
  93. item = list;
  94. list = list->next;
  95. free(item);
  96. }
  97. qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  98. }
  99. static struct func_map {
  100. unsigned long long addr;
  101. char *func;
  102. char *mod;
  103. } *func_list;
  104. static unsigned int func_count;
  105. static int func_cmp(const void *a, const void *b)
  106. {
  107. const struct func_map *fa = a;
  108. const struct func_map *fb = b;
  109. if (fa->addr < fb->addr)
  110. return -1;
  111. if (fa->addr > fb->addr)
  112. return 1;
  113. return 0;
  114. }
  115. void parse_proc_kallsyms(char *file, unsigned int size __unused)
  116. {
  117. struct func_list {
  118. struct func_list *next;
  119. unsigned long long addr;
  120. char *func;
  121. char *mod;
  122. } *list = NULL, *item;
  123. char *line;
  124. char *next = NULL;
  125. char *addr_str;
  126. char ch;
  127. int ret;
  128. int i;
  129. line = strtok_r(file, "\n", &next);
  130. while (line) {
  131. item = malloc_or_die(sizeof(*item));
  132. item->mod = NULL;
  133. ret = sscanf(line, "%as %c %as\t[%as",
  134. (float *)&addr_str, /* workaround gcc warning */
  135. &ch,
  136. (float *)&item->func,
  137. (float *)&item->mod);
  138. item->addr = strtoull(addr_str, NULL, 16);
  139. free(addr_str);
  140. /* truncate the extra ']' */
  141. if (item->mod)
  142. item->mod[strlen(item->mod) - 1] = 0;
  143. item->next = list;
  144. list = item;
  145. line = strtok_r(NULL, "\n", &next);
  146. func_count++;
  147. }
  148. func_list = malloc_or_die(sizeof(*func_list) * func_count + 1);
  149. i = 0;
  150. while (list) {
  151. func_list[i].func = list->func;
  152. func_list[i].addr = list->addr;
  153. func_list[i].mod = list->mod;
  154. i++;
  155. item = list;
  156. list = list->next;
  157. free(item);
  158. }
  159. qsort(func_list, func_count, sizeof(*func_list), func_cmp);
  160. /*
  161. * Add a special record at the end.
  162. */
  163. func_list[func_count].func = NULL;
  164. func_list[func_count].addr = 0;
  165. func_list[func_count].mod = NULL;
  166. }
  167. /*
  168. * We are searching for a record in between, not an exact
  169. * match.
  170. */
  171. static int func_bcmp(const void *a, const void *b)
  172. {
  173. const struct func_map *fa = a;
  174. const struct func_map *fb = b;
  175. if ((fa->addr == fb->addr) ||
  176. (fa->addr > fb->addr &&
  177. fa->addr < (fb+1)->addr))
  178. return 0;
  179. if (fa->addr < fb->addr)
  180. return -1;
  181. return 1;
  182. }
  183. static struct func_map *find_func(unsigned long long addr)
  184. {
  185. struct func_map *func;
  186. struct func_map key;
  187. key.addr = addr;
  188. func = bsearch(&key, func_list, func_count, sizeof(*func_list),
  189. func_bcmp);
  190. return func;
  191. }
  192. void print_funcs(void)
  193. {
  194. int i;
  195. for (i = 0; i < (int)func_count; i++) {
  196. printf("%016llx %s",
  197. func_list[i].addr,
  198. func_list[i].func);
  199. if (func_list[i].mod)
  200. printf(" [%s]\n", func_list[i].mod);
  201. else
  202. printf("\n");
  203. }
  204. }
  205. static struct printk_map {
  206. unsigned long long addr;
  207. char *printk;
  208. } *printk_list;
  209. static unsigned int printk_count;
  210. static int printk_cmp(const void *a, const void *b)
  211. {
  212. const struct func_map *fa = a;
  213. const struct func_map *fb = b;
  214. if (fa->addr < fb->addr)
  215. return -1;
  216. if (fa->addr > fb->addr)
  217. return 1;
  218. return 0;
  219. }
  220. static struct printk_map *find_printk(unsigned long long addr)
  221. {
  222. struct printk_map *printk;
  223. struct printk_map key;
  224. key.addr = addr;
  225. printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
  226. printk_cmp);
  227. return printk;
  228. }
  229. void parse_ftrace_printk(char *file, unsigned int size __unused)
  230. {
  231. struct printk_list {
  232. struct printk_list *next;
  233. unsigned long long addr;
  234. char *printk;
  235. } *list = NULL, *item;
  236. char *line;
  237. char *next = NULL;
  238. char *addr_str;
  239. int ret;
  240. int i;
  241. line = strtok_r(file, "\n", &next);
  242. while (line) {
  243. item = malloc_or_die(sizeof(*item));
  244. ret = sscanf(line, "%as : %as",
  245. (float *)&addr_str, /* workaround gcc warning */
  246. (float *)&item->printk);
  247. item->addr = strtoull(addr_str, NULL, 16);
  248. free(addr_str);
  249. item->next = list;
  250. list = item;
  251. line = strtok_r(NULL, "\n", &next);
  252. printk_count++;
  253. }
  254. printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
  255. i = 0;
  256. while (list) {
  257. printk_list[i].printk = list->printk;
  258. printk_list[i].addr = list->addr;
  259. i++;
  260. item = list;
  261. list = list->next;
  262. free(item);
  263. }
  264. qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
  265. }
  266. void print_printk(void)
  267. {
  268. int i;
  269. for (i = 0; i < (int)printk_count; i++) {
  270. printf("%016llx %s\n",
  271. printk_list[i].addr,
  272. printk_list[i].printk);
  273. }
  274. }
  275. static struct event *alloc_event(void)
  276. {
  277. struct event *event;
  278. event = malloc_or_die(sizeof(*event));
  279. memset(event, 0, sizeof(*event));
  280. return event;
  281. }
  282. enum event_type {
  283. EVENT_ERROR,
  284. EVENT_NONE,
  285. EVENT_SPACE,
  286. EVENT_NEWLINE,
  287. EVENT_OP,
  288. EVENT_DELIM,
  289. EVENT_ITEM,
  290. EVENT_DQUOTE,
  291. EVENT_SQUOTE,
  292. };
  293. static struct event *event_list;
  294. static void add_event(struct event *event)
  295. {
  296. event->next = event_list;
  297. event_list = event;
  298. }
  299. static int event_item_type(enum event_type type)
  300. {
  301. switch (type) {
  302. case EVENT_ITEM ... EVENT_SQUOTE:
  303. return 1;
  304. case EVENT_ERROR ... EVENT_DELIM:
  305. default:
  306. return 0;
  307. }
  308. }
  309. static void free_arg(struct print_arg *arg)
  310. {
  311. if (!arg)
  312. return;
  313. switch (arg->type) {
  314. case PRINT_ATOM:
  315. if (arg->atom.atom)
  316. free(arg->atom.atom);
  317. break;
  318. case PRINT_NULL:
  319. case PRINT_FIELD ... PRINT_OP:
  320. default:
  321. /* todo */
  322. break;
  323. }
  324. free(arg);
  325. }
  326. static enum event_type get_type(int ch)
  327. {
  328. if (ch == '\n')
  329. return EVENT_NEWLINE;
  330. if (isspace(ch))
  331. return EVENT_SPACE;
  332. if (isalnum(ch) || ch == '_')
  333. return EVENT_ITEM;
  334. if (ch == '\'')
  335. return EVENT_SQUOTE;
  336. if (ch == '"')
  337. return EVENT_DQUOTE;
  338. if (!isprint(ch))
  339. return EVENT_NONE;
  340. if (ch == '(' || ch == ')' || ch == ',')
  341. return EVENT_DELIM;
  342. return EVENT_OP;
  343. }
  344. static int __read_char(void)
  345. {
  346. if (input_buf_ptr >= input_buf_siz)
  347. return -1;
  348. return input_buf[input_buf_ptr++];
  349. }
  350. static int __peek_char(void)
  351. {
  352. if (input_buf_ptr >= input_buf_siz)
  353. return -1;
  354. return input_buf[input_buf_ptr];
  355. }
  356. static enum event_type __read_token(char **tok)
  357. {
  358. char buf[BUFSIZ];
  359. int ch, last_ch, quote_ch, next_ch;
  360. int i = 0;
  361. int tok_size = 0;
  362. enum event_type type;
  363. *tok = NULL;
  364. ch = __read_char();
  365. if (ch < 0)
  366. return EVENT_NONE;
  367. type = get_type(ch);
  368. if (type == EVENT_NONE)
  369. return type;
  370. buf[i++] = ch;
  371. switch (type) {
  372. case EVENT_NEWLINE:
  373. case EVENT_DELIM:
  374. *tok = malloc_or_die(2);
  375. (*tok)[0] = ch;
  376. (*tok)[1] = 0;
  377. return type;
  378. case EVENT_OP:
  379. switch (ch) {
  380. case '-':
  381. next_ch = __peek_char();
  382. if (next_ch == '>') {
  383. buf[i++] = __read_char();
  384. break;
  385. }
  386. /* fall through */
  387. case '+':
  388. case '|':
  389. case '&':
  390. case '>':
  391. case '<':
  392. last_ch = ch;
  393. ch = __peek_char();
  394. if (ch != last_ch)
  395. goto test_equal;
  396. buf[i++] = __read_char();
  397. switch (last_ch) {
  398. case '>':
  399. case '<':
  400. goto test_equal;
  401. default:
  402. break;
  403. }
  404. break;
  405. case '!':
  406. case '=':
  407. goto test_equal;
  408. default: /* what should we do instead? */
  409. break;
  410. }
  411. buf[i] = 0;
  412. *tok = strdup(buf);
  413. return type;
  414. test_equal:
  415. ch = __peek_char();
  416. if (ch == '=')
  417. buf[i++] = __read_char();
  418. break;
  419. case EVENT_DQUOTE:
  420. case EVENT_SQUOTE:
  421. /* don't keep quotes */
  422. i--;
  423. quote_ch = ch;
  424. last_ch = 0;
  425. do {
  426. if (i == (BUFSIZ - 1)) {
  427. buf[i] = 0;
  428. if (*tok) {
  429. *tok = realloc(*tok, tok_size + BUFSIZ);
  430. if (!*tok)
  431. return EVENT_NONE;
  432. strcat(*tok, buf);
  433. } else
  434. *tok = strdup(buf);
  435. if (!*tok)
  436. return EVENT_NONE;
  437. tok_size += BUFSIZ;
  438. i = 0;
  439. }
  440. last_ch = ch;
  441. ch = __read_char();
  442. buf[i++] = ch;
  443. } while (ch != quote_ch && last_ch != '\\');
  444. /* remove the last quote */
  445. i--;
  446. goto out;
  447. case EVENT_ERROR ... EVENT_SPACE:
  448. case EVENT_ITEM:
  449. default:
  450. break;
  451. }
  452. while (get_type(__peek_char()) == type) {
  453. if (i == (BUFSIZ - 1)) {
  454. buf[i] = 0;
  455. if (*tok) {
  456. *tok = realloc(*tok, tok_size + BUFSIZ);
  457. if (!*tok)
  458. return EVENT_NONE;
  459. strcat(*tok, buf);
  460. } else
  461. *tok = strdup(buf);
  462. if (!*tok)
  463. return EVENT_NONE;
  464. tok_size += BUFSIZ;
  465. i = 0;
  466. }
  467. ch = __read_char();
  468. buf[i++] = ch;
  469. }
  470. out:
  471. buf[i] = 0;
  472. if (*tok) {
  473. *tok = realloc(*tok, tok_size + i);
  474. if (!*tok)
  475. return EVENT_NONE;
  476. strcat(*tok, buf);
  477. } else
  478. *tok = strdup(buf);
  479. if (!*tok)
  480. return EVENT_NONE;
  481. return type;
  482. }
  483. static void free_token(char *tok)
  484. {
  485. if (tok)
  486. free(tok);
  487. }
  488. static enum event_type read_token(char **tok)
  489. {
  490. enum event_type type;
  491. for (;;) {
  492. type = __read_token(tok);
  493. if (type != EVENT_SPACE)
  494. return type;
  495. free_token(*tok);
  496. }
  497. /* not reached */
  498. return EVENT_NONE;
  499. }
  500. /* no newline */
  501. static enum event_type read_token_item(char **tok)
  502. {
  503. enum event_type type;
  504. for (;;) {
  505. type = __read_token(tok);
  506. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  507. return type;
  508. free_token(*tok);
  509. }
  510. /* not reached */
  511. return EVENT_NONE;
  512. }
  513. static int test_type(enum event_type type, enum event_type expect)
  514. {
  515. if (type != expect) {
  516. die("Error: expected type %d but read %d",
  517. expect, type);
  518. return -1;
  519. }
  520. return 0;
  521. }
  522. static int test_type_token(enum event_type type, char *token,
  523. enum event_type expect, char *expect_tok)
  524. {
  525. if (type != expect) {
  526. die("Error: expected type %d but read %d",
  527. expect, type);
  528. return -1;
  529. }
  530. if (strcmp(token, expect_tok) != 0) {
  531. die("Error: expected '%s' but read '%s'",
  532. expect_tok, token);
  533. return -1;
  534. }
  535. return 0;
  536. }
  537. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  538. {
  539. enum event_type type;
  540. if (newline_ok)
  541. type = read_token(tok);
  542. else
  543. type = read_token_item(tok);
  544. return test_type(type, expect);
  545. }
  546. static int read_expect_type(enum event_type expect, char **tok)
  547. {
  548. return __read_expect_type(expect, tok, 1);
  549. }
  550. static int __read_expected(enum event_type expect, char *str, int newline_ok)
  551. {
  552. enum event_type type;
  553. char *token;
  554. int ret;
  555. if (newline_ok)
  556. type = read_token(&token);
  557. else
  558. type = read_token_item(&token);
  559. ret = test_type_token(type, token, expect, str);
  560. free_token(token);
  561. return 0;
  562. }
  563. static int read_expected(enum event_type expect, char *str)
  564. {
  565. return __read_expected(expect, str, 1);
  566. }
  567. static int read_expected_item(enum event_type expect, char *str)
  568. {
  569. return __read_expected(expect, str, 0);
  570. }
  571. static char *event_read_name(void)
  572. {
  573. char *token;
  574. if (read_expected(EVENT_ITEM, (char *)"name") < 0)
  575. return NULL;
  576. if (read_expected(EVENT_OP, (char *)":") < 0)
  577. return NULL;
  578. if (read_expect_type(EVENT_ITEM, &token) < 0)
  579. goto fail;
  580. return token;
  581. fail:
  582. free_token(token);
  583. return NULL;
  584. }
  585. static int event_read_id(void)
  586. {
  587. char *token;
  588. int id;
  589. if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0)
  590. return -1;
  591. if (read_expected(EVENT_OP, (char *)":") < 0)
  592. return -1;
  593. if (read_expect_type(EVENT_ITEM, &token) < 0)
  594. goto fail;
  595. id = strtoul(token, NULL, 0);
  596. free_token(token);
  597. return id;
  598. fail:
  599. free_token(token);
  600. return -1;
  601. }
  602. static int event_read_fields(struct event *event, struct format_field **fields)
  603. {
  604. struct format_field *field = NULL;
  605. enum event_type type;
  606. char *token;
  607. char *last_token;
  608. int count = 0;
  609. do {
  610. type = read_token(&token);
  611. if (type == EVENT_NEWLINE) {
  612. free_token(token);
  613. return count;
  614. }
  615. count++;
  616. if (test_type_token(type, token, EVENT_ITEM, (char *)"field"))
  617. goto fail;
  618. free_token(token);
  619. type = read_token(&token);
  620. /*
  621. * The ftrace fields may still use the "special" name.
  622. * Just ignore it.
  623. */
  624. if (event->flags & EVENT_FL_ISFTRACE &&
  625. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  626. free_token(token);
  627. type = read_token(&token);
  628. }
  629. if (test_type_token(type, token, EVENT_OP, (char *)":") < 0)
  630. return -1;
  631. if (read_expect_type(EVENT_ITEM, &token) < 0)
  632. goto fail;
  633. last_token = token;
  634. field = malloc_or_die(sizeof(*field));
  635. memset(field, 0, sizeof(*field));
  636. /* read the rest of the type */
  637. for (;;) {
  638. type = read_token(&token);
  639. if (type == EVENT_ITEM ||
  640. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  641. /*
  642. * Some of the ftrace fields are broken and have
  643. * an illegal "." in them.
  644. */
  645. (event->flags & EVENT_FL_ISFTRACE &&
  646. type == EVENT_OP && strcmp(token, ".") == 0)) {
  647. if (strcmp(token, "*") == 0)
  648. field->flags |= FIELD_IS_POINTER;
  649. if (field->type) {
  650. field->type = realloc(field->type,
  651. strlen(field->type) +
  652. strlen(last_token) + 2);
  653. strcat(field->type, " ");
  654. strcat(field->type, last_token);
  655. } else
  656. field->type = last_token;
  657. last_token = token;
  658. continue;
  659. }
  660. break;
  661. }
  662. if (!field->type) {
  663. die("no type found");
  664. goto fail;
  665. }
  666. field->name = last_token;
  667. if (test_type(type, EVENT_OP))
  668. goto fail;
  669. if (strcmp(token, "[") == 0) {
  670. enum event_type last_type = type;
  671. char *brackets = token;
  672. int len;
  673. field->flags |= FIELD_IS_ARRAY;
  674. type = read_token(&token);
  675. while (strcmp(token, "]") != 0) {
  676. if (last_type == EVENT_ITEM &&
  677. type == EVENT_ITEM)
  678. len = 2;
  679. else
  680. len = 1;
  681. last_type = type;
  682. brackets = realloc(brackets,
  683. strlen(brackets) +
  684. strlen(token) + len);
  685. if (len == 2)
  686. strcat(brackets, " ");
  687. strcat(brackets, token);
  688. free_token(token);
  689. type = read_token(&token);
  690. if (type == EVENT_NONE) {
  691. die("failed to find token");
  692. goto fail;
  693. }
  694. }
  695. free_token(token);
  696. brackets = realloc(brackets, strlen(brackets) + 2);
  697. strcat(brackets, "]");
  698. /* add brackets to type */
  699. type = read_token(&token);
  700. /*
  701. * If the next token is not an OP, then it is of
  702. * the format: type [] item;
  703. */
  704. if (type == EVENT_ITEM) {
  705. field->type = realloc(field->type,
  706. strlen(field->type) +
  707. strlen(field->name) +
  708. strlen(brackets) + 2);
  709. strcat(field->type, " ");
  710. strcat(field->type, field->name);
  711. free_token(field->name);
  712. strcat(field->type, brackets);
  713. field->name = token;
  714. type = read_token(&token);
  715. } else {
  716. field->type = realloc(field->type,
  717. strlen(field->type) +
  718. strlen(brackets) + 1);
  719. strcat(field->type, brackets);
  720. }
  721. free(brackets);
  722. }
  723. if (test_type_token(type, token, EVENT_OP, (char *)";"))
  724. goto fail;
  725. free_token(token);
  726. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  727. goto fail_expect;
  728. if (read_expected(EVENT_OP, (char *)":") < 0)
  729. goto fail_expect;
  730. if (read_expect_type(EVENT_ITEM, &token))
  731. goto fail;
  732. field->offset = strtoul(token, NULL, 0);
  733. free_token(token);
  734. if (read_expected(EVENT_OP, (char *)";") < 0)
  735. goto fail_expect;
  736. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  737. goto fail_expect;
  738. if (read_expected(EVENT_OP, (char *)":") < 0)
  739. goto fail_expect;
  740. if (read_expect_type(EVENT_ITEM, &token))
  741. goto fail;
  742. field->size = strtoul(token, NULL, 0);
  743. free_token(token);
  744. if (read_expected(EVENT_OP, (char *)";") < 0)
  745. goto fail_expect;
  746. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  747. goto fail;
  748. free_token(token);
  749. *fields = field;
  750. fields = &field->next;
  751. } while (1);
  752. return 0;
  753. fail:
  754. free_token(token);
  755. fail_expect:
  756. if (field)
  757. free(field);
  758. return -1;
  759. }
  760. static int event_read_format(struct event *event)
  761. {
  762. char *token;
  763. int ret;
  764. if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
  765. return -1;
  766. if (read_expected(EVENT_OP, (char *)":") < 0)
  767. return -1;
  768. if (read_expect_type(EVENT_NEWLINE, &token))
  769. goto fail;
  770. free_token(token);
  771. ret = event_read_fields(event, &event->format.common_fields);
  772. if (ret < 0)
  773. return ret;
  774. event->format.nr_common = ret;
  775. ret = event_read_fields(event, &event->format.fields);
  776. if (ret < 0)
  777. return ret;
  778. event->format.nr_fields = ret;
  779. return 0;
  780. fail:
  781. free_token(token);
  782. return -1;
  783. }
  784. enum event_type
  785. process_arg_token(struct event *event, struct print_arg *arg,
  786. char **tok, enum event_type type);
  787. static enum event_type
  788. process_arg(struct event *event, struct print_arg *arg, char **tok)
  789. {
  790. enum event_type type;
  791. char *token;
  792. type = read_token(&token);
  793. *tok = token;
  794. return process_arg_token(event, arg, tok, type);
  795. }
  796. static enum event_type
  797. process_cond(struct event *event, struct print_arg *top, char **tok)
  798. {
  799. struct print_arg *arg, *left, *right;
  800. enum event_type type;
  801. char *token = NULL;
  802. arg = malloc_or_die(sizeof(*arg));
  803. memset(arg, 0, sizeof(*arg));
  804. left = malloc_or_die(sizeof(*left));
  805. right = malloc_or_die(sizeof(*right));
  806. arg->type = PRINT_OP;
  807. arg->op.left = left;
  808. arg->op.right = right;
  809. *tok = NULL;
  810. type = process_arg(event, left, &token);
  811. if (test_type_token(type, token, EVENT_OP, (char *)":"))
  812. goto out_free;
  813. arg->op.op = token;
  814. type = process_arg(event, right, &token);
  815. top->op.right = arg;
  816. *tok = token;
  817. return type;
  818. out_free:
  819. free_token(*tok);
  820. free(right);
  821. free(left);
  822. free_arg(arg);
  823. return EVENT_ERROR;
  824. }
  825. static int get_op_prio(char *op)
  826. {
  827. if (!op[1]) {
  828. switch (op[0]) {
  829. case '*':
  830. case '/':
  831. case '%':
  832. return 6;
  833. case '+':
  834. case '-':
  835. return 7;
  836. /* '>>' and '<<' are 8 */
  837. case '<':
  838. case '>':
  839. return 9;
  840. /* '==' and '!=' are 10 */
  841. case '&':
  842. return 11;
  843. case '^':
  844. return 12;
  845. case '|':
  846. return 13;
  847. case '?':
  848. return 16;
  849. default:
  850. die("unknown op '%c'", op[0]);
  851. return -1;
  852. }
  853. } else {
  854. if (strcmp(op, "++") == 0 ||
  855. strcmp(op, "--") == 0) {
  856. return 3;
  857. } else if (strcmp(op, ">>") == 0 ||
  858. strcmp(op, "<<") == 0) {
  859. return 8;
  860. } else if (strcmp(op, ">=") == 0 ||
  861. strcmp(op, "<=") == 0) {
  862. return 9;
  863. } else if (strcmp(op, "==") == 0 ||
  864. strcmp(op, "!=") == 0) {
  865. return 10;
  866. } else if (strcmp(op, "&&") == 0) {
  867. return 14;
  868. } else if (strcmp(op, "||") == 0) {
  869. return 15;
  870. } else {
  871. die("unknown op '%s'", op);
  872. return -1;
  873. }
  874. }
  875. }
  876. static void set_op_prio(struct print_arg *arg)
  877. {
  878. /* single ops are the greatest */
  879. if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
  880. arg->op.prio = 0;
  881. return;
  882. }
  883. arg->op.prio = get_op_prio(arg->op.op);
  884. }
  885. static enum event_type
  886. process_op(struct event *event, struct print_arg *arg, char **tok)
  887. {
  888. struct print_arg *left, *right = NULL;
  889. enum event_type type;
  890. char *token;
  891. /* the op is passed in via tok */
  892. token = *tok;
  893. if (arg->type == PRINT_OP && !arg->op.left) {
  894. /* handle single op */
  895. if (token[1]) {
  896. die("bad op token %s", token);
  897. return EVENT_ERROR;
  898. }
  899. switch (token[0]) {
  900. case '!':
  901. case '+':
  902. case '-':
  903. break;
  904. default:
  905. die("bad op token %s", token);
  906. return EVENT_ERROR;
  907. }
  908. /* make an empty left */
  909. left = malloc_or_die(sizeof(*left));
  910. left->type = PRINT_NULL;
  911. arg->op.left = left;
  912. right = malloc_or_die(sizeof(*right));
  913. arg->op.right = right;
  914. type = process_arg(event, right, tok);
  915. } else if (strcmp(token, "?") == 0) {
  916. left = malloc_or_die(sizeof(*left));
  917. /* copy the top arg to the left */
  918. *left = *arg;
  919. arg->type = PRINT_OP;
  920. arg->op.op = token;
  921. arg->op.left = left;
  922. arg->op.prio = 0;
  923. type = process_cond(event, arg, tok);
  924. } else if (strcmp(token, ">>") == 0 ||
  925. strcmp(token, "<<") == 0 ||
  926. strcmp(token, "&") == 0 ||
  927. strcmp(token, "|") == 0 ||
  928. strcmp(token, "&&") == 0 ||
  929. strcmp(token, "||") == 0 ||
  930. strcmp(token, "-") == 0 ||
  931. strcmp(token, "+") == 0 ||
  932. strcmp(token, "*") == 0 ||
  933. strcmp(token, "^") == 0 ||
  934. strcmp(token, "/") == 0 ||
  935. strcmp(token, "==") == 0 ||
  936. strcmp(token, "!=") == 0) {
  937. left = malloc_or_die(sizeof(*left));
  938. /* copy the top arg to the left */
  939. *left = *arg;
  940. arg->type = PRINT_OP;
  941. arg->op.op = token;
  942. arg->op.left = left;
  943. set_op_prio(arg);
  944. right = malloc_or_die(sizeof(*right));
  945. type = process_arg(event, right, tok);
  946. arg->op.right = right;
  947. } else {
  948. die("unknown op '%s'", token);
  949. /* the arg is now the left side */
  950. return EVENT_NONE;
  951. }
  952. if (type == EVENT_OP) {
  953. int prio;
  954. /* higher prios need to be closer to the root */
  955. prio = get_op_prio(*tok);
  956. if (prio > arg->op.prio)
  957. return process_op(event, arg, tok);
  958. return process_op(event, right, tok);
  959. }
  960. return type;
  961. }
  962. static enum event_type
  963. process_entry(struct event *event __unused, struct print_arg *arg,
  964. char **tok)
  965. {
  966. enum event_type type;
  967. char *field;
  968. char *token;
  969. if (read_expected(EVENT_OP, (char *)"->") < 0)
  970. return EVENT_ERROR;
  971. if (read_expect_type(EVENT_ITEM, &token) < 0)
  972. goto fail;
  973. field = token;
  974. arg->type = PRINT_FIELD;
  975. arg->field.name = field;
  976. type = read_token(&token);
  977. *tok = token;
  978. return type;
  979. fail:
  980. free_token(token);
  981. return EVENT_ERROR;
  982. }
  983. static char *arg_eval (struct print_arg *arg);
  984. static long long arg_num_eval(struct print_arg *arg)
  985. {
  986. long long left, right;
  987. long long val = 0;
  988. switch (arg->type) {
  989. case PRINT_ATOM:
  990. val = strtoll(arg->atom.atom, NULL, 0);
  991. break;
  992. case PRINT_TYPE:
  993. val = arg_num_eval(arg->typecast.item);
  994. break;
  995. case PRINT_OP:
  996. switch (arg->op.op[0]) {
  997. case '|':
  998. left = arg_num_eval(arg->op.left);
  999. right = arg_num_eval(arg->op.right);
  1000. if (arg->op.op[1])
  1001. val = left || right;
  1002. else
  1003. val = left | right;
  1004. break;
  1005. case '&':
  1006. left = arg_num_eval(arg->op.left);
  1007. right = arg_num_eval(arg->op.right);
  1008. if (arg->op.op[1])
  1009. val = left && right;
  1010. else
  1011. val = left & right;
  1012. break;
  1013. case '<':
  1014. left = arg_num_eval(arg->op.left);
  1015. right = arg_num_eval(arg->op.right);
  1016. switch (arg->op.op[1]) {
  1017. case 0:
  1018. val = left < right;
  1019. break;
  1020. case '<':
  1021. val = left << right;
  1022. break;
  1023. case '=':
  1024. val = left <= right;
  1025. break;
  1026. default:
  1027. die("unknown op '%s'", arg->op.op);
  1028. }
  1029. break;
  1030. case '>':
  1031. left = arg_num_eval(arg->op.left);
  1032. right = arg_num_eval(arg->op.right);
  1033. switch (arg->op.op[1]) {
  1034. case 0:
  1035. val = left > right;
  1036. break;
  1037. case '>':
  1038. val = left >> right;
  1039. break;
  1040. case '=':
  1041. val = left >= right;
  1042. break;
  1043. default:
  1044. die("unknown op '%s'", arg->op.op);
  1045. }
  1046. break;
  1047. case '=':
  1048. left = arg_num_eval(arg->op.left);
  1049. right = arg_num_eval(arg->op.right);
  1050. if (arg->op.op[1] != '=')
  1051. die("unknown op '%s'", arg->op.op);
  1052. val = left == right;
  1053. break;
  1054. case '!':
  1055. left = arg_num_eval(arg->op.left);
  1056. right = arg_num_eval(arg->op.right);
  1057. switch (arg->op.op[1]) {
  1058. case '=':
  1059. val = left != right;
  1060. break;
  1061. default:
  1062. die("unknown op '%s'", arg->op.op);
  1063. }
  1064. break;
  1065. default:
  1066. die("unknown op '%s'", arg->op.op);
  1067. }
  1068. break;
  1069. case PRINT_NULL:
  1070. case PRINT_FIELD ... PRINT_SYMBOL:
  1071. case PRINT_STRING:
  1072. default:
  1073. die("invalid eval type %d", arg->type);
  1074. }
  1075. return val;
  1076. }
  1077. static char *arg_eval (struct print_arg *arg)
  1078. {
  1079. long long val;
  1080. static char buf[20];
  1081. switch (arg->type) {
  1082. case PRINT_ATOM:
  1083. return arg->atom.atom;
  1084. case PRINT_TYPE:
  1085. return arg_eval(arg->typecast.item);
  1086. case PRINT_OP:
  1087. val = arg_num_eval(arg);
  1088. sprintf(buf, "%lld", val);
  1089. return buf;
  1090. case PRINT_NULL:
  1091. case PRINT_FIELD ... PRINT_SYMBOL:
  1092. case PRINT_STRING:
  1093. default:
  1094. die("invalid eval type %d", arg->type);
  1095. break;
  1096. }
  1097. return NULL;
  1098. }
  1099. static enum event_type
  1100. process_fields(struct event *event, struct print_flag_sym **list, char **tok)
  1101. {
  1102. enum event_type type;
  1103. struct print_arg *arg = NULL;
  1104. struct print_flag_sym *field;
  1105. char *token = NULL;
  1106. char *value;
  1107. do {
  1108. free_token(token);
  1109. type = read_token_item(&token);
  1110. if (test_type_token(type, token, EVENT_OP, (char *)"{"))
  1111. break;
  1112. arg = malloc_or_die(sizeof(*arg));
  1113. free_token(token);
  1114. type = process_arg(event, arg, &token);
  1115. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1116. goto out_free;
  1117. field = malloc_or_die(sizeof(*field));
  1118. memset(field, 0, sizeof(field));
  1119. value = arg_eval(arg);
  1120. field->value = strdup(value);
  1121. free_token(token);
  1122. type = process_arg(event, arg, &token);
  1123. if (test_type_token(type, token, EVENT_OP, (char *)"}"))
  1124. goto out_free;
  1125. value = arg_eval(arg);
  1126. field->str = strdup(value);
  1127. free_arg(arg);
  1128. arg = NULL;
  1129. *list = field;
  1130. list = &field->next;
  1131. free_token(token);
  1132. type = read_token_item(&token);
  1133. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1134. *tok = token;
  1135. return type;
  1136. out_free:
  1137. free_arg(arg);
  1138. free_token(token);
  1139. return EVENT_ERROR;
  1140. }
  1141. static enum event_type
  1142. process_flags(struct event *event, struct print_arg *arg, char **tok)
  1143. {
  1144. struct print_arg *field;
  1145. enum event_type type;
  1146. char *token;
  1147. memset(arg, 0, sizeof(*arg));
  1148. arg->type = PRINT_FLAGS;
  1149. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1150. return EVENT_ERROR;
  1151. field = malloc_or_die(sizeof(*field));
  1152. type = process_arg(event, field, &token);
  1153. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1154. goto out_free;
  1155. arg->flags.field = field;
  1156. type = read_token_item(&token);
  1157. if (event_item_type(type)) {
  1158. arg->flags.delim = token;
  1159. type = read_token_item(&token);
  1160. }
  1161. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1162. goto out_free;
  1163. type = process_fields(event, &arg->flags.flags, &token);
  1164. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1165. goto out_free;
  1166. free_token(token);
  1167. type = read_token_item(tok);
  1168. return type;
  1169. out_free:
  1170. free_token(token);
  1171. return EVENT_ERROR;
  1172. }
  1173. static enum event_type
  1174. process_symbols(struct event *event, struct print_arg *arg, char **tok)
  1175. {
  1176. struct print_arg *field;
  1177. enum event_type type;
  1178. char *token;
  1179. memset(arg, 0, sizeof(*arg));
  1180. arg->type = PRINT_SYMBOL;
  1181. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1182. return EVENT_ERROR;
  1183. field = malloc_or_die(sizeof(*field));
  1184. type = process_arg(event, field, &token);
  1185. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1186. goto out_free;
  1187. arg->symbol.field = field;
  1188. type = process_fields(event, &arg->symbol.symbols, &token);
  1189. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1190. goto out_free;
  1191. free_token(token);
  1192. type = read_token_item(tok);
  1193. return type;
  1194. out_free:
  1195. free_token(token);
  1196. return EVENT_ERROR;
  1197. }
  1198. static enum event_type
  1199. process_paren(struct event *event, struct print_arg *arg, char **tok)
  1200. {
  1201. struct print_arg *item_arg;
  1202. enum event_type type;
  1203. int ptr_cast = 0;
  1204. char *token;
  1205. type = process_arg(event, arg, &token);
  1206. if (type == EVENT_ERROR)
  1207. return EVENT_ERROR;
  1208. if (type == EVENT_OP) {
  1209. /* handle the ptr casts */
  1210. if (!strcmp(token, "*")) {
  1211. /*
  1212. * FIXME: should we zapp whitespaces before ')' ?
  1213. * (may require a peek_token_item())
  1214. */
  1215. if (__peek_char() == ')') {
  1216. ptr_cast = 1;
  1217. free_token(token);
  1218. type = read_token_item(&token);
  1219. }
  1220. }
  1221. if (!ptr_cast) {
  1222. type = process_op(event, arg, &token);
  1223. if (type == EVENT_ERROR)
  1224. return EVENT_ERROR;
  1225. }
  1226. }
  1227. if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
  1228. free_token(token);
  1229. return EVENT_ERROR;
  1230. }
  1231. free_token(token);
  1232. type = read_token_item(&token);
  1233. /*
  1234. * If the next token is an item or another open paren, then
  1235. * this was a typecast.
  1236. */
  1237. if (event_item_type(type) ||
  1238. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1239. /* make this a typecast and contine */
  1240. /* prevous must be an atom */
  1241. if (arg->type != PRINT_ATOM)
  1242. die("previous needed to be PRINT_ATOM");
  1243. item_arg = malloc_or_die(sizeof(*item_arg));
  1244. arg->type = PRINT_TYPE;
  1245. if (ptr_cast) {
  1246. char *old = arg->atom.atom;
  1247. arg->atom.atom = malloc_or_die(strlen(old + 3));
  1248. sprintf(arg->atom.atom, "%s *", old);
  1249. free(old);
  1250. }
  1251. arg->typecast.type = arg->atom.atom;
  1252. arg->typecast.item = item_arg;
  1253. type = process_arg_token(event, item_arg, &token, type);
  1254. }
  1255. *tok = token;
  1256. return type;
  1257. }
  1258. static enum event_type
  1259. process_str(struct event *event __unused, struct print_arg *arg, char **tok)
  1260. {
  1261. enum event_type type;
  1262. char *token;
  1263. if (read_expected(EVENT_DELIM, (char *)"(") < 0)
  1264. return EVENT_ERROR;
  1265. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1266. goto fail;
  1267. arg->type = PRINT_STRING;
  1268. arg->string.string = token;
  1269. if (read_expected(EVENT_DELIM, (char *)")") < 0)
  1270. return EVENT_ERROR;
  1271. type = read_token(&token);
  1272. *tok = token;
  1273. return type;
  1274. fail:
  1275. free_token(token);
  1276. return EVENT_ERROR;
  1277. }
  1278. enum event_type
  1279. process_arg_token(struct event *event, struct print_arg *arg,
  1280. char **tok, enum event_type type)
  1281. {
  1282. char *token;
  1283. char *atom;
  1284. token = *tok;
  1285. switch (type) {
  1286. case EVENT_ITEM:
  1287. if (strcmp(token, "REC") == 0) {
  1288. free_token(token);
  1289. type = process_entry(event, arg, &token);
  1290. } else if (strcmp(token, "__print_flags") == 0) {
  1291. free_token(token);
  1292. type = process_flags(event, arg, &token);
  1293. } else if (strcmp(token, "__print_symbolic") == 0) {
  1294. free_token(token);
  1295. type = process_symbols(event, arg, &token);
  1296. } else if (strcmp(token, "__get_str") == 0) {
  1297. free_token(token);
  1298. type = process_str(event, arg, &token);
  1299. } else {
  1300. atom = token;
  1301. /* test the next token */
  1302. type = read_token_item(&token);
  1303. /* atoms can be more than one token long */
  1304. while (type == EVENT_ITEM) {
  1305. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  1306. strcat(atom, " ");
  1307. strcat(atom, token);
  1308. free_token(token);
  1309. type = read_token_item(&token);
  1310. }
  1311. /* todo, test for function */
  1312. arg->type = PRINT_ATOM;
  1313. arg->atom.atom = atom;
  1314. }
  1315. break;
  1316. case EVENT_DQUOTE:
  1317. case EVENT_SQUOTE:
  1318. arg->type = PRINT_ATOM;
  1319. arg->atom.atom = token;
  1320. type = read_token_item(&token);
  1321. break;
  1322. case EVENT_DELIM:
  1323. if (strcmp(token, "(") == 0) {
  1324. free_token(token);
  1325. type = process_paren(event, arg, &token);
  1326. break;
  1327. }
  1328. case EVENT_OP:
  1329. /* handle single ops */
  1330. arg->type = PRINT_OP;
  1331. arg->op.op = token;
  1332. arg->op.left = NULL;
  1333. type = process_op(event, arg, &token);
  1334. break;
  1335. case EVENT_ERROR ... EVENT_NEWLINE:
  1336. default:
  1337. die("unexpected type %d", type);
  1338. }
  1339. *tok = token;
  1340. return type;
  1341. }
  1342. static int event_read_print_args(struct event *event, struct print_arg **list)
  1343. {
  1344. enum event_type type;
  1345. struct print_arg *arg;
  1346. char *token;
  1347. int args = 0;
  1348. do {
  1349. arg = malloc_or_die(sizeof(*arg));
  1350. memset(arg, 0, sizeof(*arg));
  1351. type = process_arg(event, arg, &token);
  1352. if (type == EVENT_ERROR) {
  1353. free_arg(arg);
  1354. return -1;
  1355. }
  1356. *list = arg;
  1357. args++;
  1358. if (type == EVENT_OP) {
  1359. type = process_op(event, arg, &token);
  1360. list = &arg->next;
  1361. continue;
  1362. }
  1363. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  1364. free_token(token);
  1365. *list = arg;
  1366. list = &arg->next;
  1367. continue;
  1368. }
  1369. break;
  1370. } while (type != EVENT_NONE);
  1371. if (type != EVENT_NONE)
  1372. free_token(token);
  1373. return args;
  1374. }
  1375. static int event_read_print(struct event *event)
  1376. {
  1377. enum event_type type;
  1378. char *token;
  1379. int ret;
  1380. if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
  1381. return -1;
  1382. if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
  1383. return -1;
  1384. if (read_expected(EVENT_OP, (char *)":") < 0)
  1385. return -1;
  1386. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  1387. goto fail;
  1388. event->print_fmt.format = token;
  1389. event->print_fmt.args = NULL;
  1390. /* ok to have no arg */
  1391. type = read_token_item(&token);
  1392. if (type == EVENT_NONE)
  1393. return 0;
  1394. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1395. goto fail;
  1396. free_token(token);
  1397. ret = event_read_print_args(event, &event->print_fmt.args);
  1398. if (ret < 0)
  1399. return -1;
  1400. return 0;
  1401. fail:
  1402. free_token(token);
  1403. return -1;
  1404. }
  1405. static struct format_field *
  1406. find_common_field(struct event *event, const char *name)
  1407. {
  1408. struct format_field *format;
  1409. for (format = event->format.common_fields;
  1410. format; format = format->next) {
  1411. if (strcmp(format->name, name) == 0)
  1412. break;
  1413. }
  1414. return format;
  1415. }
  1416. static struct format_field *
  1417. find_field(struct event *event, const char *name)
  1418. {
  1419. struct format_field *format;
  1420. for (format = event->format.fields;
  1421. format; format = format->next) {
  1422. if (strcmp(format->name, name) == 0)
  1423. break;
  1424. }
  1425. return format;
  1426. }
  1427. static struct format_field *
  1428. find_any_field(struct event *event, const char *name)
  1429. {
  1430. struct format_field *format;
  1431. format = find_common_field(event, name);
  1432. if (format)
  1433. return format;
  1434. return find_field(event, name);
  1435. }
  1436. static unsigned long long read_size(void *ptr, int size)
  1437. {
  1438. switch (size) {
  1439. case 1:
  1440. return *(unsigned char *)ptr;
  1441. case 2:
  1442. return data2host2(ptr);
  1443. case 4:
  1444. return data2host4(ptr);
  1445. case 8:
  1446. return data2host8(ptr);
  1447. default:
  1448. /* BUG! */
  1449. return 0;
  1450. }
  1451. }
  1452. static int get_common_info(const char *type, int *offset, int *size)
  1453. {
  1454. struct event *event;
  1455. struct format_field *field;
  1456. /*
  1457. * All events should have the same common elements.
  1458. * Pick any event to find where the type is;
  1459. */
  1460. if (!event_list)
  1461. die("no event_list!");
  1462. event = event_list;
  1463. field = find_common_field(event, type);
  1464. if (!field)
  1465. die("field '%s' not found", type);
  1466. *offset = field->offset;
  1467. *size = field->size;
  1468. return 0;
  1469. }
  1470. static int parse_common_type(void *data)
  1471. {
  1472. static int type_offset;
  1473. static int type_size;
  1474. int ret;
  1475. if (!type_size) {
  1476. ret = get_common_info("common_type",
  1477. &type_offset,
  1478. &type_size);
  1479. if (ret < 0)
  1480. return ret;
  1481. }
  1482. return read_size(data + type_offset, type_size);
  1483. }
  1484. static int parse_common_pid(void *data)
  1485. {
  1486. static int pid_offset;
  1487. static int pid_size;
  1488. int ret;
  1489. if (!pid_size) {
  1490. ret = get_common_info("common_pid",
  1491. &pid_offset,
  1492. &pid_size);
  1493. if (ret < 0)
  1494. return ret;
  1495. }
  1496. return read_size(data + pid_offset, pid_size);
  1497. }
  1498. static struct event *find_event(int id)
  1499. {
  1500. struct event *event;
  1501. for (event = event_list; event; event = event->next) {
  1502. if (event->id == id)
  1503. break;
  1504. }
  1505. return event;
  1506. }
  1507. static unsigned long long eval_num_arg(void *data, int size,
  1508. struct event *event, struct print_arg *arg)
  1509. {
  1510. unsigned long long val = 0;
  1511. unsigned long long left, right;
  1512. switch (arg->type) {
  1513. case PRINT_NULL:
  1514. /* ?? */
  1515. return 0;
  1516. case PRINT_ATOM:
  1517. return strtoull(arg->atom.atom, NULL, 0);
  1518. case PRINT_FIELD:
  1519. if (!arg->field.field) {
  1520. arg->field.field = find_any_field(event, arg->field.name);
  1521. if (!arg->field.field)
  1522. die("field %s not found", arg->field.name);
  1523. }
  1524. /* must be a number */
  1525. val = read_size(data + arg->field.field->offset,
  1526. arg->field.field->size);
  1527. break;
  1528. case PRINT_FLAGS:
  1529. case PRINT_SYMBOL:
  1530. break;
  1531. case PRINT_TYPE:
  1532. return eval_num_arg(data, size, event, arg->typecast.item);
  1533. case PRINT_STRING:
  1534. return 0;
  1535. break;
  1536. case PRINT_OP:
  1537. left = eval_num_arg(data, size, event, arg->op.left);
  1538. right = eval_num_arg(data, size, event, arg->op.right);
  1539. switch (arg->op.op[0]) {
  1540. case '|':
  1541. if (arg->op.op[1])
  1542. val = left || right;
  1543. else
  1544. val = left | right;
  1545. break;
  1546. case '&':
  1547. if (arg->op.op[1])
  1548. val = left && right;
  1549. else
  1550. val = left & right;
  1551. break;
  1552. case '<':
  1553. switch (arg->op.op[1]) {
  1554. case 0:
  1555. val = left < right;
  1556. break;
  1557. case '<':
  1558. val = left << right;
  1559. break;
  1560. case '=':
  1561. val = left <= right;
  1562. break;
  1563. default:
  1564. die("unknown op '%s'", arg->op.op);
  1565. }
  1566. break;
  1567. case '>':
  1568. switch (arg->op.op[1]) {
  1569. case 0:
  1570. val = left > right;
  1571. break;
  1572. case '>':
  1573. val = left >> right;
  1574. break;
  1575. case '=':
  1576. val = left >= right;
  1577. break;
  1578. default:
  1579. die("unknown op '%s'", arg->op.op);
  1580. }
  1581. break;
  1582. case '=':
  1583. if (arg->op.op[1] != '=')
  1584. die("unknown op '%s'", arg->op.op);
  1585. val = left == right;
  1586. break;
  1587. default:
  1588. die("unknown op '%s'", arg->op.op);
  1589. }
  1590. break;
  1591. default: /* not sure what to do there */
  1592. return 0;
  1593. }
  1594. return val;
  1595. }
  1596. struct flag {
  1597. const char *name;
  1598. unsigned long long value;
  1599. };
  1600. static const struct flag flags[] = {
  1601. { "HI_SOFTIRQ", 0 },
  1602. { "TIMER_SOFTIRQ", 1 },
  1603. { "NET_TX_SOFTIRQ", 2 },
  1604. { "NET_RX_SOFTIRQ", 3 },
  1605. { "BLOCK_SOFTIRQ", 4 },
  1606. { "TASKLET_SOFTIRQ", 5 },
  1607. { "SCHED_SOFTIRQ", 6 },
  1608. { "HRTIMER_SOFTIRQ", 7 },
  1609. { "RCU_SOFTIRQ", 8 },
  1610. { "HRTIMER_NORESTART", 0 },
  1611. { "HRTIMER_RESTART", 1 },
  1612. };
  1613. static unsigned long long eval_flag(const char *flag)
  1614. {
  1615. int i;
  1616. /*
  1617. * Some flags in the format files do not get converted.
  1618. * If the flag is not numeric, see if it is something that
  1619. * we already know about.
  1620. */
  1621. if (isdigit(flag[0]))
  1622. return strtoull(flag, NULL, 0);
  1623. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1624. if (strcmp(flags[i].name, flag) == 0)
  1625. return flags[i].value;
  1626. return 0;
  1627. }
  1628. static void print_str_arg(void *data, int size,
  1629. struct event *event, struct print_arg *arg)
  1630. {
  1631. struct print_flag_sym *flag;
  1632. unsigned long long val, fval;
  1633. char *str;
  1634. int print;
  1635. switch (arg->type) {
  1636. case PRINT_NULL:
  1637. /* ?? */
  1638. return;
  1639. case PRINT_ATOM:
  1640. printf("%s", arg->atom.atom);
  1641. return;
  1642. case PRINT_FIELD:
  1643. if (!arg->field.field) {
  1644. arg->field.field = find_any_field(event, arg->field.name);
  1645. if (!arg->field.field)
  1646. die("field %s not found", arg->field.name);
  1647. }
  1648. str = malloc_or_die(arg->field.field->size + 1);
  1649. memcpy(str, data + arg->field.field->offset,
  1650. arg->field.field->size);
  1651. str[arg->field.field->size] = 0;
  1652. printf("%s", str);
  1653. free(str);
  1654. break;
  1655. case PRINT_FLAGS:
  1656. val = eval_num_arg(data, size, event, arg->flags.field);
  1657. print = 0;
  1658. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1659. fval = eval_flag(flag->value);
  1660. if (!val && !fval) {
  1661. printf("%s", flag->str);
  1662. break;
  1663. }
  1664. if (fval && (val & fval) == fval) {
  1665. if (print && arg->flags.delim)
  1666. printf("%s", arg->flags.delim);
  1667. printf("%s", flag->str);
  1668. print = 1;
  1669. val &= ~fval;
  1670. }
  1671. }
  1672. break;
  1673. case PRINT_SYMBOL:
  1674. val = eval_num_arg(data, size, event, arg->symbol.field);
  1675. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1676. fval = eval_flag(flag->value);
  1677. if (val == fval) {
  1678. printf("%s", flag->str);
  1679. break;
  1680. }
  1681. }
  1682. break;
  1683. case PRINT_TYPE:
  1684. break;
  1685. case PRINT_STRING:
  1686. printf("%s", arg->string.string);
  1687. break;
  1688. case PRINT_OP:
  1689. /*
  1690. * The only op for string should be ? :
  1691. */
  1692. if (arg->op.op[0] != '?')
  1693. return;
  1694. val = eval_num_arg(data, size, event, arg->op.left);
  1695. if (val)
  1696. print_str_arg(data, size, event, arg->op.right->op.left);
  1697. else
  1698. print_str_arg(data, size, event, arg->op.right->op.right);
  1699. break;
  1700. default:
  1701. /* well... */
  1702. break;
  1703. }
  1704. }
  1705. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1706. {
  1707. static struct format_field *field, *ip_field;
  1708. struct print_arg *args, *arg, **next;
  1709. unsigned long long ip, val;
  1710. char *ptr;
  1711. void *bptr;
  1712. if (!field) {
  1713. field = find_field(event, "buf");
  1714. if (!field)
  1715. die("can't find buffer field for binary printk");
  1716. ip_field = find_field(event, "ip");
  1717. if (!ip_field)
  1718. die("can't find ip field for binary printk");
  1719. }
  1720. ip = read_size(data + ip_field->offset, ip_field->size);
  1721. /*
  1722. * The first arg is the IP pointer.
  1723. */
  1724. args = malloc_or_die(sizeof(*args));
  1725. arg = args;
  1726. arg->next = NULL;
  1727. next = &arg->next;
  1728. arg->type = PRINT_ATOM;
  1729. arg->atom.atom = malloc_or_die(32);
  1730. sprintf(arg->atom.atom, "%lld", ip);
  1731. /* skip the first "%pf : " */
  1732. for (ptr = fmt + 6, bptr = data + field->offset;
  1733. bptr < data + size && *ptr; ptr++) {
  1734. int ls = 0;
  1735. if (*ptr == '%') {
  1736. process_again:
  1737. ptr++;
  1738. switch (*ptr) {
  1739. case '%':
  1740. break;
  1741. case 'l':
  1742. ls++;
  1743. goto process_again;
  1744. case 'L':
  1745. ls = 2;
  1746. goto process_again;
  1747. case '0' ... '9':
  1748. goto process_again;
  1749. case 'p':
  1750. ls = 1;
  1751. /* fall through */
  1752. case 'd':
  1753. case 'u':
  1754. case 'x':
  1755. case 'i':
  1756. bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
  1757. ~(long_size - 1));
  1758. switch (ls) {
  1759. case 0:
  1760. case 1:
  1761. ls = long_size;
  1762. break;
  1763. case 2:
  1764. ls = 8;
  1765. default:
  1766. break;
  1767. }
  1768. val = read_size(bptr, ls);
  1769. bptr += ls;
  1770. arg = malloc_or_die(sizeof(*arg));
  1771. arg->next = NULL;
  1772. arg->type = PRINT_ATOM;
  1773. arg->atom.atom = malloc_or_die(32);
  1774. sprintf(arg->atom.atom, "%lld", val);
  1775. *next = arg;
  1776. next = &arg->next;
  1777. break;
  1778. case 's':
  1779. arg = malloc_or_die(sizeof(*arg));
  1780. arg->next = NULL;
  1781. arg->type = PRINT_STRING;
  1782. arg->string.string = strdup(bptr);
  1783. bptr += strlen(bptr) + 1;
  1784. *next = arg;
  1785. next = &arg->next;
  1786. default:
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. return args;
  1792. }
  1793. static void free_args(struct print_arg *args)
  1794. {
  1795. struct print_arg *next;
  1796. while (args) {
  1797. next = args->next;
  1798. if (args->type == PRINT_ATOM)
  1799. free(args->atom.atom);
  1800. else
  1801. free(args->string.string);
  1802. free(args);
  1803. args = next;
  1804. }
  1805. }
  1806. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  1807. {
  1808. unsigned long long addr;
  1809. static struct format_field *field;
  1810. struct printk_map *printk;
  1811. char *format;
  1812. char *p;
  1813. if (!field) {
  1814. field = find_field(event, "fmt");
  1815. if (!field)
  1816. die("can't find format field for binary printk");
  1817. printf("field->offset = %d size=%d\n", field->offset, field->size);
  1818. }
  1819. addr = read_size(data + field->offset, field->size);
  1820. printk = find_printk(addr);
  1821. if (!printk) {
  1822. format = malloc_or_die(45);
  1823. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  1824. addr);
  1825. return format;
  1826. }
  1827. p = printk->printk;
  1828. /* Remove any quotes. */
  1829. if (*p == '"')
  1830. p++;
  1831. format = malloc_or_die(strlen(p) + 10);
  1832. sprintf(format, "%s : %s", "%pf", p);
  1833. /* remove ending quotes and new line since we will add one too */
  1834. p = format + strlen(format) - 1;
  1835. if (*p == '"')
  1836. *p = 0;
  1837. p -= 2;
  1838. if (strcmp(p, "\\n") == 0)
  1839. *p = 0;
  1840. return format;
  1841. }
  1842. static void pretty_print(void *data, int size, struct event *event)
  1843. {
  1844. struct print_fmt *print_fmt = &event->print_fmt;
  1845. struct print_arg *arg = print_fmt->args;
  1846. struct print_arg *args = NULL;
  1847. const char *ptr = print_fmt->format;
  1848. unsigned long long val;
  1849. struct func_map *func;
  1850. const char *saveptr;
  1851. char *bprint_fmt = NULL;
  1852. char format[32];
  1853. int show_func;
  1854. int len;
  1855. int ls;
  1856. if (event->flags & EVENT_FL_ISFUNC)
  1857. ptr = " %pF <-- %pF";
  1858. if (event->flags & EVENT_FL_ISBPRINT) {
  1859. bprint_fmt = get_bprint_format(data, size, event);
  1860. args = make_bprint_args(bprint_fmt, data, size, event);
  1861. arg = args;
  1862. ptr = bprint_fmt;
  1863. }
  1864. for (; *ptr; ptr++) {
  1865. ls = 0;
  1866. if (*ptr == '%') {
  1867. saveptr = ptr;
  1868. show_func = 0;
  1869. cont_process:
  1870. ptr++;
  1871. switch (*ptr) {
  1872. case '%':
  1873. printf("%%");
  1874. break;
  1875. case 'l':
  1876. ls++;
  1877. goto cont_process;
  1878. case 'L':
  1879. ls = 2;
  1880. goto cont_process;
  1881. case 'z':
  1882. case 'Z':
  1883. case '0' ... '9':
  1884. goto cont_process;
  1885. case 'p':
  1886. if (long_size == 4)
  1887. ls = 1;
  1888. else
  1889. ls = 2;
  1890. if (*(ptr+1) == 'F' ||
  1891. *(ptr+1) == 'f') {
  1892. ptr++;
  1893. show_func = *ptr;
  1894. }
  1895. /* fall through */
  1896. case 'd':
  1897. case 'i':
  1898. case 'x':
  1899. case 'X':
  1900. case 'u':
  1901. if (!arg)
  1902. die("no argument match");
  1903. len = ((unsigned long)ptr + 1) -
  1904. (unsigned long)saveptr;
  1905. /* should never happen */
  1906. if (len > 32)
  1907. die("bad format!");
  1908. memcpy(format, saveptr, len);
  1909. format[len] = 0;
  1910. val = eval_num_arg(data, size, event, arg);
  1911. arg = arg->next;
  1912. if (show_func) {
  1913. func = find_func(val);
  1914. if (func) {
  1915. printf("%s", func->func);
  1916. if (show_func == 'F')
  1917. printf("+0x%llx",
  1918. val - func->addr);
  1919. break;
  1920. }
  1921. }
  1922. switch (ls) {
  1923. case 0:
  1924. printf(format, (int)val);
  1925. break;
  1926. case 1:
  1927. printf(format, (long)val);
  1928. break;
  1929. case 2:
  1930. printf(format, (long long)val);
  1931. break;
  1932. default:
  1933. die("bad count (%d)", ls);
  1934. }
  1935. break;
  1936. case 's':
  1937. if (!arg)
  1938. die("no matching argument");
  1939. print_str_arg(data, size, event, arg);
  1940. arg = arg->next;
  1941. break;
  1942. default:
  1943. printf(">%c<", *ptr);
  1944. }
  1945. } else
  1946. printf("%c", *ptr);
  1947. }
  1948. if (args) {
  1949. free_args(args);
  1950. free(bprint_fmt);
  1951. }
  1952. }
  1953. static inline int log10_cpu(int nb)
  1954. {
  1955. if (nb / 100)
  1956. return 3;
  1957. if (nb / 10)
  1958. return 2;
  1959. return 1;
  1960. }
  1961. /* taken from Linux, written by Frederic Weisbecker */
  1962. static void print_graph_cpu(int cpu)
  1963. {
  1964. int i;
  1965. int log10_this = log10_cpu(cpu);
  1966. int log10_all = log10_cpu(cpus);
  1967. /*
  1968. * Start with a space character - to make it stand out
  1969. * to the right a bit when trace output is pasted into
  1970. * email:
  1971. */
  1972. printf(" ");
  1973. /*
  1974. * Tricky - we space the CPU field according to the max
  1975. * number of online CPUs. On a 2-cpu system it would take
  1976. * a maximum of 1 digit - on a 128 cpu system it would
  1977. * take up to 3 digits:
  1978. */
  1979. for (i = 0; i < log10_all - log10_this; i++)
  1980. printf(" ");
  1981. printf("%d) ", cpu);
  1982. }
  1983. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  1984. #define TRACE_GRAPH_INDENT 2
  1985. static void print_graph_proc(int pid, const char *comm)
  1986. {
  1987. /* sign + log10(MAX_INT) + '\0' */
  1988. char pid_str[11];
  1989. int spaces = 0;
  1990. int len;
  1991. int i;
  1992. sprintf(pid_str, "%d", pid);
  1993. /* 1 stands for the "-" character */
  1994. len = strlen(comm) + strlen(pid_str) + 1;
  1995. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  1996. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  1997. /* First spaces to align center */
  1998. for (i = 0; i < spaces / 2; i++)
  1999. printf(" ");
  2000. printf("%s-%s", comm, pid_str);
  2001. /* Last spaces to align center */
  2002. for (i = 0; i < spaces - (spaces / 2); i++)
  2003. printf(" ");
  2004. }
  2005. static struct record *
  2006. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2007. struct record *next)
  2008. {
  2009. struct format_field *field;
  2010. struct event *event;
  2011. unsigned long val;
  2012. int type;
  2013. int pid;
  2014. type = parse_common_type(next->data);
  2015. event = find_event(type);
  2016. if (!event)
  2017. return NULL;
  2018. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2019. return NULL;
  2020. pid = parse_common_pid(next->data);
  2021. field = find_field(event, "func");
  2022. if (!field)
  2023. die("function return does not have field func");
  2024. val = read_size(next->data + field->offset, field->size);
  2025. if (cur_pid != pid || cur_func != val)
  2026. return NULL;
  2027. /* this is a leaf, now advance the iterator */
  2028. return trace_read_data(cpu);
  2029. }
  2030. /* Signal a overhead of time execution to the output */
  2031. static void print_graph_overhead(unsigned long long duration)
  2032. {
  2033. /* Non nested entry or return */
  2034. if (duration == ~0ULL)
  2035. return (void)printf(" ");
  2036. /* Duration exceeded 100 msecs */
  2037. if (duration > 100000ULL)
  2038. return (void)printf("! ");
  2039. /* Duration exceeded 10 msecs */
  2040. if (duration > 10000ULL)
  2041. return (void)printf("+ ");
  2042. printf(" ");
  2043. }
  2044. static void print_graph_duration(unsigned long long duration)
  2045. {
  2046. unsigned long usecs = duration / 1000;
  2047. unsigned long nsecs_rem = duration % 1000;
  2048. /* log10(ULONG_MAX) + '\0' */
  2049. char msecs_str[21];
  2050. char nsecs_str[5];
  2051. int len;
  2052. int i;
  2053. sprintf(msecs_str, "%lu", usecs);
  2054. /* Print msecs */
  2055. len = printf("%lu", usecs);
  2056. /* Print nsecs (we don't want to exceed 7 numbers) */
  2057. if (len < 7) {
  2058. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2059. len += printf(".%s", nsecs_str);
  2060. }
  2061. printf(" us ");
  2062. /* Print remaining spaces to fit the row's width */
  2063. for (i = len; i < 7; i++)
  2064. printf(" ");
  2065. printf("| ");
  2066. }
  2067. static void
  2068. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2069. {
  2070. unsigned long long rettime, calltime;
  2071. unsigned long long duration, depth;
  2072. unsigned long long val;
  2073. struct format_field *field;
  2074. struct func_map *func;
  2075. struct event *ret_event;
  2076. int type;
  2077. int i;
  2078. type = parse_common_type(ret_rec->data);
  2079. ret_event = find_event(type);
  2080. field = find_field(ret_event, "rettime");
  2081. if (!field)
  2082. die("can't find rettime in return graph");
  2083. rettime = read_size(ret_rec->data + field->offset, field->size);
  2084. field = find_field(ret_event, "calltime");
  2085. if (!field)
  2086. die("can't find rettime in return graph");
  2087. calltime = read_size(ret_rec->data + field->offset, field->size);
  2088. duration = rettime - calltime;
  2089. /* Overhead */
  2090. print_graph_overhead(duration);
  2091. /* Duration */
  2092. print_graph_duration(duration);
  2093. field = find_field(event, "depth");
  2094. if (!field)
  2095. die("can't find depth in entry graph");
  2096. depth = read_size(data + field->offset, field->size);
  2097. /* Function */
  2098. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2099. printf(" ");
  2100. field = find_field(event, "func");
  2101. if (!field)
  2102. die("can't find func in entry graph");
  2103. val = read_size(data + field->offset, field->size);
  2104. func = find_func(val);
  2105. if (func)
  2106. printf("%s();", func->func);
  2107. else
  2108. printf("%llx();", val);
  2109. }
  2110. static void print_graph_nested(struct event *event, void *data)
  2111. {
  2112. struct format_field *field;
  2113. unsigned long long depth;
  2114. unsigned long long val;
  2115. struct func_map *func;
  2116. int i;
  2117. /* No overhead */
  2118. print_graph_overhead(-1);
  2119. /* No time */
  2120. printf(" | ");
  2121. field = find_field(event, "depth");
  2122. if (!field)
  2123. die("can't find depth in entry graph");
  2124. depth = read_size(data + field->offset, field->size);
  2125. /* Function */
  2126. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2127. printf(" ");
  2128. field = find_field(event, "func");
  2129. if (!field)
  2130. die("can't find func in entry graph");
  2131. val = read_size(data + field->offset, field->size);
  2132. func = find_func(val);
  2133. if (func)
  2134. printf("%s() {", func->func);
  2135. else
  2136. printf("%llx() {", val);
  2137. }
  2138. static void
  2139. pretty_print_func_ent(void *data, int size, struct event *event,
  2140. int cpu, int pid, const char *comm,
  2141. unsigned long secs, unsigned long usecs)
  2142. {
  2143. struct format_field *field;
  2144. struct record *rec;
  2145. void *copy_data;
  2146. unsigned long val;
  2147. printf("%5lu.%06lu | ", secs, usecs);
  2148. print_graph_cpu(cpu);
  2149. print_graph_proc(pid, comm);
  2150. printf(" | ");
  2151. field = find_field(event, "func");
  2152. if (!field)
  2153. die("function entry does not have func field");
  2154. val = read_size(data + field->offset, field->size);
  2155. /*
  2156. * peek_data may unmap the data pointer. Copy it first.
  2157. */
  2158. copy_data = malloc_or_die(size);
  2159. memcpy(copy_data, data, size);
  2160. data = copy_data;
  2161. rec = trace_peek_data(cpu);
  2162. if (rec) {
  2163. rec = get_return_for_leaf(cpu, pid, val, rec);
  2164. if (rec) {
  2165. print_graph_entry_leaf(event, data, rec);
  2166. goto out_free;
  2167. }
  2168. }
  2169. print_graph_nested(event, data);
  2170. out_free:
  2171. free(data);
  2172. }
  2173. static void
  2174. pretty_print_func_ret(void *data, int size __unused, struct event *event,
  2175. int cpu, int pid, const char *comm,
  2176. unsigned long secs, unsigned long usecs)
  2177. {
  2178. unsigned long long rettime, calltime;
  2179. unsigned long long duration, depth;
  2180. struct format_field *field;
  2181. int i;
  2182. printf("%5lu.%06lu | ", secs, usecs);
  2183. print_graph_cpu(cpu);
  2184. print_graph_proc(pid, comm);
  2185. printf(" | ");
  2186. field = find_field(event, "rettime");
  2187. if (!field)
  2188. die("can't find rettime in return graph");
  2189. rettime = read_size(data + field->offset, field->size);
  2190. field = find_field(event, "calltime");
  2191. if (!field)
  2192. die("can't find calltime in return graph");
  2193. calltime = read_size(data + field->offset, field->size);
  2194. duration = rettime - calltime;
  2195. /* Overhead */
  2196. print_graph_overhead(duration);
  2197. /* Duration */
  2198. print_graph_duration(duration);
  2199. field = find_field(event, "depth");
  2200. if (!field)
  2201. die("can't find depth in entry graph");
  2202. depth = read_size(data + field->offset, field->size);
  2203. /* Function */
  2204. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2205. printf(" ");
  2206. printf("}");
  2207. }
  2208. static void
  2209. pretty_print_func_graph(void *data, int size, struct event *event,
  2210. int cpu, int pid, const char *comm,
  2211. unsigned long secs, unsigned long usecs)
  2212. {
  2213. if (event->flags & EVENT_FL_ISFUNCENT)
  2214. pretty_print_func_ent(data, size, event,
  2215. cpu, pid, comm, secs, usecs);
  2216. else if (event->flags & EVENT_FL_ISFUNCRET)
  2217. pretty_print_func_ret(data, size, event,
  2218. cpu, pid, comm, secs, usecs);
  2219. printf("\n");
  2220. }
  2221. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  2222. char *comm)
  2223. {
  2224. struct event *event;
  2225. unsigned long secs;
  2226. unsigned long usecs;
  2227. int type;
  2228. int pid;
  2229. secs = nsecs / NSECS_PER_SEC;
  2230. nsecs -= secs * NSECS_PER_SEC;
  2231. usecs = nsecs / NSECS_PER_USEC;
  2232. type = parse_common_type(data);
  2233. event = find_event(type);
  2234. if (!event)
  2235. die("ug! no event found for type %d", type);
  2236. pid = parse_common_pid(data);
  2237. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2238. return pretty_print_func_graph(data, size, event, cpu,
  2239. pid, comm, secs, usecs);
  2240. printf("%16s-%-5d [%03d] %5lu.%06lu: %s: ",
  2241. comm, pid, cpu,
  2242. secs, usecs, event->name);
  2243. pretty_print(data, size, event);
  2244. printf("\n");
  2245. }
  2246. static void print_fields(struct print_flag_sym *field)
  2247. {
  2248. printf("{ %s, %s }", field->value, field->str);
  2249. if (field->next) {
  2250. printf(", ");
  2251. print_fields(field->next);
  2252. }
  2253. }
  2254. static void print_args(struct print_arg *args)
  2255. {
  2256. int print_paren = 1;
  2257. switch (args->type) {
  2258. case PRINT_NULL:
  2259. printf("null");
  2260. break;
  2261. case PRINT_ATOM:
  2262. printf("%s", args->atom.atom);
  2263. break;
  2264. case PRINT_FIELD:
  2265. printf("REC->%s", args->field.name);
  2266. break;
  2267. case PRINT_FLAGS:
  2268. printf("__print_flags(");
  2269. print_args(args->flags.field);
  2270. printf(", %s, ", args->flags.delim);
  2271. print_fields(args->flags.flags);
  2272. printf(")");
  2273. break;
  2274. case PRINT_SYMBOL:
  2275. printf("__print_symbolic(");
  2276. print_args(args->symbol.field);
  2277. printf(", ");
  2278. print_fields(args->symbol.symbols);
  2279. printf(")");
  2280. break;
  2281. case PRINT_STRING:
  2282. printf("__get_str(%s)", args->string.string);
  2283. break;
  2284. case PRINT_TYPE:
  2285. printf("(%s)", args->typecast.type);
  2286. print_args(args->typecast.item);
  2287. break;
  2288. case PRINT_OP:
  2289. if (strcmp(args->op.op, ":") == 0)
  2290. print_paren = 0;
  2291. if (print_paren)
  2292. printf("(");
  2293. print_args(args->op.left);
  2294. printf(" %s ", args->op.op);
  2295. print_args(args->op.right);
  2296. if (print_paren)
  2297. printf(")");
  2298. break;
  2299. default:
  2300. /* we should warn... */
  2301. return;
  2302. }
  2303. if (args->next) {
  2304. printf("\n");
  2305. print_args(args->next);
  2306. }
  2307. }
  2308. static void parse_header_field(char *type,
  2309. int *offset, int *size)
  2310. {
  2311. char *token;
  2312. if (read_expected(EVENT_ITEM, (char *)"field") < 0)
  2313. return;
  2314. if (read_expected(EVENT_OP, (char *)":") < 0)
  2315. return;
  2316. /* type */
  2317. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2318. return;
  2319. free_token(token);
  2320. if (read_expected(EVENT_ITEM, type) < 0)
  2321. return;
  2322. if (read_expected(EVENT_OP, (char *)";") < 0)
  2323. return;
  2324. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  2325. return;
  2326. if (read_expected(EVENT_OP, (char *)":") < 0)
  2327. return;
  2328. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2329. return;
  2330. *offset = atoi(token);
  2331. free_token(token);
  2332. if (read_expected(EVENT_OP, (char *)";") < 0)
  2333. return;
  2334. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  2335. return;
  2336. if (read_expected(EVENT_OP, (char *)":") < 0)
  2337. return;
  2338. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2339. return;
  2340. *size = atoi(token);
  2341. free_token(token);
  2342. if (read_expected(EVENT_OP, (char *)";") < 0)
  2343. return;
  2344. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  2345. return;
  2346. free_token(token);
  2347. }
  2348. int parse_header_page(char *buf, unsigned long size)
  2349. {
  2350. init_input_buf(buf, size);
  2351. parse_header_field((char *)"timestamp", &header_page_ts_offset,
  2352. &header_page_ts_size);
  2353. parse_header_field((char *)"commit", &header_page_size_offset,
  2354. &header_page_size_size);
  2355. parse_header_field((char *)"data", &header_page_data_offset,
  2356. &header_page_data_size);
  2357. return 0;
  2358. }
  2359. int parse_ftrace_file(char *buf, unsigned long size)
  2360. {
  2361. struct format_field *field;
  2362. struct print_arg *arg, **list;
  2363. struct event *event;
  2364. int ret;
  2365. init_input_buf(buf, size);
  2366. event = alloc_event();
  2367. if (!event)
  2368. return -ENOMEM;
  2369. event->flags |= EVENT_FL_ISFTRACE;
  2370. event->name = event_read_name();
  2371. if (!event->name)
  2372. die("failed to read ftrace event name");
  2373. if (strcmp(event->name, "function") == 0)
  2374. event->flags |= EVENT_FL_ISFUNC;
  2375. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2376. event->flags |= EVENT_FL_ISFUNCENT;
  2377. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2378. event->flags |= EVENT_FL_ISFUNCRET;
  2379. else if (strcmp(event->name, "bprint") == 0)
  2380. event->flags |= EVENT_FL_ISBPRINT;
  2381. event->id = event_read_id();
  2382. if (event->id < 0)
  2383. die("failed to read ftrace event id");
  2384. add_event(event);
  2385. ret = event_read_format(event);
  2386. if (ret < 0)
  2387. die("failed to read ftrace event format");
  2388. ret = event_read_print(event);
  2389. if (ret < 0)
  2390. die("failed to read ftrace event print fmt");
  2391. /*
  2392. * The arguments for ftrace files are parsed by the fields.
  2393. * Set up the fields as their arguments.
  2394. */
  2395. list = &event->print_fmt.args;
  2396. for (field = event->format.fields; field; field = field->next) {
  2397. arg = malloc_or_die(sizeof(*arg));
  2398. memset(arg, 0, sizeof(*arg));
  2399. *list = arg;
  2400. list = &arg->next;
  2401. arg->type = PRINT_FIELD;
  2402. arg->field.name = field->name;
  2403. arg->field.field = field;
  2404. }
  2405. return 0;
  2406. }
  2407. int parse_event_file(char *buf, unsigned long size, char *system__unused __unused)
  2408. {
  2409. struct event *event;
  2410. int ret;
  2411. init_input_buf(buf, size);
  2412. event = alloc_event();
  2413. if (!event)
  2414. return -ENOMEM;
  2415. event->name = event_read_name();
  2416. if (!event->name)
  2417. die("failed to read event name");
  2418. event->id = event_read_id();
  2419. if (event->id < 0)
  2420. die("failed to read event id");
  2421. ret = event_read_format(event);
  2422. if (ret < 0)
  2423. die("failed to read event format");
  2424. ret = event_read_print(event);
  2425. if (ret < 0)
  2426. die("failed to read event print fmt");
  2427. #define PRINT_ARGS 0
  2428. if (PRINT_ARGS && event->print_fmt.args)
  2429. print_args(event->print_fmt.args);
  2430. add_event(event);
  2431. return 0;
  2432. }
  2433. void parse_set_info(int nr_cpus, int long_sz)
  2434. {
  2435. cpus = nr_cpus;
  2436. long_size = long_sz;
  2437. }