trace-event-parse.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271
  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. static 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. return data + field->offset;
  1569. }
  1570. static int get_common_info(const char *type, int *offset, int *size)
  1571. {
  1572. struct event *event;
  1573. struct format_field *field;
  1574. /*
  1575. * All events should have the same common elements.
  1576. * Pick any event to find where the type is;
  1577. */
  1578. if (!event_list)
  1579. die("no event_list!");
  1580. event = event_list;
  1581. field = find_common_field(event, type);
  1582. if (!field)
  1583. die("field '%s' not found", type);
  1584. *offset = field->offset;
  1585. *size = field->size;
  1586. return 0;
  1587. }
  1588. static int __parse_common(void *data, int *size, int *offset,
  1589. const char *name)
  1590. {
  1591. int ret;
  1592. if (!*size) {
  1593. ret = get_common_info(name, offset, size);
  1594. if (ret < 0)
  1595. return ret;
  1596. }
  1597. return read_size(data + *offset, *size);
  1598. }
  1599. int trace_parse_common_type(void *data)
  1600. {
  1601. static int type_offset;
  1602. static int type_size;
  1603. return __parse_common(data, &type_size, &type_offset,
  1604. "common_type");
  1605. }
  1606. static int parse_common_pid(void *data)
  1607. {
  1608. static int pid_offset;
  1609. static int pid_size;
  1610. return __parse_common(data, &pid_size, &pid_offset,
  1611. "common_pid");
  1612. }
  1613. static int parse_common_pc(void *data)
  1614. {
  1615. static int pc_offset;
  1616. static int pc_size;
  1617. return __parse_common(data, &pc_size, &pc_offset,
  1618. "common_preempt_count");
  1619. }
  1620. static int parse_common_flags(void *data)
  1621. {
  1622. static int flags_offset;
  1623. static int flags_size;
  1624. return __parse_common(data, &flags_size, &flags_offset,
  1625. "common_flags");
  1626. }
  1627. static int parse_common_lock_depth(void *data)
  1628. {
  1629. static int ld_offset;
  1630. static int ld_size;
  1631. int ret;
  1632. ret = __parse_common(data, &ld_size, &ld_offset,
  1633. "common_lock_depth");
  1634. if (ret < 0)
  1635. return -1;
  1636. return ret;
  1637. }
  1638. struct event *trace_find_event(int id)
  1639. {
  1640. struct event *event;
  1641. for (event = event_list; event; event = event->next) {
  1642. if (event->id == id)
  1643. break;
  1644. }
  1645. return event;
  1646. }
  1647. static unsigned long long eval_num_arg(void *data, int size,
  1648. struct event *event, struct print_arg *arg)
  1649. {
  1650. unsigned long long val = 0;
  1651. unsigned long long left, right;
  1652. struct print_arg *larg;
  1653. switch (arg->type) {
  1654. case PRINT_NULL:
  1655. /* ?? */
  1656. return 0;
  1657. case PRINT_ATOM:
  1658. return strtoull(arg->atom.atom, NULL, 0);
  1659. case PRINT_FIELD:
  1660. if (!arg->field.field) {
  1661. arg->field.field = find_any_field(event, arg->field.name);
  1662. if (!arg->field.field)
  1663. die("field %s not found", arg->field.name);
  1664. }
  1665. /* must be a number */
  1666. val = read_size(data + arg->field.field->offset,
  1667. arg->field.field->size);
  1668. break;
  1669. case PRINT_FLAGS:
  1670. case PRINT_SYMBOL:
  1671. break;
  1672. case PRINT_TYPE:
  1673. return eval_num_arg(data, size, event, arg->typecast.item);
  1674. case PRINT_STRING:
  1675. return 0;
  1676. break;
  1677. case PRINT_OP:
  1678. if (strcmp(arg->op.op, "[") == 0) {
  1679. /*
  1680. * Arrays are special, since we don't want
  1681. * to read the arg as is.
  1682. */
  1683. if (arg->op.left->type != PRINT_FIELD)
  1684. goto default_op; /* oops, all bets off */
  1685. larg = arg->op.left;
  1686. if (!larg->field.field) {
  1687. larg->field.field =
  1688. find_any_field(event, larg->field.name);
  1689. if (!larg->field.field)
  1690. die("field %s not found", larg->field.name);
  1691. }
  1692. right = eval_num_arg(data, size, event, arg->op.right);
  1693. val = read_size(data + larg->field.field->offset +
  1694. right * long_size, long_size);
  1695. break;
  1696. }
  1697. default_op:
  1698. left = eval_num_arg(data, size, event, arg->op.left);
  1699. right = eval_num_arg(data, size, event, arg->op.right);
  1700. switch (arg->op.op[0]) {
  1701. case '|':
  1702. if (arg->op.op[1])
  1703. val = left || right;
  1704. else
  1705. val = left | right;
  1706. break;
  1707. case '&':
  1708. if (arg->op.op[1])
  1709. val = left && right;
  1710. else
  1711. val = left & right;
  1712. break;
  1713. case '<':
  1714. switch (arg->op.op[1]) {
  1715. case 0:
  1716. val = left < right;
  1717. break;
  1718. case '<':
  1719. val = left << right;
  1720. break;
  1721. case '=':
  1722. val = left <= right;
  1723. break;
  1724. default:
  1725. die("unknown op '%s'", arg->op.op);
  1726. }
  1727. break;
  1728. case '>':
  1729. switch (arg->op.op[1]) {
  1730. case 0:
  1731. val = left > right;
  1732. break;
  1733. case '>':
  1734. val = left >> right;
  1735. break;
  1736. case '=':
  1737. val = left >= right;
  1738. break;
  1739. default:
  1740. die("unknown op '%s'", arg->op.op);
  1741. }
  1742. break;
  1743. case '=':
  1744. if (arg->op.op[1] != '=')
  1745. die("unknown op '%s'", arg->op.op);
  1746. val = left == right;
  1747. break;
  1748. case '-':
  1749. val = left - right;
  1750. break;
  1751. case '+':
  1752. val = left + right;
  1753. break;
  1754. default:
  1755. die("unknown op '%s'", arg->op.op);
  1756. }
  1757. break;
  1758. default: /* not sure what to do there */
  1759. return 0;
  1760. }
  1761. return val;
  1762. }
  1763. struct flag {
  1764. const char *name;
  1765. unsigned long long value;
  1766. };
  1767. static const struct flag flags[] = {
  1768. { "HI_SOFTIRQ", 0 },
  1769. { "TIMER_SOFTIRQ", 1 },
  1770. { "NET_TX_SOFTIRQ", 2 },
  1771. { "NET_RX_SOFTIRQ", 3 },
  1772. { "BLOCK_SOFTIRQ", 4 },
  1773. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  1774. { "TASKLET_SOFTIRQ", 6 },
  1775. { "SCHED_SOFTIRQ", 7 },
  1776. { "HRTIMER_SOFTIRQ", 8 },
  1777. { "RCU_SOFTIRQ", 9 },
  1778. { "HRTIMER_NORESTART", 0 },
  1779. { "HRTIMER_RESTART", 1 },
  1780. };
  1781. static unsigned long long eval_flag(const char *flag)
  1782. {
  1783. int i;
  1784. /*
  1785. * Some flags in the format files do not get converted.
  1786. * If the flag is not numeric, see if it is something that
  1787. * we already know about.
  1788. */
  1789. if (isdigit(flag[0]))
  1790. return strtoull(flag, NULL, 0);
  1791. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  1792. if (strcmp(flags[i].name, flag) == 0)
  1793. return flags[i].value;
  1794. return 0;
  1795. }
  1796. static void print_str_arg(void *data, int size,
  1797. struct event *event, struct print_arg *arg)
  1798. {
  1799. struct print_flag_sym *flag;
  1800. unsigned long long val, fval;
  1801. char *str;
  1802. int print;
  1803. switch (arg->type) {
  1804. case PRINT_NULL:
  1805. /* ?? */
  1806. return;
  1807. case PRINT_ATOM:
  1808. printf("%s", arg->atom.atom);
  1809. return;
  1810. case PRINT_FIELD:
  1811. if (!arg->field.field) {
  1812. arg->field.field = find_any_field(event, arg->field.name);
  1813. if (!arg->field.field)
  1814. die("field %s not found", arg->field.name);
  1815. }
  1816. str = malloc_or_die(arg->field.field->size + 1);
  1817. memcpy(str, data + arg->field.field->offset,
  1818. arg->field.field->size);
  1819. str[arg->field.field->size] = 0;
  1820. printf("%s", str);
  1821. free(str);
  1822. break;
  1823. case PRINT_FLAGS:
  1824. val = eval_num_arg(data, size, event, arg->flags.field);
  1825. print = 0;
  1826. for (flag = arg->flags.flags; flag; flag = flag->next) {
  1827. fval = eval_flag(flag->value);
  1828. if (!val && !fval) {
  1829. printf("%s", flag->str);
  1830. break;
  1831. }
  1832. if (fval && (val & fval) == fval) {
  1833. if (print && arg->flags.delim)
  1834. printf("%s", arg->flags.delim);
  1835. printf("%s", flag->str);
  1836. print = 1;
  1837. val &= ~fval;
  1838. }
  1839. }
  1840. break;
  1841. case PRINT_SYMBOL:
  1842. val = eval_num_arg(data, size, event, arg->symbol.field);
  1843. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  1844. fval = eval_flag(flag->value);
  1845. if (val == fval) {
  1846. printf("%s", flag->str);
  1847. break;
  1848. }
  1849. }
  1850. break;
  1851. case PRINT_TYPE:
  1852. break;
  1853. case PRINT_STRING: {
  1854. int str_offset;
  1855. if (arg->string.offset == -1) {
  1856. struct format_field *f;
  1857. f = find_any_field(event, arg->string.string);
  1858. arg->string.offset = f->offset;
  1859. }
  1860. str_offset = *(int *)(data + arg->string.offset);
  1861. str_offset &= 0xffff;
  1862. printf("%s", ((char *)data) + str_offset);
  1863. break;
  1864. }
  1865. case PRINT_OP:
  1866. /*
  1867. * The only op for string should be ? :
  1868. */
  1869. if (arg->op.op[0] != '?')
  1870. return;
  1871. val = eval_num_arg(data, size, event, arg->op.left);
  1872. if (val)
  1873. print_str_arg(data, size, event, arg->op.right->op.left);
  1874. else
  1875. print_str_arg(data, size, event, arg->op.right->op.right);
  1876. break;
  1877. default:
  1878. /* well... */
  1879. break;
  1880. }
  1881. }
  1882. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
  1883. {
  1884. static struct format_field *field, *ip_field;
  1885. struct print_arg *args, *arg, **next;
  1886. unsigned long long ip, val;
  1887. char *ptr;
  1888. void *bptr;
  1889. if (!field) {
  1890. field = find_field(event, "buf");
  1891. if (!field)
  1892. die("can't find buffer field for binary printk");
  1893. ip_field = find_field(event, "ip");
  1894. if (!ip_field)
  1895. die("can't find ip field for binary printk");
  1896. }
  1897. ip = read_size(data + ip_field->offset, ip_field->size);
  1898. /*
  1899. * The first arg is the IP pointer.
  1900. */
  1901. args = malloc_or_die(sizeof(*args));
  1902. arg = args;
  1903. arg->next = NULL;
  1904. next = &arg->next;
  1905. arg->type = PRINT_ATOM;
  1906. arg->atom.atom = malloc_or_die(32);
  1907. sprintf(arg->atom.atom, "%lld", ip);
  1908. /* skip the first "%pf : " */
  1909. for (ptr = fmt + 6, bptr = data + field->offset;
  1910. bptr < data + size && *ptr; ptr++) {
  1911. int ls = 0;
  1912. if (*ptr == '%') {
  1913. process_again:
  1914. ptr++;
  1915. switch (*ptr) {
  1916. case '%':
  1917. break;
  1918. case 'l':
  1919. ls++;
  1920. goto process_again;
  1921. case 'L':
  1922. ls = 2;
  1923. goto process_again;
  1924. case '0' ... '9':
  1925. goto process_again;
  1926. case 'p':
  1927. ls = 1;
  1928. /* fall through */
  1929. case 'd':
  1930. case 'u':
  1931. case 'x':
  1932. case 'i':
  1933. /* the pointers are always 4 bytes aligned */
  1934. bptr = (void *)(((unsigned long)bptr + 3) &
  1935. ~3);
  1936. switch (ls) {
  1937. case 0:
  1938. case 1:
  1939. ls = long_size;
  1940. break;
  1941. case 2:
  1942. ls = 8;
  1943. default:
  1944. break;
  1945. }
  1946. val = read_size(bptr, ls);
  1947. bptr += ls;
  1948. arg = malloc_or_die(sizeof(*arg));
  1949. arg->next = NULL;
  1950. arg->type = PRINT_ATOM;
  1951. arg->atom.atom = malloc_or_die(32);
  1952. sprintf(arg->atom.atom, "%lld", val);
  1953. *next = arg;
  1954. next = &arg->next;
  1955. break;
  1956. case 's':
  1957. arg = malloc_or_die(sizeof(*arg));
  1958. arg->next = NULL;
  1959. arg->type = PRINT_STRING;
  1960. arg->string.string = strdup(bptr);
  1961. bptr += strlen(bptr) + 1;
  1962. *next = arg;
  1963. next = &arg->next;
  1964. default:
  1965. break;
  1966. }
  1967. }
  1968. }
  1969. return args;
  1970. }
  1971. static void free_args(struct print_arg *args)
  1972. {
  1973. struct print_arg *next;
  1974. while (args) {
  1975. next = args->next;
  1976. if (args->type == PRINT_ATOM)
  1977. free(args->atom.atom);
  1978. else
  1979. free(args->string.string);
  1980. free(args);
  1981. args = next;
  1982. }
  1983. }
  1984. static char *get_bprint_format(void *data, int size __unused, struct event *event)
  1985. {
  1986. unsigned long long addr;
  1987. static struct format_field *field;
  1988. struct printk_map *printk;
  1989. char *format;
  1990. char *p;
  1991. if (!field) {
  1992. field = find_field(event, "fmt");
  1993. if (!field)
  1994. die("can't find format field for binary printk");
  1995. printf("field->offset = %d size=%d\n", field->offset, field->size);
  1996. }
  1997. addr = read_size(data + field->offset, field->size);
  1998. printk = find_printk(addr);
  1999. if (!printk) {
  2000. format = malloc_or_die(45);
  2001. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  2002. addr);
  2003. return format;
  2004. }
  2005. p = printk->printk;
  2006. /* Remove any quotes. */
  2007. if (*p == '"')
  2008. p++;
  2009. format = malloc_or_die(strlen(p) + 10);
  2010. sprintf(format, "%s : %s", "%pf", p);
  2011. /* remove ending quotes and new line since we will add one too */
  2012. p = format + strlen(format) - 1;
  2013. if (*p == '"')
  2014. *p = 0;
  2015. p -= 2;
  2016. if (strcmp(p, "\\n") == 0)
  2017. *p = 0;
  2018. return format;
  2019. }
  2020. static void pretty_print(void *data, int size, struct event *event)
  2021. {
  2022. struct print_fmt *print_fmt = &event->print_fmt;
  2023. struct print_arg *arg = print_fmt->args;
  2024. struct print_arg *args = NULL;
  2025. const char *ptr = print_fmt->format;
  2026. unsigned long long val;
  2027. struct func_map *func;
  2028. const char *saveptr;
  2029. char *bprint_fmt = NULL;
  2030. char format[32];
  2031. int show_func;
  2032. int len;
  2033. int ls;
  2034. if (event->flags & EVENT_FL_ISFUNC)
  2035. ptr = " %pF <-- %pF";
  2036. if (event->flags & EVENT_FL_ISBPRINT) {
  2037. bprint_fmt = get_bprint_format(data, size, event);
  2038. args = make_bprint_args(bprint_fmt, data, size, event);
  2039. arg = args;
  2040. ptr = bprint_fmt;
  2041. }
  2042. for (; *ptr; ptr++) {
  2043. ls = 0;
  2044. if (*ptr == '\\') {
  2045. ptr++;
  2046. switch (*ptr) {
  2047. case 'n':
  2048. printf("\n");
  2049. break;
  2050. case 't':
  2051. printf("\t");
  2052. break;
  2053. case 'r':
  2054. printf("\r");
  2055. break;
  2056. case '\\':
  2057. printf("\\");
  2058. break;
  2059. default:
  2060. printf("%c", *ptr);
  2061. break;
  2062. }
  2063. } else if (*ptr == '%') {
  2064. saveptr = ptr;
  2065. show_func = 0;
  2066. cont_process:
  2067. ptr++;
  2068. switch (*ptr) {
  2069. case '%':
  2070. printf("%%");
  2071. break;
  2072. case 'l':
  2073. ls++;
  2074. goto cont_process;
  2075. case 'L':
  2076. ls = 2;
  2077. goto cont_process;
  2078. case 'z':
  2079. case 'Z':
  2080. case '0' ... '9':
  2081. goto cont_process;
  2082. case 'p':
  2083. if (long_size == 4)
  2084. ls = 1;
  2085. else
  2086. ls = 2;
  2087. if (*(ptr+1) == 'F' ||
  2088. *(ptr+1) == 'f') {
  2089. ptr++;
  2090. show_func = *ptr;
  2091. }
  2092. /* fall through */
  2093. case 'd':
  2094. case 'i':
  2095. case 'x':
  2096. case 'X':
  2097. case 'u':
  2098. if (!arg)
  2099. die("no argument match");
  2100. len = ((unsigned long)ptr + 1) -
  2101. (unsigned long)saveptr;
  2102. /* should never happen */
  2103. if (len > 32)
  2104. die("bad format!");
  2105. memcpy(format, saveptr, len);
  2106. format[len] = 0;
  2107. val = eval_num_arg(data, size, event, arg);
  2108. arg = arg->next;
  2109. if (show_func) {
  2110. func = find_func(val);
  2111. if (func) {
  2112. printf("%s", func->func);
  2113. if (show_func == 'F')
  2114. printf("+0x%llx",
  2115. val - func->addr);
  2116. break;
  2117. }
  2118. }
  2119. switch (ls) {
  2120. case 0:
  2121. printf(format, (int)val);
  2122. break;
  2123. case 1:
  2124. printf(format, (long)val);
  2125. break;
  2126. case 2:
  2127. printf(format, (long long)val);
  2128. break;
  2129. default:
  2130. die("bad count (%d)", ls);
  2131. }
  2132. break;
  2133. case 's':
  2134. if (!arg)
  2135. die("no matching argument");
  2136. print_str_arg(data, size, event, arg);
  2137. arg = arg->next;
  2138. break;
  2139. default:
  2140. printf(">%c<", *ptr);
  2141. }
  2142. } else
  2143. printf("%c", *ptr);
  2144. }
  2145. if (args) {
  2146. free_args(args);
  2147. free(bprint_fmt);
  2148. }
  2149. }
  2150. static inline int log10_cpu(int nb)
  2151. {
  2152. if (nb / 100)
  2153. return 3;
  2154. if (nb / 10)
  2155. return 2;
  2156. return 1;
  2157. }
  2158. static void print_lat_fmt(void *data, int size __unused)
  2159. {
  2160. unsigned int lat_flags;
  2161. unsigned int pc;
  2162. int lock_depth;
  2163. int hardirq;
  2164. int softirq;
  2165. lat_flags = parse_common_flags(data);
  2166. pc = parse_common_pc(data);
  2167. lock_depth = parse_common_lock_depth(data);
  2168. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  2169. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  2170. printf("%c%c%c",
  2171. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  2172. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  2173. 'X' : '.',
  2174. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  2175. 'N' : '.',
  2176. (hardirq && softirq) ? 'H' :
  2177. hardirq ? 'h' : softirq ? 's' : '.');
  2178. if (pc)
  2179. printf("%x", pc);
  2180. else
  2181. printf(".");
  2182. if (lock_depth < 0)
  2183. printf(".");
  2184. else
  2185. printf("%d", lock_depth);
  2186. }
  2187. /* taken from Linux, written by Frederic Weisbecker */
  2188. static void print_graph_cpu(int cpu)
  2189. {
  2190. int i;
  2191. int log10_this = log10_cpu(cpu);
  2192. int log10_all = log10_cpu(cpus);
  2193. /*
  2194. * Start with a space character - to make it stand out
  2195. * to the right a bit when trace output is pasted into
  2196. * email:
  2197. */
  2198. printf(" ");
  2199. /*
  2200. * Tricky - we space the CPU field according to the max
  2201. * number of online CPUs. On a 2-cpu system it would take
  2202. * a maximum of 1 digit - on a 128 cpu system it would
  2203. * take up to 3 digits:
  2204. */
  2205. for (i = 0; i < log10_all - log10_this; i++)
  2206. printf(" ");
  2207. printf("%d) ", cpu);
  2208. }
  2209. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  2210. #define TRACE_GRAPH_INDENT 2
  2211. static void print_graph_proc(int pid, const char *comm)
  2212. {
  2213. /* sign + log10(MAX_INT) + '\0' */
  2214. char pid_str[11];
  2215. int spaces = 0;
  2216. int len;
  2217. int i;
  2218. sprintf(pid_str, "%d", pid);
  2219. /* 1 stands for the "-" character */
  2220. len = strlen(comm) + strlen(pid_str) + 1;
  2221. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  2222. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  2223. /* First spaces to align center */
  2224. for (i = 0; i < spaces / 2; i++)
  2225. printf(" ");
  2226. printf("%s-%s", comm, pid_str);
  2227. /* Last spaces to align center */
  2228. for (i = 0; i < spaces - (spaces / 2); i++)
  2229. printf(" ");
  2230. }
  2231. static struct record *
  2232. get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
  2233. struct record *next)
  2234. {
  2235. struct format_field *field;
  2236. struct event *event;
  2237. unsigned long val;
  2238. int type;
  2239. int pid;
  2240. type = trace_parse_common_type(next->data);
  2241. event = trace_find_event(type);
  2242. if (!event)
  2243. return NULL;
  2244. if (!(event->flags & EVENT_FL_ISFUNCRET))
  2245. return NULL;
  2246. pid = parse_common_pid(next->data);
  2247. field = find_field(event, "func");
  2248. if (!field)
  2249. die("function return does not have field func");
  2250. val = read_size(next->data + field->offset, field->size);
  2251. if (cur_pid != pid || cur_func != val)
  2252. return NULL;
  2253. /* this is a leaf, now advance the iterator */
  2254. return trace_read_data(cpu);
  2255. }
  2256. /* Signal a overhead of time execution to the output */
  2257. static void print_graph_overhead(unsigned long long duration)
  2258. {
  2259. /* Non nested entry or return */
  2260. if (duration == ~0ULL)
  2261. return (void)printf(" ");
  2262. /* Duration exceeded 100 msecs */
  2263. if (duration > 100000ULL)
  2264. return (void)printf("! ");
  2265. /* Duration exceeded 10 msecs */
  2266. if (duration > 10000ULL)
  2267. return (void)printf("+ ");
  2268. printf(" ");
  2269. }
  2270. static void print_graph_duration(unsigned long long duration)
  2271. {
  2272. unsigned long usecs = duration / 1000;
  2273. unsigned long nsecs_rem = duration % 1000;
  2274. /* log10(ULONG_MAX) + '\0' */
  2275. char msecs_str[21];
  2276. char nsecs_str[5];
  2277. int len;
  2278. int i;
  2279. sprintf(msecs_str, "%lu", usecs);
  2280. /* Print msecs */
  2281. len = printf("%lu", usecs);
  2282. /* Print nsecs (we don't want to exceed 7 numbers) */
  2283. if (len < 7) {
  2284. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  2285. len += printf(".%s", nsecs_str);
  2286. }
  2287. printf(" us ");
  2288. /* Print remaining spaces to fit the row's width */
  2289. for (i = len; i < 7; i++)
  2290. printf(" ");
  2291. printf("| ");
  2292. }
  2293. static void
  2294. print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
  2295. {
  2296. unsigned long long rettime, calltime;
  2297. unsigned long long duration, depth;
  2298. unsigned long long val;
  2299. struct format_field *field;
  2300. struct func_map *func;
  2301. struct event *ret_event;
  2302. int type;
  2303. int i;
  2304. type = trace_parse_common_type(ret_rec->data);
  2305. ret_event = trace_find_event(type);
  2306. field = find_field(ret_event, "rettime");
  2307. if (!field)
  2308. die("can't find rettime in return graph");
  2309. rettime = read_size(ret_rec->data + field->offset, field->size);
  2310. field = find_field(ret_event, "calltime");
  2311. if (!field)
  2312. die("can't find rettime in return graph");
  2313. calltime = read_size(ret_rec->data + field->offset, field->size);
  2314. duration = rettime - calltime;
  2315. /* Overhead */
  2316. print_graph_overhead(duration);
  2317. /* Duration */
  2318. print_graph_duration(duration);
  2319. field = find_field(event, "depth");
  2320. if (!field)
  2321. die("can't find depth in entry graph");
  2322. depth = read_size(data + field->offset, field->size);
  2323. /* Function */
  2324. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2325. printf(" ");
  2326. field = find_field(event, "func");
  2327. if (!field)
  2328. die("can't find func in entry graph");
  2329. val = read_size(data + field->offset, field->size);
  2330. func = find_func(val);
  2331. if (func)
  2332. printf("%s();", func->func);
  2333. else
  2334. printf("%llx();", val);
  2335. }
  2336. static void print_graph_nested(struct event *event, void *data)
  2337. {
  2338. struct format_field *field;
  2339. unsigned long long depth;
  2340. unsigned long long val;
  2341. struct func_map *func;
  2342. int i;
  2343. /* No overhead */
  2344. print_graph_overhead(-1);
  2345. /* No time */
  2346. printf(" | ");
  2347. field = find_field(event, "depth");
  2348. if (!field)
  2349. die("can't find depth in entry graph");
  2350. depth = read_size(data + field->offset, field->size);
  2351. /* Function */
  2352. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2353. printf(" ");
  2354. field = find_field(event, "func");
  2355. if (!field)
  2356. die("can't find func in entry graph");
  2357. val = read_size(data + field->offset, field->size);
  2358. func = find_func(val);
  2359. if (func)
  2360. printf("%s() {", func->func);
  2361. else
  2362. printf("%llx() {", val);
  2363. }
  2364. static void
  2365. pretty_print_func_ent(void *data, int size, struct event *event,
  2366. int cpu, int pid, const char *comm,
  2367. unsigned long secs, unsigned long usecs)
  2368. {
  2369. struct format_field *field;
  2370. struct record *rec;
  2371. void *copy_data;
  2372. unsigned long val;
  2373. printf("%5lu.%06lu | ", secs, usecs);
  2374. print_graph_cpu(cpu);
  2375. print_graph_proc(pid, comm);
  2376. printf(" | ");
  2377. if (latency_format) {
  2378. print_lat_fmt(data, size);
  2379. printf(" | ");
  2380. }
  2381. field = find_field(event, "func");
  2382. if (!field)
  2383. die("function entry does not have func field");
  2384. val = read_size(data + field->offset, field->size);
  2385. /*
  2386. * peek_data may unmap the data pointer. Copy it first.
  2387. */
  2388. copy_data = malloc_or_die(size);
  2389. memcpy(copy_data, data, size);
  2390. data = copy_data;
  2391. rec = trace_peek_data(cpu);
  2392. if (rec) {
  2393. rec = get_return_for_leaf(cpu, pid, val, rec);
  2394. if (rec) {
  2395. print_graph_entry_leaf(event, data, rec);
  2396. goto out_free;
  2397. }
  2398. }
  2399. print_graph_nested(event, data);
  2400. out_free:
  2401. free(data);
  2402. }
  2403. static void
  2404. pretty_print_func_ret(void *data, int size __unused, struct event *event,
  2405. int cpu, int pid, const char *comm,
  2406. unsigned long secs, unsigned long usecs)
  2407. {
  2408. unsigned long long rettime, calltime;
  2409. unsigned long long duration, depth;
  2410. struct format_field *field;
  2411. int i;
  2412. printf("%5lu.%06lu | ", secs, usecs);
  2413. print_graph_cpu(cpu);
  2414. print_graph_proc(pid, comm);
  2415. printf(" | ");
  2416. if (latency_format) {
  2417. print_lat_fmt(data, size);
  2418. printf(" | ");
  2419. }
  2420. field = find_field(event, "rettime");
  2421. if (!field)
  2422. die("can't find rettime in return graph");
  2423. rettime = read_size(data + field->offset, field->size);
  2424. field = find_field(event, "calltime");
  2425. if (!field)
  2426. die("can't find calltime in return graph");
  2427. calltime = read_size(data + field->offset, field->size);
  2428. duration = rettime - calltime;
  2429. /* Overhead */
  2430. print_graph_overhead(duration);
  2431. /* Duration */
  2432. print_graph_duration(duration);
  2433. field = find_field(event, "depth");
  2434. if (!field)
  2435. die("can't find depth in entry graph");
  2436. depth = read_size(data + field->offset, field->size);
  2437. /* Function */
  2438. for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
  2439. printf(" ");
  2440. printf("}");
  2441. }
  2442. static void
  2443. pretty_print_func_graph(void *data, int size, struct event *event,
  2444. int cpu, int pid, const char *comm,
  2445. unsigned long secs, unsigned long usecs)
  2446. {
  2447. if (event->flags & EVENT_FL_ISFUNCENT)
  2448. pretty_print_func_ent(data, size, event,
  2449. cpu, pid, comm, secs, usecs);
  2450. else if (event->flags & EVENT_FL_ISFUNCRET)
  2451. pretty_print_func_ret(data, size, event,
  2452. cpu, pid, comm, secs, usecs);
  2453. printf("\n");
  2454. }
  2455. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  2456. char *comm)
  2457. {
  2458. struct event *event;
  2459. unsigned long secs;
  2460. unsigned long usecs;
  2461. int type;
  2462. int pid;
  2463. secs = nsecs / NSECS_PER_SEC;
  2464. nsecs -= secs * NSECS_PER_SEC;
  2465. usecs = nsecs / NSECS_PER_USEC;
  2466. type = trace_parse_common_type(data);
  2467. event = trace_find_event(type);
  2468. if (!event) {
  2469. warning("ug! no event found for type %d", type);
  2470. return;
  2471. }
  2472. pid = parse_common_pid(data);
  2473. if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
  2474. return pretty_print_func_graph(data, size, event, cpu,
  2475. pid, comm, secs, usecs);
  2476. if (latency_format) {
  2477. printf("%8.8s-%-5d %3d",
  2478. comm, pid, cpu);
  2479. print_lat_fmt(data, size);
  2480. } else
  2481. printf("%16s-%-5d [%03d]", comm, pid, cpu);
  2482. printf(" %5lu.%06lu: %s: ", secs, usecs, event->name);
  2483. if (event->flags & EVENT_FL_FAILED) {
  2484. printf("EVENT '%s' FAILED TO PARSE\n",
  2485. event->name);
  2486. return;
  2487. }
  2488. pretty_print(data, size, event);
  2489. printf("\n");
  2490. }
  2491. static void print_fields(struct print_flag_sym *field)
  2492. {
  2493. printf("{ %s, %s }", field->value, field->str);
  2494. if (field->next) {
  2495. printf(", ");
  2496. print_fields(field->next);
  2497. }
  2498. }
  2499. static void print_args(struct print_arg *args)
  2500. {
  2501. int print_paren = 1;
  2502. switch (args->type) {
  2503. case PRINT_NULL:
  2504. printf("null");
  2505. break;
  2506. case PRINT_ATOM:
  2507. printf("%s", args->atom.atom);
  2508. break;
  2509. case PRINT_FIELD:
  2510. printf("REC->%s", args->field.name);
  2511. break;
  2512. case PRINT_FLAGS:
  2513. printf("__print_flags(");
  2514. print_args(args->flags.field);
  2515. printf(", %s, ", args->flags.delim);
  2516. print_fields(args->flags.flags);
  2517. printf(")");
  2518. break;
  2519. case PRINT_SYMBOL:
  2520. printf("__print_symbolic(");
  2521. print_args(args->symbol.field);
  2522. printf(", ");
  2523. print_fields(args->symbol.symbols);
  2524. printf(")");
  2525. break;
  2526. case PRINT_STRING:
  2527. printf("__get_str(%s)", args->string.string);
  2528. break;
  2529. case PRINT_TYPE:
  2530. printf("(%s)", args->typecast.type);
  2531. print_args(args->typecast.item);
  2532. break;
  2533. case PRINT_OP:
  2534. if (strcmp(args->op.op, ":") == 0)
  2535. print_paren = 0;
  2536. if (print_paren)
  2537. printf("(");
  2538. print_args(args->op.left);
  2539. printf(" %s ", args->op.op);
  2540. print_args(args->op.right);
  2541. if (print_paren)
  2542. printf(")");
  2543. break;
  2544. default:
  2545. /* we should warn... */
  2546. return;
  2547. }
  2548. if (args->next) {
  2549. printf("\n");
  2550. print_args(args->next);
  2551. }
  2552. }
  2553. static void parse_header_field(const char *field,
  2554. int *offset, int *size)
  2555. {
  2556. char *token;
  2557. int type;
  2558. if (read_expected(EVENT_ITEM, "field") < 0)
  2559. return;
  2560. if (read_expected(EVENT_OP, ":") < 0)
  2561. return;
  2562. /* type */
  2563. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2564. goto fail;
  2565. free_token(token);
  2566. if (read_expected(EVENT_ITEM, field) < 0)
  2567. return;
  2568. if (read_expected(EVENT_OP, ";") < 0)
  2569. return;
  2570. if (read_expected(EVENT_ITEM, "offset") < 0)
  2571. return;
  2572. if (read_expected(EVENT_OP, ":") < 0)
  2573. return;
  2574. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2575. goto fail;
  2576. *offset = atoi(token);
  2577. free_token(token);
  2578. if (read_expected(EVENT_OP, ";") < 0)
  2579. return;
  2580. if (read_expected(EVENT_ITEM, "size") < 0)
  2581. return;
  2582. if (read_expected(EVENT_OP, ":") < 0)
  2583. return;
  2584. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2585. goto fail;
  2586. *size = atoi(token);
  2587. free_token(token);
  2588. if (read_expected(EVENT_OP, ";") < 0)
  2589. return;
  2590. type = read_token(&token);
  2591. if (type != EVENT_NEWLINE) {
  2592. /* newer versions of the kernel have a "signed" type */
  2593. if (type != EVENT_ITEM)
  2594. goto fail;
  2595. if (strcmp(token, "signed") != 0)
  2596. goto fail;
  2597. free_token(token);
  2598. if (read_expected(EVENT_OP, ":") < 0)
  2599. return;
  2600. if (read_expect_type(EVENT_ITEM, &token))
  2601. goto fail;
  2602. free_token(token);
  2603. if (read_expected(EVENT_OP, ";") < 0)
  2604. return;
  2605. if (read_expect_type(EVENT_NEWLINE, &token))
  2606. goto fail;
  2607. }
  2608. fail:
  2609. free_token(token);
  2610. }
  2611. int parse_header_page(char *buf, unsigned long size)
  2612. {
  2613. init_input_buf(buf, size);
  2614. parse_header_field("timestamp", &header_page_ts_offset,
  2615. &header_page_ts_size);
  2616. parse_header_field("commit", &header_page_size_offset,
  2617. &header_page_size_size);
  2618. parse_header_field("data", &header_page_data_offset,
  2619. &header_page_data_size);
  2620. return 0;
  2621. }
  2622. int parse_ftrace_file(char *buf, unsigned long size)
  2623. {
  2624. struct format_field *field;
  2625. struct print_arg *arg, **list;
  2626. struct event *event;
  2627. int ret;
  2628. init_input_buf(buf, size);
  2629. event = alloc_event();
  2630. if (!event)
  2631. return -ENOMEM;
  2632. event->flags |= EVENT_FL_ISFTRACE;
  2633. event->name = event_read_name();
  2634. if (!event->name)
  2635. die("failed to read ftrace event name");
  2636. if (strcmp(event->name, "function") == 0)
  2637. event->flags |= EVENT_FL_ISFUNC;
  2638. else if (strcmp(event->name, "funcgraph_entry") == 0)
  2639. event->flags |= EVENT_FL_ISFUNCENT;
  2640. else if (strcmp(event->name, "funcgraph_exit") == 0)
  2641. event->flags |= EVENT_FL_ISFUNCRET;
  2642. else if (strcmp(event->name, "bprint") == 0)
  2643. event->flags |= EVENT_FL_ISBPRINT;
  2644. event->id = event_read_id();
  2645. if (event->id < 0)
  2646. die("failed to read ftrace event id");
  2647. add_event(event);
  2648. ret = event_read_format(event);
  2649. if (ret < 0)
  2650. die("failed to read ftrace event format");
  2651. ret = event_read_print(event);
  2652. if (ret < 0)
  2653. die("failed to read ftrace event print fmt");
  2654. /* New ftrace handles args */
  2655. if (ret > 0)
  2656. return 0;
  2657. /*
  2658. * The arguments for ftrace files are parsed by the fields.
  2659. * Set up the fields as their arguments.
  2660. */
  2661. list = &event->print_fmt.args;
  2662. for (field = event->format.fields; field; field = field->next) {
  2663. arg = malloc_or_die(sizeof(*arg));
  2664. memset(arg, 0, sizeof(*arg));
  2665. *list = arg;
  2666. list = &arg->next;
  2667. arg->type = PRINT_FIELD;
  2668. arg->field.name = field->name;
  2669. arg->field.field = field;
  2670. }
  2671. return 0;
  2672. }
  2673. int parse_event_file(char *buf, unsigned long size, char *sys)
  2674. {
  2675. struct event *event;
  2676. int ret;
  2677. init_input_buf(buf, size);
  2678. event = alloc_event();
  2679. if (!event)
  2680. return -ENOMEM;
  2681. event->name = event_read_name();
  2682. if (!event->name)
  2683. die("failed to read event name");
  2684. event->id = event_read_id();
  2685. if (event->id < 0)
  2686. die("failed to read event id");
  2687. ret = event_read_format(event);
  2688. if (ret < 0) {
  2689. warning("failed to read event format for %s", event->name);
  2690. goto event_failed;
  2691. }
  2692. ret = event_read_print(event);
  2693. if (ret < 0) {
  2694. warning("failed to read event print fmt for %s", event->name);
  2695. goto event_failed;
  2696. }
  2697. event->system = strdup(sys);
  2698. #define PRINT_ARGS 0
  2699. if (PRINT_ARGS && event->print_fmt.args)
  2700. print_args(event->print_fmt.args);
  2701. add_event(event);
  2702. return 0;
  2703. event_failed:
  2704. event->flags |= EVENT_FL_FAILED;
  2705. /* still add it even if it failed */
  2706. add_event(event);
  2707. return -1;
  2708. }
  2709. void parse_set_info(int nr_cpus, int long_sz)
  2710. {
  2711. cpus = nr_cpus;
  2712. long_size = long_sz;
  2713. }