trace-event-parse.c 65 KB

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