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