trace-event-parse.c 58 KB

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