-
Notifications
You must be signed in to change notification settings - Fork 2
/
akbox.c
1253 lines (1157 loc) · 45.4 KB
/
akbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* akbox.c - Application Black (K) Box Decoder
* Copyright (c) 2006,2012 Sampo Kellomaki ([email protected]), All Rights Reserved.
* This is confidential unpublished proprietary source code of the author.
* NO WARRANTY, not even implied warranties. Contains trade secrets.
* Distribution prohibited unless authorized in writing. See file COPYING.
* Special grant: akbox.h may be used with zxid open source project under
* same licensing terms as zxid itself.
* $Id$
*
* Application Black Box provides a lock-less logging facility
* using a circular memory buffer per thread. These buffers will
* hold recent history of application and will be part of any core
* dump that may happen, facilitating interpretation of such core.
* This is a simple log analysis program for figuring out such cores.
* There may exist other more full-fledged tools for this task.
*
* Bad compile:
* export PATH=/apps/gcc/3.3.3/bin:/apps/binutils/2.14.90.0.4.1/bin:$PATH
* gcc -g -O -Wall -fmessage-length=0 -Wno-unused-label -Wno-unknown-pragmas -fno-strict-aliasing -D_REENTRANT -o akbox akbox.c -lpthread -lcrypto -lbfd -liberty
*
* Good compile:
* export PATH=/apps/gcc/3.3.3/bin:/usr/bin:$PATH
* gcc -g -O -Wall -fmessage-length=0 -Wno-unused-label -Wno-unknown-pragmas -fno-strict-aliasing -D_REENTRANT -o akbox akbox.c akboxlog.c -lpthread -lcrypto -lbfd
*
* Unfortunately there are several different versions of bfd.h and presumably
* the library. They are not compatible. One tell tale sign in _raw_size vs. rawsize.
*
* TO DO
* - use bfd to implement live snooping
* - use symbolic thread indications in brief mode
* - compute how much of each buffer was consumed (if not wrapped around)
*
* See also: objdump -afph core2
* See also: ak-lock.pl -w 1000 <ak.out
*/
#include "platform.h"
#include "errmac.h"
#include "akbox.h"
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
//#include <sys/ptrace.h> /* see also /proc/pid , Not needed*/
#include <bfd.h> /* -lbfd */
#define AKBOX_RELEASE REL " - akbox -"
/* Called by: add_password, main x18 */
void usage(char* err) {
fputs(err, stderr);
fputs(
"Application Blak Box Decoder, Rel " AKBOX_RELEASE "\n"
"Copyright (c) 2006,2012 Sampo Kellomaki ([email protected]), All Rights Reserved.\n"
"NO WARRANTY. Not even implied warranty of any kind.\n"
"Send well researched bug reports to [email protected]\n"
"$Id$\n\n"
"Usage: akbox [options] bin.exe core # analyze a core wrt binary\n"
" akbox [options] -p pid # attach to live process and analyze it\n"
" akbox [options] -f pid # attach to process, follow continuously\n"
" akbox [options] -r funcno [n] # resolve function number to name and file\n"
" akbox [options] -e errno # Show errno as string\n"
" akbox [options] -x bin.exe # extract function names from binary\n"
" akbox [options] -y core >exe # extract an executable from core\n"
" akbox [options] -z p sigma >v.gz # Extract checksum and version info\n"
" akbox [options] -t # Show table of known function numbers.\n"
" akbox [options] -T # Show table of known targets.\n"
" -s nn Use swimlane view to visualize activity in each thread where\n"
" each thread column is nn characters wide.\n"
" -2 Use 2 swimlanes: 1st is main thread and 2nd is all worker threads.\n"
" -c nn Use swimlane view to visualize activity of each\n"
" connection. Each lane is nn characters wide and two sublanes are\n"
" used for main thread and all other threads. Lane width is nn.\n"
" -b Brief. Omit time stamps and other info.\n"
" -m msec Fuzz factor in millisec for simultaneous events in lane views.\n"
" -n nn Only mmap nn first bytes of core (used for analyzing huge cores).\n"
" -g file Produce GraphViz output (see \n"
" http://www.research.att.com/sw/tools/graphviz/download.html)\n"
"\n"
"The default view is a simple time ordered listing of all activity with\n"
"one event per line, irrespective of line width.\n"
"You can further analyze the log with ak-lock.pl -w 1000 <ak.out\n"
, stderr);
exit(1);
}
/* Called by: ak_gviz, anal_live x3, extract_a_sym x2, extract_exe, extract_syms, open_obj x6 */
void die(char* why) {
perror(why); exit(2);
}
/* Called by: extract_a_sym x2, locate_buffers x2, resolve */
void die2(char* why) {
fprintf(stderr, "%s\n", why); exit(3);
}
/* Called by: locate_buffers, main x2 */
void warning(char* why) {
fprintf(stderr, "%s\n", why);
}
void akbox_gviz(char* file);
pthread_mutexattr_t MUTEXATTR_DECL;
unsigned long max_size = ULONG_MAX; /* max size of mmap, e.g. core file */
int adhoc = 1;
int leak_free = 0;
int assert_nonfatal = 0;
int lane_width = 80;
int fuzz_usec = 0;
int brief = 0;
char* format = "full";
char* gviz = 0; /* File where graphviz output should be dumped */
int swimlane = 0;
int siz, n_thr; /* size of mmap'd core image, number of buffers */
unsigned char* base; /* base of mmap'd core image */
unsigned char* ebase = 0; /* base of mmap'd executable binary image */
int esiz = 0;
struct ak_master_rec* mr;
struct ak_buf* b[AK_MAX_BUFS];
struct ak_ts* pp[AK_MAX_BUFS]; /* original "point" */
struct ak_ts* p[AK_MAX_BUFS]; /* working point */
struct ak_ts* lim[AK_MAX_BUFS]; /* end of buffer */
struct ak_ts* start[AK_MAX_BUFS]; /* start of buffer */
int seen[AK_MAX_BUFS]; /* Used to determine when HOSTART has happened. */
char* file[65536]; /* Function number to file mapping */
char* func[65536]; /* Function number to function name mapping */
extern const_str sev[];
bfd* ebfd = 0; /* Binary File Descriptor for the executable binary, if any. */
bfd* cbfd = 0; /* Binary File Descriptor for the core, if any. */
/* Called by: anal_live, extract_a_sym x3, extract_syms x3, open_obj x2 */
void die_bfd(char* why) {
bfd_perror(why);
exit(4);
}
#define BFD_DEBUG(x)
#define BFD_DEBUG_ON(x) x
/* Called by: */
CU8* zx_memmem(CU8* haystack, int haystacklen, CU8* needle, int needlelen)
{
CU8* p = haystack;
CU8* lim = haystack + haystacklen - needlelen + 1;
while ((p < lim) && (p = memchr(p, needle[0], lim-p))) {
if (!memcmp(p, needle, needlelen)) return p;
++p;
}
return 0;
}
/* Look up an address in BFD aware way from core or executable sections. */
/* Called by: amap x2 */
static char* map_in_bfd(bfd* abfd, char* x)
{
bfd_size_type siz;
char* cont;
char* vma;
char* vma_lim;
asection* s;
for (s = abfd->sections; s; s = s->next) {
vma = (char*)bfd_get_section_vma(abfd, s);
siz = bfd_section_size(abfd, s);
vma_lim = (char*)( bfd_get_section_vma(abfd, s) + siz );
if (x >= vma && x < vma_lim) {
BFD_DEBUG(fprintf(stderr, "addr %p [%p..%p] in %s section %s\n", x, (char*)s->vma, (char*)s->vma + s->rawsize, what, bfd_get_section_name(abfd, s)));
if (!(s->flags & SEC_HAS_CONTENTS))
continue;
cont = x - (int)s->vma + (int)s->contents;
BFD_DEBUG(fprintf(stderr, "map_in_bfd(%s, %p) returns %p (s->contents=%p, s->vma=%p s->filepos=%d)\n", what, x, cont, s->contents, (char*)s->vma, (int)s->filepos));
return cont;
}
}
return 0;
}
/* Called by: extract_exe, locate_buffers x7, print_trace */
static char* amap(char* x)
{
char* y;
if (!x) return "(null)";
/* *** do endianness check and adjust as needed */
y = map_in_bfd(cbfd, x);
if (y) {
if (y >= (char*)base + siz)
return "(core-truncated)";
return y;
}
if (ebase) {
y = map_in_bfd(ebfd, x);
if (y) {
if (y >= (char*)ebase + esiz)
return "(exec-truncated)";
return y;
}
}
fprintf(stderr, "addr %p not found in any section\n", x);
return "(not-found)";
}
#define LK_FMT "%s"
#define lkmap(lk) amap(lk)
#if 0
/* Called by: print_io x4 */
static char* proto_map(int proto, char* proto_buf)
{
switch (proto) {
case SG_PROTO_LDAP: return "LDAP";
case SG_PROTO_LDAPS: return "LDAPS";
case SG_PROTO_HTTP: return "HTTP";
case SG_PROTO_HTTP11: return "HTTP11";
case SG_PROTO_HTTPS: return "HTTPS";
case SG_PROTO_HTTP11S: return "HTTP11S";
case SG_PROTO_MM1: return "MM1";
case SG_PROTO_SNMP: return "SNMP";
case SG_PROTO_RADIUS: return "RADIUS";
case SG_PROTO_SIP_TCP: return "SIP_TCP";
case SG_PROTO_SIPS: return "SIPS";
case SG_PROTO_SIP_UDP: return "SIP_UDP";
case SG_PROTO_UHAP_UDP: return "UHAP_UDP";
case SG_PROTO_UHAP: return "UHAP";
case SG_PROTO_UHAPS: return "UHAPS";
case SG_PROTO_RAWTCP_STREAM: return "RAWTCPSTREAM";
case SG_PROTO_RAWTCP_STREAMS: return "RAWTCPSTREAMS";
case SG_PROTO_RAWTCP: return "RAWTCP";
case SG_PROTO_RAWSSL: return "RAWSSL";
case SG_PROTO_LINETCP: return "LINETCP";
case SG_PROTO_LINESSL: return "LINESSL";
case SG_PROTO_RAWUDP: return "RAWUDP";
default:
sprintf(proto_buf, "proto%d", proto);
return proto_buf;
}
}
/* Called by: print_io x6 */
static char* flags_map(unsigned char flags, char* flags_buf){
if (flags & IO_CLOSED) flags_buf[0] = 'C'; else flags_buf[0] = '-';
if (flags & IO_HTTP_EOF) flags_buf[1] = 'H'; else flags_buf[1] = '-';
if (flags & IO_ENQUEUED) flags_buf[2] = 'Q'; else flags_buf[2] = '-';
if (flags & IO_INUSE) flags_buf[3] = 'U'; else flags_buf[3] = '-';
if (flags & IO_CLOSING) flags_buf[4] = 'I'; else flags_buf[4] = '-';
if (flags & IO_MISSING) flags_buf[5] = 'M'; else flags_buf[5] = '-';
if (flags & IO_MISSPOLL) flags_buf[6] = 'S'; else flags_buf[6] = '-';
flags_buf[7] = '\0';
return flags_buf;
}
/* Called by: print_line */
static void print_io(int i, char* raz)
{
char proto_buf[16];
char flags_buf[8];
struct ak_io* io = ((struct ak_io*)(p[i]));
switch (io->role) {
case IO_ROLE_FE: /* 0 */
printf("%s(%x.%p)\t%s_FE (%s) req_head(%p) req_tail(%p) (%.28s) [" LK_FMT "]\n", raz,
io->fd, io->io, proto_map(io->proto, proto_buf), flags_map(io->flags, flags_buf), io->req_head, io->req_tail,
io->msg, lkmap(p[i]->h.logkey));
break;
case IO_ROLE_BE: /* 1 */
printf("%s(%x.%p)\t%s_BE (%s) (%.28s) [" LK_FMT "]\n", raz,
io->fd, io->io, proto_map(io->proto, proto_buf), flags_map(io->flags, flags_buf), io->msg, lkmap(p[i]->h.logkey));
break;
case IO_ROLE_LISTENER: /* 2 */
printf("%s(%x.%p)\t%s_LISTENER (%s) (%.28s) [" LK_FMT "]\n", raz,
io->fd, io->io, proto_map(io->proto, proto_buf), flags_map(io->flags, flags_buf), io->msg, lkmap(p[i]->h.logkey));
break;
case IO_ROLE_UDP_LISTENER: /* 3 */
printf("%s(%x.%p)\t%s_UDP_LISTENER (%s) (%.28s) [" LK_FMT "]\n", raz,
io->fd, io->io, proto_map(io->proto, proto_buf), flags_map(io->flags, flags_buf), io->msg, lkmap(p[i]->h.logkey));
break;
case IO_ROLE_HOPELESS: /* 5 */
printf("%s(%x.%p)\tHOPELESS_BE (%s) (%.28s) [" LK_FMT "]\n", raz,
io->fd, io->io, flags_map(io->flags, flags_buf), io->msg, lkmap(p[i]->h.logkey));
break;
case IO_ROLE_SUPERVISOR: /* 4 */
default:
printf("%s(%x.%p)\trole=%d proto=%d (%s) req_head(%p) req_tail(%p) (%.28s) [" LK_FMT "]\n", raz,
io->fd, io->io, io->role, io->proto, flags_map(io->flags, flags_buf), io->req_head, io->req_tail,
io->msg, lkmap(p[i]->h.logkey));
}
}
/* Called by: print_line */
static void print_mem(int i, char* raz)
{
struct ak_mem* r = ((struct ak_mem*)(p[i]));
printf("%s(%p)\tlen=%d pool=%p p_b=%d (%.32s) [" LK_FMT "]\n", raz,
r->mem, r->len, r->pool, r->pool_blocks, r->msg, lkmap(p[i]->h.logkey));
}
/* Called by: decode_pdu_flags x4 */
static char* map_inthr(int x) {
switch (x & 0x7) {
case 0: return "0"; /* not in thread */
case 1: return "T"; /* inthread any prio */
case 2: return "I"; /* inthread */
case 3: return "A"; /* aborted */
case 4: return "W"; /* in write */
case 5: return "R"; /* run (actively running) */
case 6: return "S"; /* suspended */
case 7: return "E"; /* enqueued */
default: return "?";
}
}
/* Called by: print_pdu x4, print_pdu2 x4, print_pdu_arg x4, print_pdu_lite, print_run */
static char* decode_pdu_flags(char* buf, int flags)
{
#define FL(bit,name) (flags & bit ? name : "-")
switch (flags & 0x30) {
case 0x00: sprintf(buf, "REQ(%s,%s,%s,%s)", FL(0x08,"all_seen"), FL(0x80,"D"),
FL(0x40,"synth_done"), map_inthr(flags)); break;
case 0x10: sprintf(buf, "RESP(%s,%s,%s,%s)", FL(0x08,"all_seen"), FL(0x80,"D"),
FL(0x40,"synth_done"), map_inthr(flags)); break;
case 0x20: sprintf(buf, "SUBREQ(%s,%s,%s,%s)", FL(0x08,"all_seen"), FL(0x80,"D"),
FL(0x40,"synth_done"), map_inthr(flags)); break;
case 0x30: sprintf(buf, "SUBRESP(%s,%s,%s,%s)", FL(0x08,"all_seen"), FL(0x80,"D"),
FL(0x40,"synth_done"), map_inthr(flags)); break;
}
return buf;
}
/* Called by: print_line */
static void print_run(int i, char* raz)
{
char buf[128];
struct ak_run* r = ((struct ak_run*)(p[i]));
if (r->run) {
if (r->ph.pdu)
printf("%s(%x:%p)\t%s\tmid=%d run=%p (%.32s) [" LK_FMT "]\n", raz,
r->ph.pdu_op, r->ph.pdu, decode_pdu_flags(buf, r->ph.pdu_flags),
r->ph.pdu_mid, r->run, r->msg, lkmap(p[i]->h.logkey));
else
printf("%s nopdu run=%p (%.32s) [" LK_FMT "]\n", raz,
r->run, r->msg, lkmap(p[i]->h.logkey));
}
else
printf("%s norun (%.32s) [" LK_FMT "]\n", raz,
r->msg, lkmap(p[i]->h.logkey));
}
/* Called by: print_line */
static void print_pdu_lite(int i, char* raz)
{
char buf[128];
struct ak_pdu_lite* pdu = ((struct ak_pdu_lite*)(p[i]));
printf("%s(%x:%p)\t%s\tmid=%d (%.36s) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->msg, lkmap(p[i]->h.logkey));
}
/* Called by: print_line */
static void print_pdu(int i, char* raz)
{
char buf[128];
struct ak_pdu* pdu = ((struct ak_pdu*)(p[i]));
if (pdu->pduparent) {
if (pdu->pdureq) {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) req(%p) parent(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdureq, pdu->pduparent, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, lkmap(p[i]->h.logkey));
} else {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) parent(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pduparent, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, lkmap(p[i]->h.logkey));
}
} else {
if (pdu->pdureq) {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) req(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdureq, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, lkmap(p[i]->h.logkey));
} else {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, lkmap(p[i]->h.logkey));
}
}
}
/* Called by: print_line */
static void print_pdu_arg(int i, char* raz)
{
char buf[128];
struct ak_pdu_arg* pdu = ((struct ak_pdu_arg*)(p[i]));
if (pdu->pduparent) {
if (pdu->pdureq) {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) req(%p) parent(%p) nx(%p) pr(%p) wnx(%p) deps=%d (0x%x) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdureq, pdu->pduparent, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->arg, lkmap(p[i]->h.logkey));
} else {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) parent(%p) nx(%p) pr(%p) wnx(%p) deps=%d (0x%x) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pduparent, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->arg, lkmap(p[i]->h.logkey));
}
} else {
if (pdu->pdureq) {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) req(%p) nx(%p) pr(%p) wnx(%p) deps=%d (0x%x) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdureq, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->arg, lkmap(p[i]->h.logkey));
} else {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) nx(%p) pr(%p) wnx(%p) deps=%d (0x%x) [" LK_FMT "]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->arg, lkmap(p[i]->h.logkey));
}
}
}
/* Called by: print_line */
static void print_pdu2(int i, char* raz)
{
char buf[128];
struct ak_pdu* pdu = ((struct ak_pdu*)(p[i]));
if (pdu->pduparent) {
if (pdu->pdureq) {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) req(%p) parent(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [%p]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdureq, pdu->pduparent, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, p[i]->h.logkey); /* msg is func called, logkey is really nargs */
} else {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) parent(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [%p]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pduparent, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, p[i]->h.logkey); /* msg is func called, logkey is really nargs */
}
} else {
if (pdu->pdureq) {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) req(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [%p]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdureq, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, p[i]->h.logkey); /* msg is func called, logkey is really nargs */
} else {
printf("%s(%x:%p)\t%s\tmid=%d fe(%p) nx(%p) pr(%p) wnx(%p) deps=%d (%.4s) [%p]\n", raz,
pdu->ph.pdu_op, pdu->ph.pdu, decode_pdu_flags(buf, pdu->ph.pdu_flags),
pdu->ph.pdu_mid, pdu->pdufe, pdu->pdunext,
pdu->pduprev, pdu->writenext, pdu->pdu_deps,
pdu->msg, p[i]->h.logkey); /* msg is func called, logkey is really nargs */
}
}
}
#endif
/* Called by: print_line */
static void print_report(int i, char* raz)
{
struct ak_report* r = ((struct ak_report*)(p[i]));
printf("%s " LK_FMT "\tblock_size=%d, blks from pool: %d/%d; from malloc: %d/%d %d bytes (%.16s)\n", raz, lkmap(p[i]->h.logkey), r->block_size, r->blocks_out, r->n_blocks, r->malloc_cnt_bal, r->malloc_cnt, r->malloc_vol_bal, r->msg);
}
/* Called by: print_line */
static void print_ini(int i, char* raz)
{
struct ak_ini* inc = ((struct ak_ini*)(p[i]));
printf("%s F:%s S:%d Bytes M:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x [%s]\n", raz, inc->msg, inc->size, inc->md5val[0], inc->md5val[1], inc->md5val[2], inc->md5val[3], inc->md5val[4], inc->md5val[5], inc->md5val[6], inc->md5val[7], inc->md5val[8], inc->md5val[9], inc->md5val[10], inc->md5val[11], inc->md5val[12], inc->md5val[13], inc->md5val[14], inc->md5val[15], lkmap(p[i]->h.logkey));
}
/* Called by: print_line x2 */
static void print_ts(int i, char* raz)
{
printf(raz, p[i]->msg, lkmap(p[i]->h.logkey));
}
/* Called by: print_line */
static void print_tsa(int i, char* raz)
{
struct ak_tsa* r = ((struct ak_tsa*)(p[i]));
printf(raz, r->msg, r->arg, lkmap(p[i]->h.logkey));
}
/* Called by: print_line x6 */
static void print_trace(int i, char* raz)
{
struct _binid* cur_func;
struct ak_disasm* di = ((struct ak_disasm*)(p[i]));
cur_func = (struct _binid*)amap((char*)di->cur_func);
printf("%s(%s)\n", raz, lkmap((char *)cur_func->key));
}
#ifdef SUNOS
struct split_ll{
union{
struct{
unsigned long high;
unsigned long low;
};
unsigned long long full;
};
};
/* Called by: get_low */
int am_big_endian()
{
long one= 1;
return !(*((char *)(&one)));
}
/* Called by: get_nano, get_secs */
unsigned long get_low (unsigned long long num){
struct split_ll ll;
ll.full = num;
if (am_big_endian())
return ll.low;
else
return ll.high;
}
/* Called by: print_line x2 */
int get_secs (ak_time act_tick){
unsigned long a;
a = get_low ((act_tick - mr->first_tick)/1000000000.0);
return (int)(a);
}
/* Called by: print_line x2 */
unsigned long get_nano (ak_time act_tick){
ak_time rest;
rest = ((act_tick - mr->first_tick)%1000000000);
return get_low(rest);
}
#endif
#ifdef MINGW
/* Called by: print_line x2 */
int get_secs (ak_time act_tick){
return (int)((act_tick.QuadPart - mr->first_tick.QuadPart)/mr->ticksPerSecond.QuadPart);
}
/* Called by: print_line x2 */
unsigned long get_nano (ak_time act_tick){
ak_time rest;
double ticksPerNano;
ticksPerNano = (mr->ticksPerSecond.QuadPart/1000000000.0);
rest.QuadPart = ((act_tick.QuadPart - mr->first_tick.QuadPart)%mr->ticksPerSecond.QuadPart);
rest.QuadPart = (rest.QuadPart/ticksPerNano);
return (unsigned long)rest.LowPart;
}
#endif
/* You need to teach this function about every possible message type. When (if) swimlanes
* are implemented, this function probably needs to be aware of them. */
/* Called by: print_buffers x3 */
static void print_line(int i)
{
int seve;
struct tm t;
#ifdef MINGW
unsigned int secs = mr->first_sec + get_secs(p[i]->h.tv);
GMTIME(secs, t);
printf("%c%-2d %04d%02d%02d %02d%02d%02d.%09lu %15s:%-4d %20s():\t",
b[i]->comment[0], i-1, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, get_nano (p[i]->h.tv),
file[p[i]->h.func], p[i]->h.line, func[p[i]->h.func]);
#endif
#ifdef SUNOS
unsigned int secs = mr->first_sec + get_secs(p[i]->h.tv);
GMTIME(secs, t);
printf("%c%-2d %04d%02d%02d %02d%02d%02d.%09lu %15s:%-4d %20s():\t",
b[i]->comment[0], i-1, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, get_nano (p[i]->h.tv),
file[p[i]->h.func], p[i]->h.line, func[p[i]->h.func]);
#endif
#if (!defined(SUNOS)&&!defined(MINGW))
if (brief) {
printf("%c%-2d %03ld.%06ld %10.10s:%-3d ", b[i]->comment[0], i-1,
p[i]->h.tv.tv_sec % 1000, p[i]->h.tv.tv_usec,
func[p[i]->h.func], p[i]->h.line);
} else {
GMTIME(p[i]->h.tv.tv_sec, t);
printf("%c%-2d %04d%02d%02d %02d%02d%02d.%06ld %15s:%-4d %20s():\t",
b[i]->comment[0], i-1, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, p[i]->h.tv.tv_usec,
file[p[i]->h.func], p[i]->h.line, func[p[i]->h.func]);
}
#endif
switch (p[i]->raz) {
#define AK_RAZ_INI(sym,code,desc) case AK_ ## sym ## _RAZ: print_ini(i, #sym ); break;
#define AK_RAZ_TS(sym,code,desc) case AK_ ## sym ## _RAZ: print_ts(i, #sym " (%s) [" LK_FMT "]\n"); break;
#define AK_RAZ_TS2(sym,code,desc) case AK_ ## sym ## _RAZ: print_ts(i, #sym " (%s) [%p]\n"); break;
#define AK_RAZ_TSA(sym,code,desc) case AK_ ## sym ## _RAZ: print_tsa(i, #sym " (%s) arg=%p [" LK_FMT "]\n"); break;
#define AK_RAZ_MEM(sym,code,desc) case AK_ ## sym ## _RAZ: print_mem(i, #sym); break;
#define AK_RAZ_RUN(sym,code,desc) case AK_ ## sym ## _RAZ: print_run(i, #sym); break;
#define AK_RAZ_PDU(sym,code,desc) case AK_ ## sym ## _RAZ: print_pdu(i, #sym); break;
#define AK_RAZ_PDU2(sym,code,desc) case AK_ ## sym ## _RAZ: print_pdu2(i, #sym); break;
#define AK_RAZ_LITE(sym,code,desc) case AK_ ## sym ## _RAZ: print_pdu_lite(i, #sym); break;
#define AK_RAZ_ARG(sym,code,desc) case AK_ ## sym ## _RAZ: print_pdu_arg(i, #sym); break;
#define AK_RAZ_IO(sym,code,desc) case AK_ ## sym ## _RAZ: print_io(i, #sym); break;
#define AK_RAZ_REPORT(sym,code,desc) case AK_ ## sym ## _RAZ: print_report(i, #sym); break;
#define AK_RAZ_SPEC(sym,code,desc)
#include "aktab.h"
case AK_ERR_RAZ:
seve = ((struct ak_err*)p[i])->severity;
printf("ERR %s %s %d (%s) [" LK_FMT "]\n",
map_error(((struct ak_err*)p[i])->error_code),
(seve>=0) && (seve < 8) ? sev[seve] : "***UNKWN_SEV",
((struct ak_err*)p[i])->action,
((struct ak_err*)p[i])->msg, lkmap(p[i]->h.logkey)); break;
case AK_ASSERT_RAZ: /* ASSERT or CHK macro, logkey is condition */
printf("ASSERT(" LK_FMT ") [%s]\n", lkmap(p[i]->h.logkey), p[i]->msg); break;
case AK_ASSERTOP_RAZ: /* ASSERTOP macro, logkey is value a, msg is condition */
printf("ASSERTOP(" LK_FMT ") [%s]\n", lkmap(p[i]->h.logkey), p[i]->msg); break;
case AK_FAIL_RAZ: /* FAIL macro logkey: val (e.g. failed magic), msg: why */
printf("FAIL(%s) [%p]\n", p[i]->msg, p[i]->h.logkey); break;
case AK_FAILS_RAZ: /* FAIL_PDU macro logkey: str (e.g. function name), msg: why */
printf("FAIL(%s) [" LK_FMT "]\n", p[i]->msg, lkmap(p[i]->h.logkey)); break;
case AK_TRACE_VMENTRY_RAZ: print_trace(i, "VMENTRY"); break;
case AK_TRACE_CALL_RAZ: print_trace(i, "CALL"); break;
case AK_TRACE_RET_RAZ: print_trace(i, "RET"); break;
case AK_TRACE_NATCALL_RAZ: print_trace(i, "NATCALL"); break;
case AK_TRACE_NATRET_RAZ: print_trace(i, "NATRET"); break;
case AK_TRACE_RAZ: print_trace(i, "TRACE"); break;
default:
printf("unknown_reason %x [" LK_FMT "]\n", p[i]->raz, lkmap(p[i]->h.logkey));
}
++(p[i]); /* Advance to next line */
/*printf("adv. to next p=%p pp=%p lim=%p\n", p[i], pp[i], lim[i]);*/
}
#ifdef MINGW
/* Called by: anal_core, anal_live */
static void print_buffers()
{
unsigned long long old_tv;
int oldest,i,j, wrapped_seen = 0;
printf("COLDSTART\n");
while (1) {
/* Find oldest (that has not run around buffer yet) */
oldest = -1; /* none */
old_tv = ULLONG_MAX;
for (i = 0; i <= n_thr; ++i) /* Number of threads + 1 static */
if ((p[i] != pp[i]) && /* must not have wrapped around back to the point */
(p[i]->h.tv.QuadPart < old_tv)) {
oldest = i;
old_tv = p[i]->h.tv.QuadPart;
}
if (oldest == -1) break; /* nothing more to print */
i = oldest;
while ((p[i] != pp[i]) && /* must not have wrapped around back to the point */
(p[i]->h.tv.QuadPart <= old_tv)) {
if (!seen[i]) {
seen[i] = 1;
for (j = 0; j < n_thr; ++j)
if (!seen[j])
break;
if (!wrapped_seen && b[i]->wrap_around) {
printf("FIRSTWRAP\n");
wrapped_seen = 1;
}
if (j == n_thr)
printf("ALLSEEN\n");
}
print_line(i);
if (p[i] >= lim[i])
p[i] = start[i];
}
}
}
#endif
#ifdef SUNOS
/* Called by: anal_core, anal_live */
static void print_buffers()
{
unsigned long long old_tv;
int oldest,i,j, wrapped_seen = 0;
printf("COLDSTART\n");
while (1) {
/* Find oldest (that has not run around buffer yet) */
oldest = -1; /* none */
old_tv = ULLONG_MAX;
for (i = 0; i <= n_thr; ++i) /* Number of threads + 1 static */
if ((p[i] != pp[i]) && /* must not have wrapped around back to the point */
(p[i]->h.tv < old_tv)) {
oldest = i;
old_tv = p[i]->h.tv;
}
if (oldest == -1) break; /* nothing more to print */
i = oldest;
while ((p[i] != pp[i]) && /* must not have wrapped around back to the point */
(p[i]->h.tv <= old_tv)) {
if (!seen[i]) {
seen[i] = 1;
for (j = 0; j < n_thr; ++j)
if (!seen[j])
break;
if (!wrapped_seen && b[i]->wrap_around) {
printf("FIRSTWRAP\n");
wrapped_seen = 1;
}
if (j == n_thr)
printf("ALLSEEN\n");
}
print_line(i);
if (p[i] >= lim[i])
p[i] = start[i];
}
}
}
#endif
#if (!defined(SUNOS)&&!defined(MINGW))
/* Called by: anal_core, anal_live */
static void print_buffers()
{
/*unsigned long long end_slice_usec;*/
struct timeval old_tv;
int oldest,i,j, wrapped_seen = 0;
printf("COLDSTART\n");
while (1) {
/* Find oldest (that has not run around buffer yet) */
oldest = -1; /* none */
old_tv.tv_sec = LONG_MAX;
old_tv.tv_usec = LONG_MAX;
for (i = 0; i <= n_thr; ++i) /* Number of threads + 1 static */
if ((p[i] != pp[i]) && /* must not have wrapped around back to the point */
((p[i]->h.tv.tv_sec < old_tv.tv_sec)
|| ((p[i]->h.tv.tv_sec == old_tv.tv_sec)
&& (p[i]->h.tv.tv_usec < old_tv.tv_usec)))) {
oldest = i;
old_tv = p[i]->h.tv;
}
if (oldest == -1) break; /* nothing more to print */
/* Print as many lines from selected buffer as fuzz_usec permits. Note: handing 64
* bit ints on 32 bit arcitecture proves to be tricky and gdb, for example, apparently
* gets it wrong, thus we do the comparisons by parts here. */
/*end_slice_usec = old_tv.tv_sec * 1000000L + old_tv.tv_usec + fuzz_usec;*/
old_tv.tv_usec += fuzz_usec;
if (old_tv.tv_usec > 1000000) {
old_tv.tv_sec += old_tv.tv_usec / 1000000;
old_tv.tv_usec += old_tv.tv_usec % 1000000;
}
i = oldest;
while ((p[i] != pp[i])
/*&& ((p[i]->h.tv.tv_sec * 1000000L + p[i]->h.tv.tv_usec) <= end_slice_usec)*/
&& ((p[i]->h.tv.tv_sec < old_tv.tv_sec)
|| ((p[i]->h.tv.tv_sec == old_tv.tv_sec)
&& (p[i]->h.tv.tv_usec <= old_tv.tv_usec)))
)
{
if (!seen[i]) {
seen[i] = 1;
for (j = 0; j < n_thr; ++j)
if (!seen[j])
break;
if (!wrapped_seen && b[i]->wrap_around) {
printf("FIRSTWRAP\n");
wrapped_seen = 1;
}
if (j == n_thr)
printf("ALLSEEN\n");
}
print_line(i);
if (p[i] >= lim[i])
p[i] = start[i];
}
}
}
#endif
/* Called by: anal_core, anal_live, extract_exe */
static void locate_buffers(FILE* file)
{
int i;
char* lkmark;
char stamp[16];
/* Find master block */
mr = (struct ak_master_rec*)base;
while (mr) {
mr = (struct ak_master_rec*)zx_memmem((U8*)mr, siz - ((unsigned char*)mr - base),
(U8*)AK_MASTER_STAMP, sizeof(AK_MASTER_STAMP));
if (!mr) die2("AK master magic stamp (" AK_MASTER_STAMP ") not found in core.");
if (mr->endian_mark == AK_ENDIAN_MARK) break;
++mr;
}
if (ebase) {
lkmark = (char*)zx_memmem(ebase, esiz, (U8*)AK_LOGKEY_MARK, sizeof(AK_LOGKEY_MARK));
/* It seems that windows exe does not have the lkmark, but dll has, take a look at this */
#if !defined(MINGW)
if (!lkmark) die2("AK log key mark (" AK_LOGKEY_MARK ") not found in executable.");
#endif
}
if (n_thr > AK_MAX_BUFS) warning("n_threads exceeds limit");
fprintf(file, "Summary\n");
fprintf(file, " bin date: %s %s\n", mr->date, mr->time);
fprintf(file, " n_threads: %d\n", n_thr);
#if !defined(INTERIXOS)
#if !defined(MINGW)
fprintf(file, " Proc Number: %d\n", mr->ProcNum);
fprintf(file, " Host Id: %x\n", (unsigned int)mr->HostId);
fprintf(file, " Sys Name: %s\n", mr->sysname);
fprintf(file, " Node Name: %s\n", mr->nodename);
fprintf(file, " OS Release: %s\n", mr->os_release);
fprintf(file, " OS Version: %s\n", mr->os_version);
fprintf(file, " Machine: %s\n", mr->machine);
#else /** MINGW **/
fprintf(file, " Proc Number: %d\n", mr->ProcNum);
fprintf(file, " Sys Name: %s\n", mr->sysname);
fprintf(file, " OS Version: %s\n", mr->os_version);
#endif
#endif
fprintf(file, " core command line: %s\n", bfd_core_file_failing_command(cbfd));
if (ebase) {
fprintf(file, " core signal : %d\n", bfd_core_file_failing_signal(cbfd));
if (!core_file_matches_executable_p(cbfd, ebfd)) {
fprintf(stderr, "WARNING: core does not match executable. logkey information may be unreliable.\n");
fprintf(file, "WARNING: core does not match executable. logkey information may be unreliable.\n");
}
fprintf(file, " bin arch : %s\n", bfd_printable_name(ebfd));
}
fprintf(file, " core arch : %s\n", bfd_printable_name(cbfd));
fprintf(file, " exe realpath : %s\n", mr->realpath);
fprintf(file, "END_PREAMBLE\n");
/** This ASSERT causes some problems in AIX **/
#if !defined(AIXOS)
ASSERTOP(((char*)(mr)),==,amap((char*)(mr->self)),mr);
#endif
n_thr = mr->n_threads;
fprintf(file, "Master record found\n");
fprintf(file, " endian_mark: 0x%x\n", mr->endian_mark);
fprintf(file, " mr address: %p\n", mr);
#if !defined(INTERIXOS)
fprintf(file, "Machine info\n");
#if !defined(MINGW)
#ifdef AIXOS
fprintf(file, " Proc Arch: %d\n", mr->ProcArch);
fprintf(file, " Proc Imp: %d\n", mr->ProcImp);
fprintf(file, " Proc Ver: %d\n", mr->ProcVer);
fprintf(file, " Width(bits): %d\n", mr->Width);
fprintf(file, " Max Memory: %ld\n", mr->MaxMem);
#else
fprintf(file, " Model Name: %s\n", mr->ModelName); /* Not completed for AIX*/
#endif
# if defined(LINUX) /** For the moment only works for LINUX **/
fprintf(file, " Max Memory: %ld\n", mr->MaxMem);
fprintf(file, " Max Swap: %ld\n", mr->MaxSwap);
fprintf(file, " Mem Unit: %ld\n", mr->MemUnit);
# endif
#else /** MINGW **/
fprintf(file, " Proc Arch: %d\n", mr->ProcArch);
fprintf(file, " Proc Type: %d\n", mr->ProcType);
fprintf(file, " Proc Level: %d\n", mr->ProcLevel);
fprintf(file, " Proc Revision: %d\n", mr->ProcRev);
#endif
#endif
fprintf(file, "Stat info\n");
fprintf(file, " exe size : %d\n", (int)mr->binary_st.st_size);
fprintf(file, " exe mode : %o\n", (unsigned int)mr->binary_st.st_mode);
fprintf(file, " exe uid : %d\n", (int)mr->binary_st.st_uid);
fprintf(file, " exe gid : %d\n", (int)mr->binary_st.st_gid);
fprintf(file, " exe nlink : %d\n", (int)mr->binary_st.st_nlink);
fprintf(file, " exe access time : %d\n", (int)mr->binary_st.st_atime);
fprintf(file, " exe modify time : %d\n", (int)mr->binary_st.st_mtime);
fprintf(file, " exe status time : %d\n", (int)mr->binary_st.st_ctime);
fprintf(file, " exe dev : %d\n", (int)mr->binary_st.st_dev);
fprintf(file, " exe inode : %d\n", (int)mr->binary_st.st_ino);
/* Locate other blocks */
fprintf(file, "BUFSPEC\n");
/* First static buffer */
b[0] = (struct ak_buf*)amap((char*)(mr->st_buf));
sprintf(stamp, AK_ST_BUFFER_STAMP);
if (strcmp(stamp, b[0]->stamp))
fprintf(stderr, "WARNING: st_buf has bad stamp (%s) mapped=%p\n", b[0]->stamp, b[0]);
pp[0] = (struct ak_ts*)amap((char*)(b[0]->p));
lim[0] = (struct ak_ts*)amap((char*)(b[0]->lim));
start[0] = b[0]->start;
p[0] = b[0]->wrap_around ? pp[0]+1 : start[0];
if (p[0] >= lim[0])
p[0] = start[0];
fprintf(file, "Static Buffer comment(%s) tid=%d wrap_around=%d start=%p lim=%p pp=%p p=%p\n",
b[0]->comment, b[0]->tid, b[0]->wrap_around, start[0], lim[0], pp[0], p[0]);
for (i = 1; i <= n_thr; ++i) {
b[i] = (struct ak_buf*)amap((char*)(mr->bufs[i-1]));
sprintf(stamp, AK_BUFFER_STAMP, i);
if (strcmp(stamp, b[i]->stamp))
fprintf(stderr, "WARNING: buf[%d] has bad stamp (%s) mapped=%p\n", i, b[i]->stamp, b[i]);
pp[i] = (struct ak_ts*)amap((char*)(b[i]->p));
lim[i] = (struct ak_ts*)amap((char*)(b[i]->lim));
start[i] = b[i]->start;
p[i] = b[i]->wrap_around ? pp[i]+1 : start[i];
if (p[i] >= lim[i])
p[i] = start[i];
fprintf(file, "Buffer %c%-2d comment(%s) tid=%d wrap_around=%d start=%p lim=%p pp=%p p=%p mem_pool=%p\n",
b[i]->comment[0], i-1, b[i]->comment, b[i]->tid,
b[i]->wrap_around, start[i], lim[i], pp[i], p[i], (void *)b[i]->mem_pool);
}
}
/* ----------------------------------------------------------------- */
/* Called by: anal_core x2, extract_a_sym, extract_exe */
static char* open_obj(char* filename, int* size, bfd** abfd, bfd_format format) {
asection* s;
char* base_ptr;
fdtype fd;
struct stat st;
fd = openfile_ro(filename);
if (fd == BADFD) die(filename);
#ifdef MINGW
DWORD filesize;
// change this to GetFileSizeEx()
if ((filesize = GetFileSize(fd, 0)) == INVALID_FILE_SIZE) die("GetFileSize() failed....");
st.st_size = filesize;
#else
if (fstat(fd, &st) == -1) die("stat(2)");
#endif
if ((unsigned int)st.st_size > max_size) {
fprintf(stderr, "mmap of %s truncated to %lu bytes due to -n flag or max_size limit (the size would have been %lu bytes).\n", filename, max_size, (long unsigned int)st.st_size);
*size = max_size;
} else
*size = st.st_size;
#ifdef MINGW
HANDLE fM;
if ((fM = CreateFileMapping(fd, 0, PAGE_READONLY, 0, 0, 0)) == 0) {
errno = GetLastError();
die("mmap");
}
if ((base_ptr = MapViewOfFile(fM, FILE_MAP_READ, 0, 0, *size)) == 0) {
errno = GetLastError();
die("mmap");
}
#else
base_ptr = mmap(0, *size, PROT_READ, MAP_SHARED|MAP_NORESERVE, fd, 0);
if (!base_ptr || base_ptr == MAP_FAILED) die("mmap");
#endif
#ifdef MINGW
*abfd = bfd_openr(filename, "pei-i386");
#else
*abfd = bfd_openr(filename, "default");
#endif
if (!*abfd) die_bfd("bfd_fdopenr");
if (!bfd_check_format(*abfd, format))
die_bfd("Not a recognized BFD file format.");
for (s = (*abfd)->sections; s; s = s->next) {
if (!(s->flags & SEC_HAS_CONTENTS)) /* often crashes here with s == 0xfe9, fix is to compile as in comment in the beginning. This is some mixed library or library versioning issue. --Sampo */
continue;
s->contents = (U8*)(base_ptr + s->filepos);
s->flags |= SEC_IN_MEMORY;
}
return base_ptr;