trace-event-parse.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  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 *)(void *)&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 *)(void *)&addr_str, /* workaround gcc warning */
  135. &ch,
  136. (float *)(void *)&item->func,
  137. (float *)(void *)&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 *)(void *)&addr_str, /* workaround gcc warning */
  246. (float *)(void *)&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 field_is_string(struct format_field *field)
  603. {
  604. if ((field->flags & FIELD_IS_ARRAY) &&
  605. (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
  606. !strstr(field->type, "s8")))
  607. return 1;
  608. return 0;
  609. }
  610. static int field_is_dynamic(struct format_field *field)
  611. {
  612. if (!strcmp(field->type, "__data_loc"))
  613. return 1;
  614. return 0;
  615. }
  616. static int event_read_fields(struct event *event, struct format_field **fields)
  617. {
  618. struct format_field *field = NULL;
  619. enum event_type type;
  620. char *token;
  621. char *last_token;
  622. int count = 0;
  623. do {
  624. type = read_token(&token);
  625. if (type == EVENT_NEWLINE) {
  626. free_token(token);
  627. return count;
  628. }
  629. count++;
  630. if (test_type_token(type, token, EVENT_ITEM, (char *)"field"))
  631. goto fail;
  632. free_token(token);
  633. type = read_token(&token);
  634. /*
  635. * The ftrace fields may still use the "special" name.
  636. * Just ignore it.
  637. */
  638. if (event->flags & EVENT_FL_ISFTRACE &&
  639. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  640. free_token(token);
  641. type = read_token(&token);
  642. }
  643. if (test_type_token(type, token, EVENT_OP, (char *)":") < 0)
  644. return -1;
  645. if (read_expect_type(EVENT_ITEM, &token) < 0)
  646. goto fail;
  647. last_token = token;
  648. field = malloc_or_die(sizeof(*field));
  649. memset(field, 0, sizeof(*field));
  650. /* read the rest of the type */
  651. for (;;) {
  652. type = read_token(&token);
  653. if (type == EVENT_ITEM ||
  654. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  655. /*
  656. * Some of the ftrace fields are broken and have
  657. * an illegal "." in them.
  658. */
  659. (event->flags & EVENT_FL_ISFTRACE &&
  660. type == EVENT_OP && strcmp(token, ".") == 0)) {
  661. if (strcmp(token, "*") == 0)
  662. field->flags |= FIELD_IS_POINTER;
  663. if (field->type) {
  664. field->type = realloc(field->type,
  665. strlen(field->type) +
  666. strlen(last_token) + 2);
  667. strcat(field->type, " ");
  668. strcat(field->type, last_token);
  669. } else
  670. field->type = last_token;
  671. last_token = token;
  672. continue;
  673. }
  674. break;
  675. }
  676. if (!field->type) {
  677. die("no type found");
  678. goto fail;
  679. }
  680. field->name = last_token;
  681. if (test_type(type, EVENT_OP))
  682. goto fail;
  683. if (strcmp(token, "[") == 0) {
  684. enum event_type last_type = type;
  685. char *brackets = token;
  686. int len;
  687. field->flags |= FIELD_IS_ARRAY;
  688. type = read_token(&token);
  689. while (strcmp(token, "]") != 0) {
  690. if (last_type == EVENT_ITEM &&
  691. type == EVENT_ITEM)
  692. len = 2;
  693. else
  694. len = 1;
  695. last_type = type;
  696. brackets = realloc(brackets,
  697. strlen(brackets) +
  698. strlen(token) + len);
  699. if (len == 2)
  700. strcat(brackets, " ");
  701. strcat(brackets, token);
  702. free_token(token);
  703. type = read_token(&token);
  704. if (type == EVENT_NONE) {
  705. die("failed to find token");
  706. goto fail;
  707. }
  708. }
  709. free_token(token);
  710. brackets = realloc(brackets, strlen(brackets) + 2);
  711. strcat(brackets, "]");
  712. /* add brackets to type */
  713. type = read_token(&token);
  714. /*
  715. * If the next token is not an OP, then it is of
  716. * the format: type [] item;
  717. */
  718. if (type == EVENT_ITEM) {
  719. field->type = realloc(field->type,
  720. strlen(field->type) +
  721. strlen(field->name) +
  722. strlen(brackets) + 2);
  723. strcat(field->type, " ");
  724. strcat(field->type, field->name);
  725. free_token(field->name);
  726. strcat(field->type, brackets);
  727. field->name = token;
  728. type = read_token(&token);
  729. } else {
  730. field->type = realloc(field->type,
  731. strlen(field->type) +
  732. strlen(brackets) + 1);
  733. strcat(field->type, brackets);
  734. }
  735. free(brackets);
  736. }
  737. if (field_is_string(field)) {
  738. field->flags |= FIELD_IS_STRING;
  739. if (field_is_dynamic(field))
  740. field->flags |= FIELD_IS_DYNAMIC;
  741. }
  742. if (test_type_token(type, token, EVENT_OP, (char *)";"))
  743. goto fail;
  744. free_token(token);
  745. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  746. goto fail_expect;
  747. if (read_expected(EVENT_OP, (char *)":") < 0)
  748. goto fail_expect;
  749. if (read_expect_type(EVENT_ITEM, &token))
  750. goto fail;
  751. field->offset = strtoul(token, NULL, 0);
  752. free_token(token);
  753. if (read_expected(EVENT_OP, (char *)";") < 0)
  754. goto fail_expect;
  755. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  756. goto fail_expect;
  757. if (read_expected(EVENT_OP, (char *)":") < 0)
  758. goto fail_expect;
  759. if (read_expect_type(EVENT_ITEM, &token))
  760. goto fail;
  761. field->size = strtoul(token, NULL, 0);
  762. free_token(token);
  763. if (read_expected(EVENT_OP, (char *)";") < 0)
  764. goto fail_expect;
  765. if (read_expected(EVENT_ITEM, (char *)"signed") < 0)
  766. goto fail_expect;
  767. if (read_expected(EVENT_OP, (char *)":") < 0)
  768. goto fail_expect;
  769. if (read_expect_type(EVENT_ITEM, &token))
  770. goto fail;
  771. if (strtoul(token, NULL, 0))
  772. field->flags |= FIELD_IS_SIGNED;
  773. free_token(token);
  774. if (read_expected(EVENT_OP, (char *)";") < 0)
  775. goto fail_expect;
  776. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  777. goto fail;
  778. free_token(token);
  779. *fields = field;
  780. fields = &field->next;
  781. } while (1);
  782. return 0;
  783. fail:
  784. free_token(token);
  785. fail_expect:
  786. if (field)
  787. free(field);
  788. return -1;
  789. }
  790. static int event_read_format(struct event *event)
  791. {
  792. char *token;
  793. int ret;
  794. if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
  795. return -1;
  796. if (read_expected(EVENT_OP, (char *)":") < 0)
  797. return -1;
  798. if (read_expect_type(EVENT_NEWLINE, &token))
  799. goto fail;
  800. free_token(token);
  801. ret = event_read_fields(event, &event->format.common_fields);
  802. if (ret < 0)
  803. return ret;
  804. event->format.nr_common = ret;
  805. ret = event_read_fields(event, &event->format.fields);
  806. if (ret < 0)
  807. return ret;
  808. event->format.nr_fields = ret;
  809. return 0;
  810. fail:
  811. free_token(token);
  812. return -1;
  813. }
  814. enum event_type
  815. process_arg_token(struct event *event, struct print_arg *arg,
  816. char **tok, enum event_type type);
  817. static enum event_type
  818. process_arg(struct event *event, struct print_arg *arg, char **tok)
  819. {
  820. enum event_type type;
  821. char *token;
  822. type = read_token(&token);
  823. *tok = token;
  824. return process_arg_token(event, arg, tok, type);
  825. }
  826. static enum event_type
  827. process_cond(struct event *event, struct print_arg *top, char **tok)
  828. {
  829. struct print_arg *arg, *left, *right;
  830. enum event_type type;
  831. char *token = NULL;
  832. arg = malloc_or_die(sizeof(*arg));
  833. memset(arg, 0, sizeof(*arg));
  834. left = malloc_or_die(sizeof(*left));
  835. right = malloc_or_die(sizeof(*right));
  836. arg->type = PRINT_OP;
  837. arg->op.left = left;
  838. arg->op.right = right;
  839. *tok = NULL;
  840. type = process_arg(event, left, &token);
  841. if (test_type_token(type, token, EVENT_OP, (char *)":"))
  842. goto out_free;
  843. arg->op.op = token;
  844. type = process_arg(event, right, &token);
  845. top->op.right = arg;
  846. *tok = token;
  847. return type;
  848. out_free:
  849. free_token(*tok);
  850. free(right);
  851. free(left);
  852. free_arg(arg);
  853. return EVENT_ERROR;
  854. }
  855. static int get_op_prio(char *op)
  856. {
  857. if (!op[1]) {
  858. switch (op[0]) {
  859. case '*':
  860. case '/':
  861. case '%':
  862. return 6;
  863. case '+':
  864. case '-':
  865. return 7;
  866. /* '>>' and '<<' are 8 */
  867. case '<':
  868. case '>':
  869. return 9;
  870. /* '==' and '!=' are 10 */
  871. case '&':
  872. return 11;
  873. case '^':
  874. return 12;
  875. case '|':
  876. return 13;
  877. case '?':
  878. return 16;
  879. default:
  880. die("unknown op '%c'", op[0]);
  881. return -1;
  882. }
  883. } else {
  884. if (strcmp(op, "++") == 0 ||
  885. strcmp(op, "--") == 0) {
  886. return 3;
  887. } else if (strcmp(op, ">>") == 0 ||
  888. strcmp(op, "<<") == 0) {
  889. return 8;
  890. } else if (strcmp(op, ">=") == 0 ||
  891. strcmp(op, "<=") == 0) {
  892. return 9;
  893. } else if (strcmp(op, "==") == 0 ||
  894. strcmp(op, "!=") == 0) {
  895. return 10;
  896. } else if (strcmp(op, "&&") == 0) {
  897. return 14;
  898. } else if (strcmp(op, "||") == 0) {
  899. return 15;
  900. } else {
  901. die("unknown op '%s'", op);
  902. return -1;
  903. }
  904. }
  905. }
  906. static void set_op_prio(struct print_arg *arg)
  907. {
  908. /* single ops are the greatest */
  909. if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
  910. arg->op.prio = 0;
  911. return;
  912. }
  913. arg->op.prio = get_op_prio(arg->op.op);
  914. }
  915. static enum event_type
  916. process_op(struct event *event, struct print_arg *arg, char **tok)
  917. {
  918. struct print_arg *left, *right = NULL;
  919. enum event_type type;
  920. char *token;
  921. /* the op is passed in via tok */
  922. token = *tok;
  923. if (arg->type == PRINT_OP && !arg->op.left) {
  924. /* handle single op */
  925. if (token[1]) {
  926. die("bad op token %s", token);
  927. return EVENT_ERROR;
  928. }
  929. switch (token[0]) {
  930. case '!':
  931. case '+':
  932. case '-':
  933. break;
  934. default:
  935. die("bad op token %s", token);
  936. return EVENT_ERROR;
  937. }
  938. /* make an empty left */
  939. left = malloc_or_die(sizeof(*left));
  940. left->type = PRINT_NULL;
  941. arg->op.left = left;
  942. right = malloc_or_die(sizeof(*right));
  943. arg->op.right = right;
  944. type = process_arg(event, right, tok);
  945. } else if (strcmp(token, "?") == 0) {
  946. left = malloc_or_die(sizeof(*left));
  947. /* copy the top arg to the left */
  948. *left = *arg;
  949. arg->type = PRINT_OP;
  950. arg->op.op = token;
  951. arg->op.left = left;
  952. arg->op.prio = 0;
  953. type = process_cond(event, arg, tok);
  954. } else if (strcmp(token, ">>") == 0 ||
  955. strcmp(token, "<<") == 0 ||
  956. strcmp(token, "&") == 0 ||
  957. strcmp(token, "|") == 0 ||
  958. strcmp(token, "&&") == 0 ||
  959. strcmp(token, "||") == 0 ||
  960. strcmp(token, "-") == 0 ||
  961. strcmp(token, "+") == 0 ||
  962. strcmp(token, "*") == 0 ||
  963. strcmp(token, "^") == 0 ||
  964. strcmp(token, "/") == 0 ||
  965. strcmp(token, "==") == 0 ||
  966. strcmp(token, "!=") == 0) {
  967. left = malloc_or_die(sizeof(*left));
  968. /* copy the top arg to the left */
  969. *left = *arg;
  970. arg->type = PRINT_OP;
  971. arg->op.op = token;
  972. arg->op.left = left;
  973. set_op_prio(arg);
  974. right = malloc_or_die(sizeof(*right));
  975. type = process_arg(event, right, tok);
  976. arg->op.right = right;
  977. } else {
  978. die("unknown op '%s'", token);
  979. /* the arg is now the left side */
  980. return EVENT_NONE;
  981. }
  982. if (type == EVENT_OP) {
  983. int prio;
  984. /* higher prios need to be closer to the root */
  985. prio = get_op_prio(*tok);
  986. if (prio > arg->op.prio)
  987. return process_op(event, arg, tok);
  988. return process_op(event, right, tok);
  989. }
  990. return type;
  991. }
  992. static enum event_type
  993. process_entry(struct event *event __unused, struct print_arg *arg,
  994. char **tok)
  995. {
  996. enum event_type type;
  997. char *field;
  998. char *token;
  999. if (read_expected(EVENT_OP, (char *)"->") < 0)
  1000. return EVENT_ERROR;
  1001. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1002. goto fail;
  1003. field = token;
  1004. arg->type = PRINT_FIELD;
  1005. arg->field.name = field;
  1006. type = read_token(&token);
  1007. *tok = token;
  1008. return type;
  1009. fail:
  1010. free_token(token);
  1011. return EVENT_ERROR;
  1012. }
  1013. static char *arg_eval (struct print_arg *arg);
  1014. static long long arg_num_eval(struct print_arg *arg)
  1015. {
  1016. long long left, right;
  1017. long long val = 0;
  1018. switch (arg->type) {
  1019. case PRINT_ATOM:
  1020. val = strtoll(arg->atom.atom, NULL, 0);
  1021. break;
  1022. case PRINT_TYPE:
  1023. val = arg_num_eval(arg->typecast.item);
  1024. break;
  1025. case PRINT_OP:
  1026. switch (arg->op.op[0]) {
  1027. case '|':
  1028. left = arg_num_eval(arg->op.left);
  1029. right = arg_num_eval(arg->op.right);
  1030. if (arg->op.op[1])
  1031. val = left || right;
  1032. else
  1033. val = left | right;
  1034. break;
  1035. case '&':
  1036. left = arg_num_eval(arg->op.left);
  1037. right = arg_num_eval(arg->op.right);
  1038. if (arg->op.op[1])
  1039. val = left && right;
  1040. else
  1041. val = left & right;
  1042. break;
  1043. case '<':
  1044. left = arg_num_eval(arg->op.left);
  1045. right = arg_num_eval(arg->op.right);
  1046. switch (arg->op.op[1]) {
  1047. case 0:
  1048. val = left < right;
  1049. break;
  1050. case '<':
  1051. val = left << right;
  1052. break;
  1053. case '=':
  1054. val = left <= right;
  1055. break;
  1056. default:
  1057. die("unknown op '%s'", arg->op.op);
  1058. }
  1059. break;
  1060. case '>':
  1061. left = arg_num_eval(arg->op.left);
  1062. right = arg_num_eval(arg->op.right);
  1063. switch (arg->op.op[1]) {
  1064. case 0:
  1065. val = left > right;
  1066. break;
  1067. case '>':
  1068. val = left >> right;
  1069. break;
  1070. case '=':
  1071. val = left >= right;
  1072. break;
  1073. default:
  1074. die("unknown op '%s'", arg->op.op);
  1075. }
  1076. break;
  1077. case '=':
  1078. left = arg_num_eval(arg->op.left);
  1079. right = arg_num_eval(arg->op.right);
  1080. if (arg->op.op[1] != '=')
  1081. die("unknown op '%s'", arg->op.op);
  1082. val = left == right;
  1083. break;
  1084. case '!':
  1085. left = arg_num_eval(arg->op.left);
  1086. right = arg_num_eval(arg->op.right);
  1087. switch (arg->op.op[1]) {
  1088. case '=':
  1089. val = left != right;
  1090. break;
  1091. default:
  1092. die("unknown op '%s'", arg->op.op);
  1093. }
  1094. break;
  1095. default:
  1096. die("unknown op '%s'", arg->op.op);
  1097. }
  1098. break;
  1099. case PRINT_NULL:
  1100. case PRINT_FIELD ... PRINT_SYMBOL:
  1101. case PRINT_STRING:
  1102. default:
  1103. die("invalid eval type %d", arg->type);
  1104. }
  1105. return val;
  1106. }
  1107. static char *arg_eval (struct print_arg *arg)
  1108. {
  1109. long long val;
  1110. static char buf[20];
  1111. switch (arg->type) {
  1112. case PRINT_ATOM:
  1113. return arg->atom.atom;
  1114. case PRINT_TYPE:
  1115. return arg_eval(arg->typecast.item);
  1116. case PRINT_OP:
  1117. val = arg_num_eval(arg);
  1118. sprintf(buf, "%lld", val);
  1119. return buf;
  1120. case PRINT_NULL:
  1121. case PRINT_FIELD ... PRINT_SYMBOL:
  1122. case PRINT_STRING:
  1123. default:
  1124. die("invalid eval type %d", arg->type);
  1125. break;
  1126. }
  1127. return NULL;
  1128. }
  1129. static enum event_type
  1130. process_fields(struct event *event, struct print_flag_sym **list, char **tok)
  1131. {
  1132. enum event_type type;
  1133. struct print_arg *arg = NULL;
  1134. struct print_flag_sym *field;
  1135. char *token = NULL;
  1136. char *value;
  1137. do {
  1138. free_token(token);
  1139. type = read_token_item(&token);
  1140. if (test_type_token(type, token, EVENT_OP, (char *)"{"))
  1141. break;
  1142. arg = malloc_or_die(sizeof(*arg));
  1143. free_token(token);
  1144. type = process_arg(event, arg, &token);
  1145. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1146. goto out_free;
  1147. field = malloc_or_die(sizeof(*field));
  1148. memset(field, 0, sizeof(field));
  1149. value = arg_eval(arg);
  1150. field->value = strdup(value);
  1151. free_token(token);
  1152. type = process_arg(event, arg, &token);
  1153. if (test_type_token(type, token, EVENT_OP, (char *)"}"))
  1154. goto out_free;
  1155. value = arg_eval(arg);
  1156. field->str = strdup(value);
  1157. free_arg(arg);
  1158. arg = NULL;
  1159. *list = field;
  1160. list = &field->next;
  1161. free_token(token);
  1162. type = read_token_item(&token);
  1163. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1164. *tok = token;
  1165. return type;
  1166. out_free:
  1167. free_arg(arg);
  1168. free_token(token);
  1169. return EVENT_ERROR;
  1170. }
  1171. static enum event_type
  1172. process_flags(struct event *event, struct print_arg *arg, char **tok)
  1173. {
  1174. struct print_arg *field;
  1175. enum event_type type;
  1176. char *token;
  1177. memset(arg, 0, sizeof(*arg));
  1178. arg->type = PRINT_FLAGS;
  1179. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1180. return EVENT_ERROR;
  1181. field = malloc_or_die(sizeof(*field));
  1182. type = process_arg(event, field, &token);
  1183. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1184. goto out_free;
  1185. arg->flags.field = field;
  1186. type = read_token_item(&token);
  1187. if (event_item_type(type)) {
  1188. arg->flags.delim = token;
  1189. type = read_token_item(&token);
  1190. }
  1191. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1192. goto out_free;
  1193. type = process_fields(event, &arg->flags.flags, &token);
  1194. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1195. goto out_free;
  1196. free_token(token);
  1197. type = read_token_item(tok);
  1198. return type;
  1199. out_free:
  1200. free_token(token);
  1201. return EVENT_ERROR;
  1202. }
  1203. static enum event_type
  1204. process_symbols(struct event *event, struct print_arg *arg, char **tok)
  1205. {
  1206. struct print_arg *field;
  1207. enum event_type type;
  1208. char *token;
  1209. memset(arg, 0, sizeof(*arg));
  1210. arg->type = PRINT_SYMBOL;
  1211. if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
  1212. return EVENT_ERROR;
  1213. field = malloc_or_die(sizeof(*field));
  1214. type = process_arg(event, field, &token);
  1215. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1216. goto out_free;
  1217. arg->symbol.field = field;
  1218. type = process_fields(event, &arg->symbol.symbols, &token);
  1219. if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
  1220. goto out_free;
  1221. free_token(token);
  1222. type = read_token_item(tok);
  1223. return type;
  1224. out_free:
  1225. free_token(token);
  1226. return EVENT_ERROR;
  1227. }
  1228. static enum event_type
  1229. process_paren(struct event *event, struct print_arg *arg, char **tok)
  1230. {
  1231. struct print_arg *item_arg;
  1232. enum event_type type;
  1233. int ptr_cast = 0;
  1234. char *token;
  1235. type = process_arg(event, arg, &token);
  1236. if (type == EVENT_ERROR)
  1237. return EVENT_ERROR;
  1238. if (type == EVENT_OP) {
  1239. /* handle the ptr casts */
  1240. if (!strcmp(token, "*")) {
  1241. /*
  1242. * FIXME: should we zapp whitespaces before ')' ?
  1243. * (may require a peek_token_item())
  1244. */
  1245. if (__peek_char() == ')') {
  1246. ptr_cast = 1;
  1247. free_token(token);
  1248. type = read_token_item(&token);
  1249. }
  1250. }
  1251. if (!ptr_cast) {
  1252. type = process_op(event, arg, &token);
  1253. if (type == EVENT_ERROR)
  1254. return EVENT_ERROR;
  1255. }
  1256. }
  1257. if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
  1258. free_token(token);
  1259. return EVENT_ERROR;
  1260. }
  1261. free_token(token);
  1262. type = read_token_item(&token);
  1263. /*
  1264. * If the next token is an item or another open paren, then
  1265. * this was a typecast.
  1266. */
  1267. if (event_item_type(type) ||
  1268. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1269. /* make this a typecast and contine */
  1270. /* prevous must be an atom */
  1271. if (arg->type != PRINT_ATOM)
  1272. die("previous needed to be PRINT_ATOM");
  1273. item_arg = malloc_or_die(sizeof(*item_arg));
  1274. arg->type = PRINT_TYPE;
  1275. if (ptr_cast) {
  1276. char *old = arg->atom.atom;
  1277. arg->atom.atom = malloc_or_die(strlen(old + 3));
  1278. sprintf(arg->atom.atom, "%s *", old);
  1279. free(old);
  1280. }
  1281. arg->typecast.type = arg->atom.atom;
  1282. arg->typecast.item = item_arg;
  1283. type = process_arg_token(event, item_arg, &token, type);
  1284. }
  1285. *tok = token;
  1286. return type;
  1287. }
  1288. static enum event_type
  1289. process_str(struct event *event __unused, struct print_arg *arg, char **tok)
  1290. {
  1291. enum event_type type;
  1292. char *token;
  1293. if (read_expected(EVENT_DELIM, (char *)"(") < 0)
  1294. return EVENT_ERROR;
  1295. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1296. goto fail;
  1297. arg->type = PRINT_STRING;
  1298. arg->string.string = token;
  1299. arg->string.offset = -1;
  1300. if (read_expected(EVENT_DELIM, (char *)")") < 0)
  1301. return EVENT_ERROR;
  1302. type = read_token(&token);
  1303. *tok = token;
  1304. return type;
  1305. fail:
  1306. free_token(token);
  1307. return EVENT_ERROR;
  1308. }
  1309. enum event_type
  1310. process_arg_token(struct event *event, struct print_arg *arg,
  1311. char **tok, enum event_type type)
  1312. {
  1313. char *token;
  1314. char *atom;
  1315. token = *tok;
  1316. switch (type) {
  1317. case EVENT_ITEM:
  1318. if (strcmp(token, "REC") == 0) {
  1319. free_token(token);
  1320. type = process_entry(event, arg, &token);
  1321. } else if (strcmp(token, "__print_flags") == 0) {
  1322. free_token(token);
  1323. type = process_flags(event, arg, &token);
  1324. } else if (strcmp(token, "__print_symbolic") == 0) {
  1325. free_token(token);
  1326. type = process_symbols(event, arg, &token);
  1327. } else if (strcmp(token, "__get_str") == 0) {
  1328. free_token(token);
  1329. type = process_str(event, arg, &token);
  1330. } else {
  1331. atom = token;
  1332. /* test the next token */
  1333. type = read_token_item(&token);
  1334. /* atoms can be more than one token long */
  1335. while (type == EVENT_ITEM) {
  1336. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  1337. strcat(atom, " ");
  1338. strcat(atom, token);
  1339. free_token(token);
  1340. type = read_token_item(&token);
  1341. }
  1342. /* todo, test for function */
  1343. arg->type = PRINT_ATOM;
  1344. arg->atom.atom = atom;
  1345. }
  1346. break;
  1347. case EVENT_DQUOTE:
  1348. case EVENT_SQUOTE:
  1349. arg->type = PRINT_ATOM;
  1350. arg->atom.atom = token;
  1351. type = read_token_item(&token);
  1352. break;
  1353. case EVENT_DELIM:
  1354. if (strcmp(token, "(") == 0) {
  1355. free_token(token);
  1356. type = process_paren(event, arg, &token);
  1357. break;
  1358. }
  1359. case EVENT_OP:
  1360. /* handle single ops */
  1361. arg->type = PRINT_OP;
  1362. arg->op.op = token;
  1363. arg->op.left = NULL;
  1364. type = process_op(event, arg, &token);
  1365. break;
  1366. case EVENT_ERROR ... EVENT_NEWLINE:
  1367. default:
  1368. die("unexpected type %d", type);
  1369. }
  1370. *tok = token;
  1371. return type;
  1372. }
  1373. static int event_read_print_args(struct event *event, struct print_arg **list)
  1374. {
  1375. enum event_type type;
  1376. struct print_arg *arg;
  1377. char *token;
  1378. int args = 0;
  1379. do {
  1380. arg = malloc_or_die(sizeof(*arg));
  1381. memset(arg, 0, sizeof(*arg));
  1382. type = process_arg(event, arg, &token);
  1383. if (type == EVENT_ERROR) {
  1384. free_arg(arg);
  1385. return -1;
  1386. }
  1387. *list = arg;
  1388. args++;
  1389. if (type == EVENT_OP) {
  1390. type = process_op(event, arg, &token);
  1391. list = &arg->next;
  1392. continue;
  1393. }
  1394. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  1395. free_token(token);
  1396. *list = arg;
  1397. list = &arg->next;
  1398. continue;
  1399. }
  1400. break;
  1401. } while (type != EVENT_NONE);
  1402. if (type != EVENT_NONE)
  1403. free_token(token);
  1404. return args;
  1405. }
  1406. static int event_read_print(struct event *event)
  1407. {
  1408. enum event_type type;
  1409. char *token;
  1410. int ret;
  1411. if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
  1412. return -1;
  1413. if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
  1414. return -1;
  1415. if (read_expected(EVENT_OP, (char *)":") < 0)
  1416. return -1;
  1417. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  1418. goto fail;
  1419. event->print_fmt.format = token;
  1420. event->print_fmt.args = NULL;
  1421. /* ok to have no arg */
  1422. type = read_token_item(&token);
  1423. if (type == EVENT_NONE)
  1424. return 0;
  1425. if (test_type_token(type, token, EVENT_DELIM, (char *)","))
  1426. goto fail;
  1427. free_token(token);
  1428. ret = event_read_print_args(event, &event->print_fmt.args);
  1429. if (ret < 0)
  1430. return -1;
  1431. return 0;
  1432. fail:
  1433. free_token(token);
  1434. return -1;
  1435. }
  1436. static struct format_field *
  1437. find_common_field(struct event *event, const char *name)
  1438. {
  1439. struct format_field *format;
  1440. for (format = event->format.common_fields;
  1441. format; format = format->next) {
  1442. if (strcmp(format->name, name) == 0)
  1443. break;
  1444. }
  1445. return format;
  1446. }
  1447. static struct format_field *
  1448. find_field(struct event *event, const char *name)
  1449. {
  1450. struct format_field *format;
  1451. for (format = event->format.fields;
  1452. format; format = format->next) {
  1453. if (strcmp(format->name, name) == 0)
  1454. break;
  1455. }
  1456. return format;
  1457. }
  1458. static struct format_field *
  1459. find_any_field(struct event *event, const char *name)
  1460. {
  1461. struct format_field *format;
  1462. format = find_common_field(event, name);
  1463. if (format)
  1464. return format;
  1465. return find_field(event, name);
  1466. }
  1467. static unsigned long long read_size(void *ptr, int size)
  1468. {
  1469. switch (size) {
  1470. case 1:
  1471. return *(unsigned char *)ptr;
  1472. case 2:
  1473. return data2host2(ptr);
  1474. case 4:
  1475. return data2host4(ptr);
  1476. case 8:
  1477. return data2host8(ptr);
  1478. default:
  1479. /* BUG! */
  1480. return 0;
  1481. }
  1482. }
  1483. unsigned long long
  1484. raw_field_value(struct event *event, const char *name, void *data)
  1485. {
  1486. struct format_field *field;
  1487. field = find_any_field(event, name);
  1488. if (!field)
  1489. return 0ULL;
  1490. return read_size(data + field->offset, field->size);
  1491. }
  1492. void *raw_field_ptr(struct event *event, const char *name, void *data)
  1493. {
  1494. struct format_field *field;
  1495. field = find_any_field(event, name);
  1496. if (!field)
  1497. return NULL;
  1498. return data + field->offset;
  1499. }
  1500. static int get_common_info(const char *type, int *offset, int *size)
  1501. {
  1502. struct event *event;
  1503. struct format_field *field;
  1504. /*
  1505. * All events should have the same common elements.
  1506. * Pick any event to find where the type is;
  1507. */
  1508. if (!event_list)
  1509. die("no event_list!");
  1510. event = event_list;
  1511. field = find_common_field(event, type);
  1512. if (!field)
  1513. die("field '%s' not found", type);
  1514. *offset = field->offset;
  1515. *size = field->size;
  1516. return 0;
  1517. }
  1518. int trace_parse_common_type(void *data)
  1519. {
  1520. static int type_offset;
  1521. static int type_size;
  1522. int ret;
  1523. if (!type_size) {
  1524. ret = get_common_info("common_type",
  1525. &type_offset,
  1526. &type_size);
  1527. if (ret < 0)
  1528. return ret;
  1529. }
  1530. return read_size(data + type_offset, type_size);
  1531. }
  1532. static int parse_common_pid(void *data)
  1533. {
  1534. static int pid_offset;
  1535. static int pid_size;
  1536. int ret;
  1537. if (!pid_size) {
  1538. ret = get_common_info("common_pid",
  1539. &pid_offset,
  1540. &pid_size);
  1541. if (ret < 0)
  1542. return ret;
  1543. }
  1544. return read_size(data + pid_offset, pid_size);
  1545. }
  1546. struct event *trace_find_event(int id)
  1547. {
  1548. struct event *event;
  1549. for (event = event_list; event; event = event->next) {
  1550. if (event->id == id)
  1551. break;
  1552. }
  1553. return event;
  1554. }
  1555. static unsigned long long eval_num_arg(void *data, int size,
  1556. struct event *event, struct print_arg *arg)
  1557. {
  1558. unsigned long long val = 0;
  1559. unsigned long long left, right;
  1560. switch (arg->type) {
  1561. case PRINT_NULL:
  1562. /* ?? */
  1563. return 0;
  1564. case PRINT_ATOM:
  1565. return strtoull(arg->atom.atom, NULL, 0);
  1566. case PRINT_FIELD:
  1567. if (!arg->field.field) {
  1568. arg->field.field = find_any_field(event, arg->field.name);
  1569. if (!arg->field.field)
  1570. die("field %s not found", arg->field.name);
  1571. }
  1572. /* must be a number */
  1573. val = read_size(data + arg->field.field->offset,
  1574. arg->field.field->size);
  1575. break;
  1576. case PRINT_FLAGS:
  1577. case PRINT_SYMBOL:
  1578. break;
  1579. case PRINT_TYPE:
  1580. return eval_num_arg(data, size, event, arg->typecast.item);
  1581. case PRINT_STRING:
  1582. return 0;
  1583. break;
  1584. case PRINT_OP:
  1585. left = eval_num_arg(data, size, event, arg->op.left);
  1586. right = eval_num_arg(data, size, event, arg->op.right);
  1587. switch (arg->op.op[0]) {
  1588. case '|':
  1589. if (arg->op.op[1])
  1590. val = left || right;
  1591. else
  1592. val = left | right;
  1593. break;
  1594. case '&':
  1595. if (arg->op.op[1])
  1596. val = left && right;
  1597. else
  1598. val = left & right;
  1599. break;
  1600. case '<':
  1601. switch (arg->op.op[1]) {
  1602. case 0:
  1603. val = left < right;
  1604. break;
  1605. case '<':
  1606. val = left << right;
  1607. break;
  1608. case '=':
  1609. val = left <= right;
  1610. break;
  1611. default:
  1612. die("unknown op '%s'", arg->op.op);
  1613. }
  1614. break;
  1615. case '>':
  1616. switch (arg->op.op[1]) {
  1617. case 0:
  1618. val = left > right;
  1619. break;
  1620. case '>':
  1621. val = left >> right;
  1622. break;
  1623. case '=':
  1624. val = left >= right;
  1625. break;
  1626. default:
  1627. die("unknown op '%s'", arg->op.op);
  1628. }
  1629. break;
  1630. case '=':
  1631. if (arg->op.op[1] != '=')
  1632. die("unknown op '%s'", arg->op.op);
  1633. val = left == right;
  1634. break;
  1635. default:
  1636. die("unknown op '%s'", arg->op.op);
  1637. }
  1638. break;
  1639. default: /* not sure what to do there */
  1640. return 0;
  1641. }
  1642. return val;
  1643. }
  1644. struct flag {
  1645. const char *name;
  1646. unsigned long long value;
  1647. };
  1648. static const struct flag flags[] = {
  1649. { "HI_SOFTIRQ", 0 },
  1650. { "TIMER_SOFTIRQ", 1 },
  1651. { "NET_TX_SOFTIRQ", 2 },
  1652. { "NET_RX_SOFTIRQ", 3 },
  1653. { "BLOCK_SOFTIRQ", 4 },
  1654. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  1655. { "TASKLET_SOFTIRQ", 6 },
  1656. { "SCHED_SOFTIRQ", 7 },
  1657. { "HRTIMER_SOFTIRQ", 8 },
  1658. { "RCU_SOFTIRQ", 9 },
  1659. { "HRTIMER_NORESTART", 0 },
  1660. { "HRTIMER_RESTART", 1 },
  1661. };
  1662. static unsigned long long eval_flag(const char *flag)
  1663. {
  1664. int i;
  1665. /*
  1666. * Some flags in the format files do not get converted.
  1667. * If the flag is not numeric, see if it is something that
  1668. * we already know about.
  1669. */
  1670. if (isdigit(flag[0]))
  1671. return strtoull(flag, NULL, 0);
  1672. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1673. if (strcmp(flags[i].name, flag) == 0)
  1674. return flags[i].value;
  1675. return 0;
  1676. }
  1677. static void print_str_arg(void *data, int size,
  1678. struct event *event, struct print_arg *arg)
  1679. {
  1680. struct print_flag_sym *flag;
  1681. unsigned long long val, fval;
  1682. char *str;
  1683. int print;
  1684. switch (arg->type) {
  1685. case PRINT_NULL:
  1686. /* ?? */
  1687. return;
  1688. case PRINT_ATOM:
  1689. printf("%s", arg->atom.atom);
  1690. return;
  1691. case PRINT_FIELD:
  1692. if (!arg->field.field) {
  1693. arg->field.field = find_any_field(event, arg->field.name);
  1694. if (!arg->field.field)
  1695. die("field %s not found", arg->field.name);
  1696. }
  1697. str = malloc_or_die(arg->field.field->size + 1);
  1698. memcpy(str, data + arg->field.field->offset,
  1699. arg->field.field->size);
  1700. str[arg->field.field->size] = 0;
  1701. printf("%s", str);
  1702. free(str);
  1703. break;
  1704. case PRINT_FLAGS:
  1705. val = eval_num_arg(data, size, event, arg->flags.field);
  1706. print = 0;
  1707. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1708. fval = eval_flag(flag->value);
  1709. if (!val && !fval) {
  1710. printf("%s", flag->str);
  1711. break;
  1712. }
  1713. if (fval && (val & fval) == fval) {
  1714. if (print && arg->flags.delim)
  1715. printf("%s", arg->flags.delim);
  1716. printf("%s", flag->str);
  1717. print = 1;
  1718. val &= ~fval;
  1719. }
  1720. }
  1721. break;
  1722. case PRINT_SYMBOL:
  1723. val = eval_num_arg(data, size, event, arg->symbol.field);
  1724. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1725. fval = eval_flag(flag->value);
  1726. if (val == fval) {
  1727. printf("%s", flag->str);
  1728. break;
  1729. }
  1730. }
  1731. break;
  1732. case PRINT_TYPE:
  1733. break;
  1734. case PRINT_STRING: {
  1735. int str_offset;
  1736. if (arg->string.offset == -1) {
  1737. struct format_field *f;
  1738. f = find_any_field(event, arg->string.string);
  1739. arg->string.offset = f->offset;
  1740. }
  1741. str_offset = *(int *)(data + arg->string.offset);
  1742. str_offset &= 0xffff;
  1743. printf("%s", ((char *)data) + str_offset);
  1744. break;
  1745. }
  1746. case PRINT_OP:
  1747. /*
  1748. * The only op for string should be ? :
  1749. */
  1750. if (arg->op.op[0] != '?')
  1751. return;
  1752. val = eval_num_arg(data, size, event, arg->op.left);
  1753. if (val)
  1754. print_str_arg(data, size, event, arg->op.right->op.left);
  1755. else
  1756. print_str_arg(data, size, event, arg->op.right->op.right);
  1757. break;
  1758. default:
  1759. /* well... */
  1760. break;
  1761. }
  1762. }
  1763. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1764. {
  1765. static struct format_field *field, *ip_field;
  1766. struct print_arg *args, *arg, **next;
  1767. unsigned long long ip, val;
  1768. char *ptr;
  1769. void *bptr;
  1770. if (!field) {
  1771. field = find_field(event, "buf");
  1772. if (!field)
  1773. die("can't find buffer field for binary printk");
  1774. ip_field = find_field(event, "ip");
  1775. if (!ip_field)
  1776. die("can't find ip field for binary printk");
  1777. }
  1778. ip = read_size(data + ip_field->offset, ip_field->size);
  1779. /*
  1780. * The first arg is the IP pointer.
  1781. */
  1782. args = malloc_or_die(sizeof(*args));
  1783. arg = args;
  1784. arg->next = NULL;
  1785. next = &arg->next;
  1786. arg->type = PRINT_ATOM;
  1787. arg->atom.atom = malloc_or_die(32);
  1788. sprintf(arg->atom.atom, "%lld", ip);
  1789. /* skip the first "%pf : " */
  1790. for (ptr = fmt + 6, bptr = data + field->offset;
  1791. bptr < data + size && *ptr; ptr++) {
  1792. int ls = 0;
  1793. if (*ptr == '%') {
  1794. process_again:
  1795. ptr++;
  1796. switch (*ptr) {
  1797. case '%':
  1798. break;
  1799. case 'l':
  1800. ls++;
  1801. goto process_again;
  1802. case 'L':
  1803. ls = 2;
  1804. goto process_again;
  1805. case '0' ... '9':
  1806. goto process_again;
  1807. case 'p':
  1808. ls = 1;
  1809. /* fall through */
  1810. case 'd':
  1811. case 'u':
  1812. case 'x':
  1813. case 'i':
  1814. bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
  1815. ~(long_size - 1));
  1816. switch (ls) {
  1817. case 0:
  1818. case 1:
  1819. ls = long_size;
  1820. break;
  1821. case 2:
  1822. ls = 8;
  1823. default:
  1824. break;
  1825. }
  1826. val = read_size(bptr, ls);
  1827. bptr += ls;
  1828. arg = malloc_or_die(sizeof(*arg));
  1829. arg->next = NULL;
  1830. arg->type = PRINT_ATOM;
  1831. arg->atom.atom = malloc_or_die(32);
  1832. sprintf(arg->atom.atom, "%lld", val);
  1833. *next = arg;
  1834. next = &arg->next;
  1835. break;
  1836. case 's':
  1837. arg = malloc_or_die(sizeof(*arg));
  1838. arg->next = NULL;
  1839. arg->type = PRINT_STRING;
  1840. arg->string.string = strdup(bptr);
  1841. bptr += strlen(bptr) + 1;
  1842. *next = arg;
  1843. next = &arg->next;
  1844. default:
  1845. break;
  1846. }
  1847. }
  1848. }
  1849. return args;
  1850. }
  1851. static void free_args(struct print_arg *args)
  1852. {
  1853. struct print_arg *next;
  1854. while (args) {
  1855. next = args->next;
  1856. if (args->type == PRINT_ATOM)
  1857. free(args->atom.atom);
  1858. else
  1859. free(args->string.string);
  1860. free(args);
  1861. args = next;
  1862. }
  1863. }
  1864. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  1865. {
  1866. unsigned long long addr;
  1867. static struct format_field *field;
  1868. struct printk_map *printk;
  1869. char *format;
  1870. char *p;
  1871. if (!field) {
  1872. field = find_field(event, "fmt");
  1873. if (!field)
  1874. die("can't find format field for binary printk");
  1875. printf("field->offset = %d size=%d\n", field->offset, field->size);
  1876. }
  1877. addr = read_size(data + field->offset, field->size);
  1878. printk = find_printk(addr);
  1879. if (!printk) {
  1880. format = malloc_or_die(45);
  1881. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  1882. addr);
  1883. return format;
  1884. }
  1885. p = printk->printk;
  1886. /* Remove any quotes. */
  1887. if (*p == '"')
  1888. p++;
  1889. format = malloc_or_die(strlen(p) + 10);
  1890. sprintf(format, "%s : %s", "%pf", p);
  1891. /* remove ending quotes and new line since we will add one too */
  1892. p = format + strlen(format) - 1;
  1893. if (*p == '"')
  1894. *p = 0;
  1895. p -= 2;
  1896. if (strcmp(p, "\\n") == 0)
  1897. *p = 0;
  1898. return format;
  1899. }
  1900. static void pretty_print(void *data, int size, struct event *event)
  1901. {
  1902. struct print_fmt *print_fmt = &event->print_fmt;
  1903. struct print_arg *arg = print_fmt->args;
  1904. struct print_arg *args = NULL;
  1905. const char *ptr = print_fmt->format;
  1906. unsigned long long val;
  1907. struct func_map *func;
  1908. const char *saveptr;
  1909. char *bprint_fmt = NULL;
  1910. char format[32];
  1911. int show_func;
  1912. int len;
  1913. int ls;
  1914. if (event->flags & EVENT_FL_ISFUNC)
  1915. ptr = " %pF <-- %pF";
  1916. if (event->flags & EVENT_FL_ISBPRINT) {
  1917. bprint_fmt = get_bprint_format(data, size, event);
  1918. args = make_bprint_args(bprint_fmt, data, size, event);
  1919. arg = args;
  1920. ptr = bprint_fmt;
  1921. }
  1922. for (; *ptr; ptr++) {
  1923. ls = 0;
  1924. if (*ptr == '%') {
  1925. saveptr = ptr;
  1926. show_func = 0;
  1927. cont_process:
  1928. ptr++;
  1929. switch (*ptr) {
  1930. case '%':
  1931. printf("%%");
  1932. break;
  1933. case 'l':
  1934. ls++;
  1935. goto cont_process;
  1936. case 'L':
  1937. ls = 2;
  1938. goto cont_process;
  1939. case 'z':
  1940. case 'Z':
  1941. case '0' ... '9':
  1942. goto cont_process;
  1943. case 'p':
  1944. if (long_size == 4)
  1945. ls = 1;
  1946. else
  1947. ls = 2;
  1948. if (*(ptr+1) == 'F' ||
  1949. *(ptr+1) == 'f') {
  1950. ptr++;
  1951. show_func = *ptr;
  1952. }
  1953. /* fall through */
  1954. case 'd':
  1955. case 'i':
  1956. case 'x':
  1957. case 'X':
  1958. case 'u':
  1959. if (!arg)
  1960. die("no argument match");
  1961. len = ((unsigned long)ptr + 1) -
  1962. (unsigned long)saveptr;
  1963. /* should never happen */
  1964. if (len > 32)
  1965. die("bad format!");
  1966. memcpy(format, saveptr, len);
  1967. format[len] = 0;
  1968. val = eval_num_arg(data, size, event, arg);
  1969. arg = arg->next;
  1970. if (show_func) {
  1971. func = find_func(val);
  1972. if (func) {
  1973. printf("%s", func->func);
  1974. if (show_func == 'F')
  1975. printf("+0x%llx",
  1976. val - func->addr);
  1977. break;
  1978. }
  1979. }
  1980. switch (ls) {
  1981. case 0:
  1982. printf(format, (int)val);
  1983. break;
  1984. case 1:
  1985. printf(format, (long)val);
  1986. break;
  1987. case 2:
  1988. printf(format, (long long)val);
  1989. break;
  1990. default:
  1991. die("bad count (%d)", ls);
  1992. }
  1993. break;
  1994. case 's':
  1995. if (!arg)
  1996. die("no matching argument");
  1997. print_str_arg(data, size, event, arg);
  1998. arg = arg->next;
  1999. break;
  2000. default:
  2001. printf(">%c<", *ptr);
  2002. }
  2003. } else
  2004. printf("%c", *ptr);
  2005. }
  2006. if (args) {
  2007. free_args(args);
  2008. free(bprint_fmt);
  2009. }
  2010. }
  2011. static inline int log10_cpu(int nb)
  2012. {
  2013. if (nb / 100)
  2014. return 3;
  2015. if (nb / 10)
  2016. return 2;
  2017. return 1;
  2018. }
  2019. /* taken from Linux, written by Frederic Weisbecker */
  2020. static void print_graph_cpu(int cpu)
  2021. {
  2022. int i;
  2023. int log10_this = log10_cpu(cpu);
  2024. int log10_all = log10_cpu(cpus);
  2025. /*
  2026. * Start with a space character - to make it stand out
  2027. * to the right a bit when trace output is pasted into
  2028. * email:
  2029. */
  2030. printf(" ");
  2031. /*
  2032. * Tricky - we space the CPU field according to the max
  2033. * number of online CPUs. On a 2-cpu system it would take
  2034. * a maximum of 1 digit - on a 128 cpu system it would
  2035. * take up to 3 digits:
  2036. */
  2037. for (i = 0; i < log10_all - log10_this; i++)
  2038. printf(" ");
  2039. printf("%d) ", cpu);
  2040. }
  2041. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  2042. #define TRACE_GRAPH_INDENT 2
  2043. static void print_graph_proc(int pid, const char *comm)
  2044. {
  2045. /* sign + log10(MAX_INT) + '\0' */
  2046. char pid_str[11];
  2047. int spaces = 0;
  2048. int len;
  2049. int i;
  2050. sprintf(pid_str, "%d", pid);
  2051. /* 1 stands for the "-" character */
  2052. len = strlen(comm) + strlen(pid_str) + 1;
  2053. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  2054. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  2055. /* First spaces to align center */
  2056. for (i = 0; i < spaces / 2; i++)
  2057. printf(" ");
  2058. printf("%s-%s", comm, pid_str);
  2059. /* Last spaces to align center */
  2060. for (i = 0; i < spaces - (spaces / 2); i++)
  2061. printf(" ");
  2062. }
  2063. static struct record *
  2064. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2065. struct record *next)
  2066. {
  2067. struct format_field *field;
  2068. struct event *event;
  2069. unsigned long val;
  2070. int type;
  2071. int pid;
  2072. type = trace_parse_common_type(next->data);
  2073. event = trace_find_event(type);
  2074. if (!event)
  2075. return NULL;
  2076. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2077. return NULL;
  2078. pid = parse_common_pid(next->data);
  2079. field = find_field(event, "func");
  2080. if (!field)
  2081. die("function return does not have field func");
  2082. val = read_size(next->data + field->offset, field->size);
  2083. if (cur_pid != pid || cur_func != val)
  2084. return NULL;
  2085. /* this is a leaf, now advance the iterator */
  2086. return trace_read_data(cpu);
  2087. }
  2088. /* Signal a overhead of time execution to the output */
  2089. static void print_graph_overhead(unsigned long long duration)
  2090. {
  2091. /* Non nested entry or return */
  2092. if (duration == ~0ULL)
  2093. return (void)printf(" ");
  2094. /* Duration exceeded 100 msecs */
  2095. if (duration > 100000ULL)
  2096. return (void)printf("! ");
  2097. /* Duration exceeded 10 msecs */
  2098. if (duration > 10000ULL)
  2099. return (void)printf("+ ");
  2100. printf(" ");
  2101. }
  2102. static void print_graph_duration(unsigned long long duration)
  2103. {
  2104. unsigned long usecs = duration / 1000;
  2105. unsigned long nsecs_rem = duration % 1000;
  2106. /* log10(ULONG_MAX) + '\0' */
  2107. char msecs_str[21];
  2108. char nsecs_str[5];
  2109. int len;
  2110. int i;
  2111. sprintf(msecs_str, "%lu", usecs);
  2112. /* Print msecs */
  2113. len = printf("%lu", usecs);
  2114. /* Print nsecs (we don't want to exceed 7 numbers) */
  2115. if (len < 7) {
  2116. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2117. len += printf(".%s", nsecs_str);
  2118. }
  2119. printf(" us ");
  2120. /* Print remaining spaces to fit the row's width */
  2121. for (i = len; i < 7; i++)
  2122. printf(" ");
  2123. printf("| ");
  2124. }
  2125. static void
  2126. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2127. {
  2128. unsigned long long rettime, calltime;
  2129. unsigned long long duration, depth;
  2130. unsigned long long val;
  2131. struct format_field *field;
  2132. struct func_map *func;
  2133. struct event *ret_event;
  2134. int type;
  2135. int i;
  2136. type = trace_parse_common_type(ret_rec->data);
  2137. ret_event = trace_find_event(type);
  2138. field = find_field(ret_event, "rettime");
  2139. if (!field)
  2140. die("can't find rettime in return graph");
  2141. rettime = read_size(ret_rec->data + field->offset, field->size);
  2142. field = find_field(ret_event, "calltime");
  2143. if (!field)
  2144. die("can't find rettime in return graph");
  2145. calltime = read_size(ret_rec->data + field->offset, field->size);
  2146. duration = rettime - calltime;
  2147. /* Overhead */
  2148. print_graph_overhead(duration);
  2149. /* Duration */
  2150. print_graph_duration(duration);
  2151. field = find_field(event, "depth");
  2152. if (!field)
  2153. die("can't find depth in entry graph");
  2154. depth = read_size(data + field->offset, field->size);
  2155. /* Function */
  2156. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2157. printf(" ");
  2158. field = find_field(event, "func");
  2159. if (!field)
  2160. die("can't find func in entry graph");
  2161. val = read_size(data + field->offset, field->size);
  2162. func = find_func(val);
  2163. if (func)
  2164. printf("%s();", func->func);
  2165. else
  2166. printf("%llx();", val);
  2167. }
  2168. static void print_graph_nested(struct event *event, void *data)
  2169. {
  2170. struct format_field *field;
  2171. unsigned long long depth;
  2172. unsigned long long val;
  2173. struct func_map *func;
  2174. int i;
  2175. /* No overhead */
  2176. print_graph_overhead(-1);
  2177. /* No time */
  2178. printf(" | ");
  2179. field = find_field(event, "depth");
  2180. if (!field)
  2181. die("can't find depth in entry graph");
  2182. depth = read_size(data + field->offset, field->size);
  2183. /* Function */
  2184. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2185. printf(" ");
  2186. field = find_field(event, "func");
  2187. if (!field)
  2188. die("can't find func in entry graph");
  2189. val = read_size(data + field->offset, field->size);
  2190. func = find_func(val);
  2191. if (func)
  2192. printf("%s() {", func->func);
  2193. else
  2194. printf("%llx() {", val);
  2195. }
  2196. static void
  2197. pretty_print_func_ent(void *data, int size, struct event *event,
  2198. int cpu, int pid, const char *comm,
  2199. unsigned long secs, unsigned long usecs)
  2200. {
  2201. struct format_field *field;
  2202. struct record *rec;
  2203. void *copy_data;
  2204. unsigned long val;
  2205. printf("%5lu.%06lu | ", secs, usecs);
  2206. print_graph_cpu(cpu);
  2207. print_graph_proc(pid, comm);
  2208. printf(" | ");
  2209. field = find_field(event, "func");
  2210. if (!field)
  2211. die("function entry does not have func field");
  2212. val = read_size(data + field->offset, field->size);
  2213. /*
  2214. * peek_data may unmap the data pointer. Copy it first.
  2215. */
  2216. copy_data = malloc_or_die(size);
  2217. memcpy(copy_data, data, size);
  2218. data = copy_data;
  2219. rec = trace_peek_data(cpu);
  2220. if (rec) {
  2221. rec = get_return_for_leaf(cpu, pid, val, rec);
  2222. if (rec) {
  2223. print_graph_entry_leaf(event, data, rec);
  2224. goto out_free;
  2225. }
  2226. }
  2227. print_graph_nested(event, data);
  2228. out_free:
  2229. free(data);
  2230. }
  2231. static void
  2232. pretty_print_func_ret(void *data, int size __unused, struct event *event,
  2233. int cpu, int pid, const char *comm,
  2234. unsigned long secs, unsigned long usecs)
  2235. {
  2236. unsigned long long rettime, calltime;
  2237. unsigned long long duration, depth;
  2238. struct format_field *field;
  2239. int i;
  2240. printf("%5lu.%06lu | ", secs, usecs);
  2241. print_graph_cpu(cpu);
  2242. print_graph_proc(pid, comm);
  2243. printf(" | ");
  2244. field = find_field(event, "rettime");
  2245. if (!field)
  2246. die("can't find rettime in return graph");
  2247. rettime = read_size(data + field->offset, field->size);
  2248. field = find_field(event, "calltime");
  2249. if (!field)
  2250. die("can't find calltime in return graph");
  2251. calltime = read_size(data + field->offset, field->size);
  2252. duration = rettime - calltime;
  2253. /* Overhead */
  2254. print_graph_overhead(duration);
  2255. /* Duration */
  2256. print_graph_duration(duration);
  2257. field = find_field(event, "depth");
  2258. if (!field)
  2259. die("can't find depth in entry graph");
  2260. depth = read_size(data + field->offset, field->size);
  2261. /* Function */
  2262. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2263. printf(" ");
  2264. printf("}");
  2265. }
  2266. static void
  2267. pretty_print_func_graph(void *data, int size, struct event *event,
  2268. int cpu, int pid, const char *comm,
  2269. unsigned long secs, unsigned long usecs)
  2270. {
  2271. if (event->flags & EVENT_FL_ISFUNCENT)
  2272. pretty_print_func_ent(data, size, event,
  2273. cpu, pid, comm, secs, usecs);
  2274. else if (event->flags & EVENT_FL_ISFUNCRET)
  2275. pretty_print_func_ret(data, size, event,
  2276. cpu, pid, comm, secs, usecs);
  2277. printf("\n");
  2278. }
  2279. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  2280. char *comm)
  2281. {
  2282. struct event *event;
  2283. unsigned long secs;
  2284. unsigned long usecs;
  2285. int type;
  2286. int pid;
  2287. secs = nsecs / NSECS_PER_SEC;
  2288. nsecs -= secs * NSECS_PER_SEC;
  2289. usecs = nsecs / NSECS_PER_USEC;
  2290. type = trace_parse_common_type(data);
  2291. event = trace_find_event(type);
  2292. if (!event) {
  2293. printf("ug! no event found for type %d\n", type);
  2294. return;
  2295. }
  2296. pid = parse_common_pid(data);
  2297. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2298. return pretty_print_func_graph(data, size, event, cpu,
  2299. pid, comm, secs, usecs);
  2300. printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
  2301. comm, pid, cpu,
  2302. secs, nsecs, event->name);
  2303. pretty_print(data, size, event);
  2304. printf("\n");
  2305. }
  2306. static void print_fields(struct print_flag_sym *field)
  2307. {
  2308. printf("{ %s, %s }", field->value, field->str);
  2309. if (field->next) {
  2310. printf(", ");
  2311. print_fields(field->next);
  2312. }
  2313. }
  2314. static void print_args(struct print_arg *args)
  2315. {
  2316. int print_paren = 1;
  2317. switch (args->type) {
  2318. case PRINT_NULL:
  2319. printf("null");
  2320. break;
  2321. case PRINT_ATOM:
  2322. printf("%s", args->atom.atom);
  2323. break;
  2324. case PRINT_FIELD:
  2325. printf("REC->%s", args->field.name);
  2326. break;
  2327. case PRINT_FLAGS:
  2328. printf("__print_flags(");
  2329. print_args(args->flags.field);
  2330. printf(", %s, ", args->flags.delim);
  2331. print_fields(args->flags.flags);
  2332. printf(")");
  2333. break;
  2334. case PRINT_SYMBOL:
  2335. printf("__print_symbolic(");
  2336. print_args(args->symbol.field);
  2337. printf(", ");
  2338. print_fields(args->symbol.symbols);
  2339. printf(")");
  2340. break;
  2341. case PRINT_STRING:
  2342. printf("__get_str(%s)", args->string.string);
  2343. break;
  2344. case PRINT_TYPE:
  2345. printf("(%s)", args->typecast.type);
  2346. print_args(args->typecast.item);
  2347. break;
  2348. case PRINT_OP:
  2349. if (strcmp(args->op.op, ":") == 0)
  2350. print_paren = 0;
  2351. if (print_paren)
  2352. printf("(");
  2353. print_args(args->op.left);
  2354. printf(" %s ", args->op.op);
  2355. print_args(args->op.right);
  2356. if (print_paren)
  2357. printf(")");
  2358. break;
  2359. default:
  2360. /* we should warn... */
  2361. return;
  2362. }
  2363. if (args->next) {
  2364. printf("\n");
  2365. print_args(args->next);
  2366. }
  2367. }
  2368. static void parse_header_field(char *type,
  2369. int *offset, int *size)
  2370. {
  2371. char *token;
  2372. if (read_expected(EVENT_ITEM, (char *)"field") < 0)
  2373. return;
  2374. if (read_expected(EVENT_OP, (char *)":") < 0)
  2375. return;
  2376. /* type */
  2377. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2378. return;
  2379. free_token(token);
  2380. if (read_expected(EVENT_ITEM, type) < 0)
  2381. return;
  2382. if (read_expected(EVENT_OP, (char *)";") < 0)
  2383. return;
  2384. if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
  2385. return;
  2386. if (read_expected(EVENT_OP, (char *)":") < 0)
  2387. return;
  2388. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2389. return;
  2390. *offset = atoi(token);
  2391. free_token(token);
  2392. if (read_expected(EVENT_OP, (char *)";") < 0)
  2393. return;
  2394. if (read_expected(EVENT_ITEM, (char *)"size") < 0)
  2395. return;
  2396. if (read_expected(EVENT_OP, (char *)":") < 0)
  2397. return;
  2398. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2399. return;
  2400. *size = atoi(token);
  2401. free_token(token);
  2402. if (read_expected(EVENT_OP, (char *)";") < 0)
  2403. return;
  2404. if (read_expected(EVENT_ITEM, (char *)"signed") < 0)
  2405. return;
  2406. if (read_expected(EVENT_OP, (char *)":") < 0)
  2407. return;
  2408. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2409. return;
  2410. free_token(token);
  2411. if (read_expected(EVENT_OP, (char *)";") < 0)
  2412. return;
  2413. if (read_expect_type(EVENT_NEWLINE, &token) < 0)
  2414. return;
  2415. free_token(token);
  2416. }
  2417. int parse_header_page(char *buf, unsigned long size)
  2418. {
  2419. init_input_buf(buf, size);
  2420. parse_header_field((char *)"timestamp", &header_page_ts_offset,
  2421. &header_page_ts_size);
  2422. parse_header_field((char *)"commit", &header_page_size_offset,
  2423. &header_page_size_size);
  2424. parse_header_field((char *)"data", &header_page_data_offset,
  2425. &header_page_data_size);
  2426. return 0;
  2427. }
  2428. int parse_ftrace_file(char *buf, unsigned long size)
  2429. {
  2430. struct format_field *field;
  2431. struct print_arg *arg, **list;
  2432. struct event *event;
  2433. int ret;
  2434. init_input_buf(buf, size);
  2435. event = alloc_event();
  2436. if (!event)
  2437. return -ENOMEM;
  2438. event->flags |= EVENT_FL_ISFTRACE;
  2439. event->name = event_read_name();
  2440. if (!event->name)
  2441. die("failed to read ftrace event name");
  2442. if (strcmp(event->name, "function") == 0)
  2443. event->flags |= EVENT_FL_ISFUNC;
  2444. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2445. event->flags |= EVENT_FL_ISFUNCENT;
  2446. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2447. event->flags |= EVENT_FL_ISFUNCRET;
  2448. else if (strcmp(event->name, "bprint") == 0)
  2449. event->flags |= EVENT_FL_ISBPRINT;
  2450. event->id = event_read_id();
  2451. if (event->id < 0)
  2452. die("failed to read ftrace event id");
  2453. add_event(event);
  2454. ret = event_read_format(event);
  2455. if (ret < 0)
  2456. die("failed to read ftrace event format");
  2457. ret = event_read_print(event);
  2458. if (ret < 0)
  2459. die("failed to read ftrace event print fmt");
  2460. /*
  2461. * The arguments for ftrace files are parsed by the fields.
  2462. * Set up the fields as their arguments.
  2463. */
  2464. list = &event->print_fmt.args;
  2465. for (field = event->format.fields; field; field = field->next) {
  2466. arg = malloc_or_die(sizeof(*arg));
  2467. memset(arg, 0, sizeof(*arg));
  2468. *list = arg;
  2469. list = &arg->next;
  2470. arg->type = PRINT_FIELD;
  2471. arg->field.name = field->name;
  2472. arg->field.field = field;
  2473. }
  2474. return 0;
  2475. }
  2476. int parse_event_file(char *buf, unsigned long size, char *sys)
  2477. {
  2478. struct event *event;
  2479. int ret;
  2480. init_input_buf(buf, size);
  2481. event = alloc_event();
  2482. if (!event)
  2483. return -ENOMEM;
  2484. event->name = event_read_name();
  2485. if (!event->name)
  2486. die("failed to read event name");
  2487. event->id = event_read_id();
  2488. if (event->id < 0)
  2489. die("failed to read event id");
  2490. ret = event_read_format(event);
  2491. if (ret < 0)
  2492. die("failed to read event format");
  2493. ret = event_read_print(event);
  2494. if (ret < 0)
  2495. die("failed to read event print fmt");
  2496. event->system = strdup(sys);
  2497. #define PRINT_ARGS 0
  2498. if (PRINT_ARGS && event->print_fmt.args)
  2499. print_args(event->print_fmt.args);
  2500. add_event(event);
  2501. return 0;
  2502. }
  2503. void parse_set_info(int nr_cpus, int long_sz)
  2504. {
  2505. cpus = nr_cpus;
  2506. long_size = long_sz;
  2507. }