annotate src/readpstlog.c @ 26:9eeba3f4ca4b

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