trace-event-parse.c 64 KB

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