trace-parse-events.c 61 KB

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