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