trace-event-parse.c 66 KB

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