summaryrefslogtreecommitdiff
path: root/mtdump.c
blob: 6f18d0cc5f6036aacba3b23a80a99255f86db744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <Eina.h>

static char *
pstr(const char *cmd, int lines)
{
   FILE *f;
   char *str = NULL;
   char buf[4096];
   int str_len = 0;
   int n = 0;
   int count = 0;
   
   f = popen(cmd, "r");
   if (!f) return NULL;
   while (fgets(buf, sizeof(buf) - 1, f))
     {
        int len = strlen(buf);
        if ((len > 1) && (buf[len - 1] == '\n'))
          {
             buf[len - 1] = 0;
             len--;
          }
        str_len += len;
        str = realloc(str, str_len + 1);
        if (count == 0) str[0] = 0;
        strcat(str, buf);
        n++;
        count++;
        if (n >= lines) break;
     }
   pclose(f);
   return str;
}

static char *
addr2func(const char *file, const char *addr)
{
   char *s;
   char cmd[4096], buf[4096];
   static Eina_Hash *hash = NULL;
   
   if (!hash) hash = eina_hash_string_superfast_new(NULL);
   snprintf(buf, sizeof(buf), "%s %s", file, addr);
   s = eina_hash_find(hash, buf);
   if (s) return strdup(s);
   snprintf(cmd, sizeof(cmd), "addr2line -f -e %s", buf);
   s = pstr(cmd, 1);
   eina_hash_add(hash, buf, s);
   return strdup(s);
}

static FILE *outf = NULL;

#define OUT(fmt, args...) fprintf(outf, fmt, ##args)

int
main(int argc, char **argv)
{
   FILE *f;
   char buf[4096];
   int items = 0;
   int btnum = 0;
   int expected = 0;
   time_t t0;
   
   if (argc < 3)
     {
        printf("usage: mtdump MTLOGFILE OUTFILE\n");
        exit(1);
     }
   
   f = fopen(argv[1], "r");
   if (!f)
     {
        perror("fopen");
        exit(1);
     }

   outf = fopen(argv[2], "w");
   if (!outf)
     {
        perror("fopen");
        exit(1);
     }
   
   while (fgets(buf, sizeof(buf) - 1, f))
     {
        if (!strncmp(buf, "== ", 3)) expected++;
     }
   fseek(f, 0, SEEK_SET);
   t0 = time(NULL);
   while (fgets(buf, sizeof(buf) - 1, f))
     {
        int len;
        
        len = strlen(buf);
        if (len < 2) continue;
        if ((len > 1) && (buf[len - 1] == '\n'))
          {
             buf[len - 1] = 0;
             len--;
          }
        if (!strncmp(buf, "== ", 3))
          {
             unsigned long size;
             char type[4096] = "";
             unsigned long alloctime;
             
             if (sscanf(buf, "%*s %s %lu %*s %lu", type, &size, &alloctime)
                 == 3)
               {
                  time_t t;
                  struct tm *tmp;
                  char out[256] = "??";
                  int eta;
                  
                  if (items > 0) OUT("\n");
                  items++;
                  t = time(NULL) - t0;
                  eta = (expected * (int)t) / items;
                  printf("%i/%i -- ETA %i min %i sec                       \r",
                         items, expected, eta / 60, eta % 60);
                  fflush(stdout);
                  btnum = 0;
                  t = alloctime;
                  tmp = localtime(&t);
                  if (tmp)
                    strftime(out, sizeof(out), "%F/%T", tmp);
                  else
                    strcpy(out, "??");
                  OUT("%lu %s %s |", size, type, out);
               }
          }
        else
          {
             if (btnum > 0)
               {
                  if (strchr(buf, '['))
                    {
                       // 1 of:
                       // /usr/local/lib/libeina.so.1(+0x16ab8)[0x7fdaccc4cab8]
                       // /lib/x86_64-linux-gnu/libc.so.6(__strdup+0x22)[0x7fdace0732f2]
                       // ello[0x402d8c]
                       // /usr/local/bin/ello[0x402d8c]
                       if (strchr(buf, '('))
                         {
                            char file[4096], addr[4096];
                            // 1 of:
                            // /usr/local/lib/libeina.so.1(+0x16ab8)[0x7fdaccc4cab8]
                            // /lib/x86_64-linux-gnu/libc.so.6(__strdup+0x22)[0x7fdace0732f2]
                            if (sscanf(buf, "%[^(](%[^)])%*s", file, addr) == 2)
                              {
                                 if (addr[0] == '+')
                                   {
                                      char *s = addr2func(file, addr + 1);
                                      if (s)
                                        {
                                           OUT(" %s", s);
                                           free(s);
                                        }
                                      else
                                        OUT(" ??");
                                   }
                                 else
                                   {
                                      char *s;
                                      
                                      s = strchr(addr, '+');
                                      if (s) *s = 0;
                                      OUT(" %s", addr);
                                   }
                              }
                            else
                              OUT(" ??");
                         }
                       else
                         {
                            // 1 of:
                            // ello[0x402d8c]
                            // /usr/local/bin/ello[0x402d8c]
                            if (buf[0] == '/')
                              {
                                 char file[4096], addr[4096];
                                 
                                 if (sscanf(buf, "%[^\\[][%[^]]]", file, addr) == 2)
                                   {
                                      char *s = addr2func(file, addr);
                                      if (s)
                                        {
                                           OUT(" %s", s);
                                           free(s);
                                        }
                                      else
                                        OUT(" ??");
                                   }
                                 else
                                   OUT(" ??");
                              }
                            else
                              OUT(" ?main?");
                         }
                    }
                  else
                    OUT(" ??");
               }
             btnum++;
          }
     }
   fclose(f);
   fclose(outf);
   
   exit(0);
   return 0;
}