trace-event-parse.c 64 KB

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