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