comparison src/readpstlog.c @ 52:034641c26ab9

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