trace-event-parse.c 64 KB

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