trace-event-parse.c 62 KB

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