16
|
1 #include <stdio.h>
|
|
2 #include <ctype.h>
|
|
3 #include <string.h>
|
|
4
|
|
5 #ifndef _WIN32
|
|
6 # include <unistd.h>
|
|
7 #endif
|
|
8
|
|
9 #ifndef __GNUC__
|
|
10 # include "XGetopt.h"
|
|
11 #endif
|
|
12
|
|
13 #include "define.h"
|
|
14
|
|
15 #define BUF_SIZE 4096
|
|
16
|
|
17 int usage();
|
|
18 size_t get(void * buf, int size, unsigned int count, FILE *fp);
|
|
19 int split_args(char *args, int **targ);
|
|
20 int is_in(int a, int *b, int c);
|
|
21
|
|
22 int main(int argc, char** argv) {
|
36
|
23 int level = 0;
|
|
24 int *i=NULL, x, ptr, stop=0, flag;
|
|
25 char *fname, *buf, rec_type;
|
|
26 unsigned char version;
|
|
27 int *show_type=NULL, show_size=0;
|
|
28 int *ex_type=NULL, ex_size=0;
|
|
29 unsigned int funcname, filename, text, end, dtype, line, c;
|
|
30 FILE *fp;
|
|
31 struct _debug_file_rec_m mfile_rec;
|
|
32 struct _debug_file_rec_l lfile_rec;
|
|
33 char format = 'D'; // default
|
|
34 while ((c = getopt(argc, argv, "f:t:x:")) != -1) {
|
|
35 switch(c) {
|
|
36 case 'f':
|
|
37 // change the output format
|
|
38 format = toupper(optarg[0]);
|
|
39 break;
|
|
40 case 't':
|
|
41 //change the type of statements shown
|
|
42 show_size = split_args(optarg, &show_type);
|
|
43 // type = atoi(optarg);
|
|
44 break;
|
|
45 case 'x':
|
|
46 // change the type of statements excluded
|
|
47 ex_size = split_args(optarg, &ex_type);
|
|
48 break;
|
|
49 }
|
|
50 }
|
|
51 if (argc > optind) {
|
|
52 fname = argv[optind++];
|
|
53 } else {
|
|
54 usage();
|
|
55 exit(2);
|
|
56 }
|
16
|
57
|
36
|
58 fp = fopen(fname, "rb");
|
|
59 if (fp == NULL) {
|
|
60 printf("Error. couldn't open debug file\n");
|
|
61 return 2;
|
|
62 }
|
|
63 if (get(&version, sizeof(char), 1, fp)==0) {
|
|
64 printf("Error. could not read version byte from front of file");
|
|
65 return 3;
|
|
66 }
|
16
|
67
|
36
|
68 if (version > DEBUG_VERSION) {
|
|
69 printf("Version number is higher than the format I know about.");
|
|
70 return 4;
|
|
71 }
|
16
|
72
|
36
|
73 buf = (char*) xmalloc(BUF_SIZE);
|
16
|
74
|
36
|
75 while (!stop) {
|
|
76 if (fread(&x, sizeof(int), 1, fp)<=0) break;
|
|
77 ptr = 0;
|
|
78 if (x > 0) {
|
|
79 if (i) free(i);
|
|
80 i = (int*)xmalloc(sizeof(int)*(x+1));
|
|
81 // plus 1 cause we want to read the offset of the next index
|
|
82 if (get(i, sizeof(int), x+1, fp)==0) {
|
|
83 // we have reached the end of the debug file
|
|
84 printf("oh dear. we must now end\n");
|
|
85 break;
|
|
86 }
|
|
87 while (ptr < x) {
|
|
88 fseek(fp,i[ptr++], SEEK_SET);
|
|
89 get(&rec_type, 1, sizeof(char), fp);
|
|
90 if (rec_type == 'L') {
|
|
91 get(&lfile_rec, sizeof(lfile_rec), 1, fp);
|
|
92 funcname=lfile_rec.funcname;
|
|
93 filename=lfile_rec.filename;
|
|
94 text = lfile_rec.text;
|
|
95 end = lfile_rec.end;
|
|
96 dtype = lfile_rec.type;
|
|
97 line = lfile_rec.line;
|
|
98 } else if (rec_type == 'M') {
|
|
99 get(&mfile_rec, sizeof(mfile_rec), 1, fp);
|
|
100 funcname = mfile_rec.funcname;
|
|
101 filename = mfile_rec.filename;
|
|
102 text = mfile_rec.text;
|
|
103 end = mfile_rec.end;
|
|
104 dtype = mfile_rec.type;
|
|
105 line = mfile_rec.line;
|
|
106 }
|
|
107 if (dtype == DEBUG_FUNCENT_NO) level++;
|
|
108 if ((show_type == NULL || is_in(dtype, show_type, show_size)) &&
|
|
109 (ex_type == NULL || !is_in(dtype, ex_type, ex_size))) {
|
|
110 c = 0; flag = 0;
|
|
111 while (c < end) {
|
|
112 int ii = (level-1) * 4;
|
|
113 if (ii < 0) ii = 0;
|
|
114 if (ii > 64) ii = 64;
|
|
115 char indent[ii+1];
|
|
116 memset(indent, ' ', ii);
|
|
117 indent[ii] = '\0';
|
|
118 if (c + (BUF_SIZE-1) < end) {
|
|
119 get(buf, 1, BUF_SIZE-1, fp);
|
|
120 buf[BUF_SIZE-1] = '\0';
|
|
121 c += BUF_SIZE-1;
|
|
122 } else {
|
|
123 get(buf, 1, end-c, fp);
|
|
124 buf[end-c] = '\0';
|
|
125 c = end;
|
|
126 }
|
|
127 if (flag == 0) {
|
|
128 if (format == 'I') { // indented text format
|
|
129 char *b = buf+text;
|
|
130 printf("%s %s/%s[%d]: ", indent, &buf[filename], &buf[funcname], line);
|
|
131 while (b) {
|
|
132 char *p = strchr(b, '\n');
|
|
133 if (p) {
|
|
134 *p = '\0';
|
|
135 printf("%s\n%s ", b, indent);
|
|
136 b = p + 1;
|
|
137 }
|
|
138 else {
|
|
139 printf("%s", b);
|
|
140 b = NULL;
|
|
141 }
|
|
142 }
|
|
143 }
|
|
144 else if (format == 'T') { // text format
|
|
145 printf("%s/%s[%d]: %s", &buf[filename], &buf[funcname], line, &buf[text]);
|
|
146 } else {
|
|
147 printf("Type: %d\nFile[line]: %s[%d]\nFunction:%s\nText:%s", dtype,
|
|
148 &buf[filename], line, &buf[funcname], &buf[text]);
|
|
149 }
|
|
150 flag = 1;
|
|
151 } else {
|
|
152 if (format == 'I') {
|
|
153 char *b = buf;
|
|
154 while (b) {
|
|
155 char *p = strchr(b, '\n');
|
|
156 if (p) {
|
|
157 *p = '\0';
|
|
158 printf("%s\n%s ", b, indent);
|
|
159 b = p + 1;
|
|
160 }
|
|
161 else {
|
|
162 printf("%s", b);
|
|
163 b = NULL;
|
|
164 }
|
|
165 }
|
|
166 }
|
|
167 else printf("%s", buf);
|
|
168 }
|
|
169 }
|
|
170 printf("\n");
|
|
171 }
|
|
172 if (dtype == DEBUG_FUNCRET_NO) level--;
|
|
173 }
|
|
174 if (fseek(fp, i[ptr], SEEK_SET)==-1) {
|
|
175 printf("finished\n");
|
|
176 break;
|
|
177 }
|
|
178 } else {
|
|
179 printf("...no more items\n");
|
|
180 break;
|
|
181 }
|
16
|
182 }
|
36
|
183 free(buf);
|
|
184 fclose(fp);
|
|
185 return 0;
|
16
|
186 }
|
36
|
187
|
16
|
188 size_t get(void * buf, int size, unsigned int count, FILE *fp) {
|
36
|
189 size_t z;
|
|
190 if ((z=fread(buf,size, count, fp)) < count) {
|
|
191 printf("Read Failed! (size=%d, count=%d,z=%ld)\n", size, count, (long)z);
|
|
192 exit(1);
|
|
193 }
|
|
194 return z;
|
16
|
195 }
|
|
196
|
|
197 int usage() {
|
36
|
198 printf("readlog -t[show_type] -x[exclude_type] -f[format] filename\n");
|
|
199 printf("\tformat:\n\t\tt: text log format\n");
|
|
200 printf("\t\ti: indented text log format\n");
|
|
201 printf("\tshow_type:\n\t\tcomma separated list of types to show "
|
|
202 "[ie, 2,4,1,6]\n");
|
|
203 printf("\texclude_type:\n\t\tcomma separated list of types to exclude "
|
|
204 "[ie, 1,5,3,7]\n");
|
|
205 return 0;
|
16
|
206 }
|
|
207
|
36
|
208
|
16
|
209 int split_args(char *args, int **targ) {
|
36
|
210 int count = 1, *i, x, z;
|
|
211 char *tmp = args, *y;
|
|
212 if (*targ != NULL) {
|
|
213 free(*targ);
|
|
214 }
|
|
215 // find the number of tokens in the string. Starting
|
|
216 // from 1 cause there will always be one
|
|
217 while ((tmp = strchr(tmp, ',')) != NULL) {
|
|
218 tmp++; count++;
|
|
219 }
|
|
220 *targ = (int*)xmalloc(count * sizeof(int));
|
|
221 i = *targ; // for convienience
|
|
222 tmp = args;
|
|
223 z = 0;
|
|
224 for (x = 0; x < count; x++) {
|
|
225 y = strtok(tmp, ",");
|
|
226 tmp = NULL; // must be done after first call
|
|
227 if (y != NULL) {
|
|
228 i[x] = atoi(y);
|
|
229 z++;
|
|
230 }
|
|
231 }
|
|
232 return z;
|
16
|
233 }
|
|
234
|
36
|
235
|
16
|
236 // checks to see if the first arg is in the array of the second arg,
|
|
237 // the size of which is specified with the third arg. If the second
|
36
|
238 // arg is NULL, then it is obvious that it is not there.
|
16
|
239 int is_in(int a, int *b, int c){
|
36
|
240 int d = 0;
|
|
241 if (b == NULL || c == 0) { // no array or no items in array
|
|
242 return 0;
|
|
243 }
|
|
244 while (d < c) {
|
|
245 if (a == b[d]) return 1;
|
|
246 d++;
|
|
247 }
|
|
248 return 0;
|
16
|
249 }
|