trace-event-parse.c 63 KB

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