blob: f4dd4cb7c1ab27a46f1d1c595ea1bbc799a365d6 [file] [log] [blame]
Rudolf Marek8679e522011-09-02 23:34:15 +02001#include <stdio.h>
2#include <uthash.h>
3#include <sys/gmon_out.h>
4#include <stdlib.h>
5
6#define GMON_SEC "seconds s"
7uint32_t mineip = 0xffffffff;
8uint32_t maxeip = 0;
9
10/* a hash structure to hold the arc */
11struct arec {
12 uint32_t eip;
13 uint32_t from;
14 uint32_t count;
15 UT_hash_handle hh;
16};
17
18struct arec *arc = NULL;
19
20void note_arc(uint32_t eip, uint32_t from)
21{
22 struct arec *s;
23
24 HASH_FIND_INT(arc, &eip, s);
25 if (s == NULL) {
26 s = malloc(sizeof(struct arec));
27 s->eip = eip;
28 s->from = from;
29 s->count = 1;
30 if (eip > maxeip)
31 maxeip = eip;
32 if (eip < mineip)
33 maxeip = eip;
34
35 HASH_ADD_INT(arc, eip, s);
36 } else {
37 s->count++;
38 }
39}
40
41int main(int argc, char* argv[])
42{
43 FILE *f, *fo;
44 struct arec *s;
45 uint32_t eip, from, tmp;
46 uint8_t tag;
47 uint16_t hit;
48
Daniele Forsiea70a502014-08-03 13:47:58 +020049 if (argc != 2) {
Rudolf Marek8679e522011-09-02 23:34:15 +020050 fprintf(stderr, "Please specify the coreboot trace log as parameter\n");
51 return 1;
52 }
53
54 f = fopen(argv[1], "r");
Daniele Forsiea70a502014-08-03 13:47:58 +020055 if (f == NULL) {
56 perror("Unable to open the input file");
57 return 1;
58 }
Rudolf Marek8679e522011-09-02 23:34:15 +020059
Daniele Forsiea70a502014-08-03 13:47:58 +020060 fo = fopen("gmon.out", "w+");
61 if (fo == NULL) {
62 perror("Unable to open the output file");
63 fclose(f);
Rudolf Marek8679e522011-09-02 23:34:15 +020064 return 1;
65 }
66
67 while (!feof(f)) {
68 if (fscanf(f, "~%x(%x)%*[^\n]\n", &eip, &from) == 2) {
69 note_arc(eip, from);
70 } else if (fscanf(f, "%*c~%x(%x)%*[^\n]\n", &eip, &from) == 2) {
71 note_arc(eip, from);
72 } else {
73 /* just drop a line */
74 tmp = fscanf(f, "%*[^\n]\n");
75 }
76 }
77
78 /* write gprof header */
79 fwrite(GMON_MAGIC, 1, sizeof(GMON_MAGIC) - 1, fo);
80 tmp = GMON_VERSION;
81 fwrite(&tmp, 1, sizeof(tmp), fo);
82 tmp = 0;
83 fwrite(&tmp, 1, sizeof(tmp), fo);
84 fwrite(&tmp, 1, sizeof(tmp), fo);
85 fwrite(&tmp, 1, sizeof(tmp), fo);
86 /* write fake histogram */
87 tag = GMON_TAG_TIME_HIST;
88 fwrite(&tag, 1, sizeof(tag), fo);
89 fwrite(&mineip, 1, sizeof(mineip), fo);
90 fwrite(&maxeip, 1, sizeof(maxeip), fo);
91 /* size of histogram */
92 tmp = 1;
93 fwrite(&tmp, 1, sizeof(tmp), fo);
94 /* prof rate */
95 tmp = 1000;
96 fwrite(&tmp, 1, sizeof(tmp), fo);
97 fwrite(GMON_SEC, 1, sizeof(GMON_SEC) - 1, fo);
98 hit = 1;
99 fwrite(&hit, 1, sizeof(hit), fo);
100
101 /* write call graph data */
102 tag = GMON_TAG_CG_ARC;
103 for (s = arc; s != NULL; s = s->hh.next) {
104 fwrite(&tag, 1, sizeof(tag), fo);
105 fwrite(&s->from, 1, sizeof(s->from), fo);
106 fwrite(&s->eip, 1, sizeof(s->eip), fo);
107 fwrite(&s->count, 1, sizeof(s->count), fo);
108 }
109
110 fclose(fo);
Daniele Forsiea70a502014-08-03 13:47:58 +0200111 fclose(f);
112
Rudolf Marek8679e522011-09-02 23:34:15 +0200113 return 0;
114}