/
CoreUtils.cc
1386 lines (1195 loc) · 40.9 KB
/
CoreUtils.cc
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
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/****************************************************************************
CoreUtils.cc
Description: Automated processing of core files on Sparc & Linux
****************************************************************************/
/*
Stack Unwinding procedure on ix86 architecture on Linux :
Get the first frame pointer in $ebp.
The value stored in $ebp is the address of prev frame pointer.
Keep on unwinding till it is Ox0.
$ebp+4 in each frame represents $eip.(PC)
*/
/*
* Accessing arguments on 386 :
* ----------------------------
* We need to start from $ebp+4 and then keep on reading args
* till we reach the base pointer for prev. frame
*
*
* (high memory)
* + +
* | Callers Stack Frame |
* +---------------------+
* | function call |
* | arguments |
* +---------------------+
* | Return Address +
* +-------------------- +
* | Old base pointer + Base pointer BP
* +-------------------- +
* | |
* | |
* | | Local (automatic) variables
* | |
* | |
* | |
* | |
* | |
* | |
* +---------------------+ Stack pointer SP
* | free stack | (low memory, top of the stack)
* | begins here |
* + +
*
*
* +-----------------+ +-----------------+
* FP -> | previous FP --------> | previous FP ------>...
* | | | |
* | return address | | return address |
* +-----------------+ +-----------------+
*/
/*
PORTING ISSUES:
read_core_memory read_core_memory II read_heap_header not used
-> figure out some way to make print_netstate useful!
*/
/* 32-bit arguments are pushed down stack in reverse syntactic order (hence accessed/popped in the right order), above the 32-bit near return address. %ebp, %esi, %edi, %ebx are callee-saved, other registers are caller-saved; %eax is to hold the result, or %edx:%eax for 64-bit results */
/* has -fomit-frame-pointer has any repercussions??
We assume that all the code is generated with frame pointers set. */
/* modify the "note" in process_core */
/* Document properly */
#include "ink_config.h"
#if defined(sparc)
// We need procfs data strucutures and they
// don't support large files
#undef _LARGEFILE_SOURCE
#undef _FILE_OFFSET_BITS
#define is_debug_tag_set(_t) 1
#include <sys/types.h>
#include <sys/core.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "libts.h"
#include "DynArray.h"
#include "CoreUtils.h"
#include "Diags.h"
#include "http/HttpSM.h"
#include "P_EventSystem.h"
#include "I_Version.h"
#include <string.h>
#include <sys/elf.h>
#include <procfs.h>
int lwpid;
int moffset = (int) &(((HttpSM *) NULL)->magic);
lwpTable default_lwpTable = { 0, 0 };
DynArray<struct lwpTable>arrayLwp(&default_lwpTable, 0);
#endif /* sparc */
#if defined(linux)
#include "CoreUtils.h"
#define __p_type p_type //ugly hack? - see resolv.h
#define D(x) x /* for debugging */
intptr_t f1, f2;
int framepointer = 0;
int program_counter = 0;
#endif // linux check
#if defined(darwin) || defined(freebsd) || defined(solaris) || defined(openbsd) // FIXME: solaris x86
// TODO: Cleanup multiple includes
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include "libts.h"
#include "CoreUtils.h"
#endif /* darwin || freebsd || solaris */
#include "EventName.h"
#include "http/HttpSM.h"
bool inTable;
FILE *fp;
memTable default_memTable = { 0, 0, 0 };
DynArray<struct memTable>arrayMem(&default_memTable, 0);
HTTPHdrImpl *global_http;
HttpSM *last_seen_http_sm = NULL;
char ethread_ptr_str[256] = "";
char netvc_ptr_str[256] = "";
HdrHeap *swizzle_heap;
char *ptr_data;
// returns the index of the vaddr or the index after where it should be
intptr_t
CoreUtils::find_vaddr(intptr_t vaddr, intptr_t upper, intptr_t lower)
{
intptr_t index = (intptr_t) floor((double)((upper + lower) / 2));
// match in table, returns index to be inserted into
if (arrayMem[index].vaddr == vaddr) {
inTable = true;
return index + 1;
// no match
} else if (upper == lower) {
inTable = false;
return upper;
// no match
} else if (index == lower) {
inTable = false;
if ((index == 0) && (arrayMem[index].vaddr > vaddr))
return 0;
else
return index + 1;
} else {
if (arrayMem[index].vaddr > vaddr)
return find_vaddr(vaddr, index, lower);
else
return find_vaddr(vaddr, upper, index);
}
assert(0);
return -1;
}
// inserts virtual address struct into the list
void
CoreUtils::insert_table(intptr_t vaddr1, intptr_t offset1, intptr_t fsize1)
{
// TODO: What was this intended for??
#if 0
memTable m;
m.vaddr = vaddr1;
m.offset = offset1;
m.fsize = fsize1;
#endif
if (arrayMem.length() == 0) {
arrayMem(0);
arrayMem[(intptr_t)0].vaddr = vaddr1;
arrayMem[(intptr_t)0].offset = offset1;
arrayMem[(intptr_t)0].fsize = fsize1;
} else {
intptr_t index = find_vaddr(vaddr1, arrayMem.length(), 0);
if (index == arrayMem.length()) {
arrayMem(index);
arrayMem[index].vaddr = vaddr1;
arrayMem[index].offset = offset1;
arrayMem[index].fsize = fsize1;
} else if (index == 0) {
arrayMem(arrayMem.length());
for (intptr_t i = 0; i < arrayMem.length(); i++) {
arrayMem[arrayMem.length() - i - 1].vaddr = arrayMem[arrayMem.length() - i - 2].vaddr;
arrayMem[arrayMem.length() - i - 1].offset = arrayMem[arrayMem.length() - i - 2].offset;
arrayMem[arrayMem.length() - i - 1].fsize = arrayMem[arrayMem.length() - i - 2].fsize;
}
arrayMem[(intptr_t)0].vaddr = vaddr1;
arrayMem[(intptr_t)0].offset = offset1;
arrayMem[(intptr_t)0].fsize = fsize1;
} else {
arrayMem(arrayMem.length());
for (intptr_t i = 1; i < arrayMem.length() - index; i++) {
arrayMem[arrayMem.length() - i].vaddr = arrayMem[arrayMem.length() - i - 1].vaddr;
arrayMem[arrayMem.length() - i].offset = arrayMem[arrayMem.length() - i - 1].offset;
arrayMem[arrayMem.length() - i].fsize = arrayMem[arrayMem.length() - i - 1].fsize;
}
arrayMem[index].vaddr = vaddr1;
arrayMem[index].offset = offset1;
arrayMem[index].fsize = fsize1;
}
}
}
// returns -1 if not found in table or fills the buffer with characters
// from the beginning of the memory section
intptr_t
CoreUtils::read_core_memory(intptr_t vaddr, intptr_t length, char *buf, FILE * fp)
{
intptr_t index = find_vaddr(vaddr, arrayMem.length(), 0);
if (inTable == false)
return -1;
else {
intptr_t offset = arrayMem[index].offset;
if (fseek(fp, offset, SEEK_SET) != -1) {
for (int j = 0; j < length; j++) {
*buf++ = (char) getc(fp);
}
buf -= length;
}
return 1;
}
}
// returns -1 if not found in table or fills the buffer with characters
// from the beginning of the memory section + offset
intptr_t
CoreUtils::read_core_memory(intptr_t offset, intptr_t vaddr, intptr_t length, char *buf, FILE * fp)
{
intptr_t index = find_vaddr(vaddr, arrayMem.length(), 0);
if (inTable == false)
return -1;
else {
intptr_t offset2 = arrayMem[index].offset;
if (fseek(fp, offset2 + offset, SEEK_SET) != -1) {
for (int j = 0; j < length; j++) {
*buf++ = (char) getc(fp);
}
buf -= length;
}
return 1;
}
}
// returns -1 on failure otherwise fills the buffer and
// returns the number of bytes read
intptr_t
CoreUtils::read_from_core(intptr_t vaddr, intptr_t bytes, char *buf)
{
intptr_t index = find_vaddr(vaddr, arrayMem.length(), 0);
intptr_t vadd = arrayMem[index - 1].vaddr;
intptr_t offset = arrayMem[index - 1].offset;
intptr_t size = arrayMem[index - 1].fsize;
intptr_t offset2 = abs(vaddr - vadd);
if (bytes > (size - offset2))
return -1;
else {
if (fseek(fp, offset2 + offset, SEEK_SET) != -1) {
char *frameoff;
if ((frameoff = (char *)ats_malloc(sizeof(char) * bytes))) {
if (fread(frameoff, bytes, 1, fp) == 1) {
memcpy(buf, frameoff, bytes);
/*for(int j =0; j < bytes; j++) {
*buf++ = getc(fp);
}
buf -= bytes;*/
ats_free(frameoff);
return bytes;
}
ats_free(frameoff);
}
} else
return -1;
}
return -1;
}
/* SPARC specific functions */
#if defined(sparc)
// returns the active thread id
int
CoreUtils::get_active_thread_Id()
{
return lwpid;
}
// copies stack info for the thread's base frame to the given
// core_stack_state pointer
void
CoreUtils::get_base_frame(intptr_t threadId, core_stack_state * coress)
{
intptr_t framep = arrayLwp[threadId - 1].framep;
// finds vaddress less than framep
intptr_t index = find_vaddr(framep, arrayMem.length(), 0);
intptr_t vadd = arrayMem[index - 1].vaddr;
intptr_t off = arrayMem[index - 1].offset;
intptr_t off2 = abs(vadd - framep);
intptr_t size = arrayMem[index - 1].fsize;
memset(coress, 0, sizeof(*coress));
// seek to the framep offset
if (fseek(fp, off + off2, SEEK_SET) != -1) {
char *frameoff;
if ((frameoff = (char *)ats_malloc(sizeof(char) * sizeof(rwindow)))) {
if (fread(frameoff, sizeof(rwindow), 1, fp) == 1) {
// memcpy rwindow struct and print out
struct rwindow regs, *r;
r = (struct rwindow *) frameoff;
memcpy(®s, r, sizeof(struct rwindow));
coress->regs = regs;
framep = regs.rw_in[6];
coress->framep = framep;
}
ats_free(frameoff);
} else {
printf("Failed to seek to top of the stack\n");
}
coress->stkbase = vadd + size;
}
}
// returns 0 if current frame is already at the top of the stack
// or returns 1 and moves up the stack once
int
CoreUtils::get_next_frame(core_stack_state * coress)
{
int framep = coress->framep;
if (framep >= coress->stkbase) {
if (is_debug_tag_set("stack")) {
printf("already at top of stack\n");
}
return 0;
} else {
if (is_debug_tag_set("stack")) {
printf("distance from end of the stack: %d\n", coress->stkbase - framep);
}
int index = find_vaddr(framep, arrayMem.length(), 0);
// finds vaddress less than framep
int vadd = arrayMem[index - 1].vaddr;
int off = arrayMem[index - 1].offset;
int off2 = abs(vadd - framep);
// seek to the framep offset
if (fseek(fp, off + off2, SEEK_SET) != -1) {
char *frameoff;
if ((frameoff = (char *)ats_malloc(sizeof(char) * sizeof(rwindow)))) {
if (fread(frameoff, sizeof(rwindow), 1, fp) == 1) {
// memcpy rwindow struct and print out
struct rwindow regs, *r;
r = (struct rwindow *) frameoff;
memcpy(®s, r, sizeof(struct rwindow));
coress->regs = regs;
framep = regs.rw_in[6];
coress->framep = framep;
}
}
ats_free(frameoff);
}
}
return 1;
}
// prints the http header
void
CoreUtils::find_stuff(StuffTest_f f)
{
core_stack_state coress;
int i;
void *test_val;
int id = get_active_thread_Id();
get_base_frame(id, &coress);
do {
// looping through all of the local and in registers
// all the way up the stack
for (int i = 0; i < 8; i++) {
test_val = (void *) coress.regs.rw_local[i];
f(test_val);
}
for (i = 0; i < 8; i++) {
test_val = (void *) coress.regs.rw_in[i];
f(test_val);
}
} while (get_next_frame(&coress) != 0);
}
#endif /* SPARC specific Stack unwinding */
/* Linux Specific functions */
#if defined(linux)
// copies stack info for the thread's base frame to the given
// core_stack_state pointer
void
CoreUtils::get_base_frame(intptr_t framep, core_stack_state *coress)
{
// finds vaddress less than framep
intptr_t index = find_vaddr(framep, arrayMem.length(), 0);
intptr_t vadd = arrayMem[index - 1].vaddr;
intptr_t off = arrayMem[index - 1].offset;
intptr_t off2 = abs(vadd - framep);
intptr_t size = arrayMem[index - 1].fsize;
intptr_t i = 0;
memset(coress, 0, sizeof(*coress));
D(printf("stkbase=%p\n", (void*)(vadd + size)));
// seek to the framep offset
if (fseek(fp, off + off2, SEEK_SET) != -1) {
void **frameoff;
if ((frameoff = (void **)ats_malloc(sizeof(long)))) {
if (fread(frameoff, 4, 1, fp) == 1) {
coress->framep = (intptr_t) *frameoff;
if (fread(frameoff, 4, 1, fp) == 1) {
coress->pc = (intptr_t) *frameoff;
}
// read register arguments
for (i = 0; i < NO_OF_ARGS; i++) {
if (fread(frameoff, 4, 1, fp) == 1) {
coress->arg[i] = (intptr_t) *frameoff;
}
}
}
ats_free(frameoff);
}
} else {
printf("Failed to seek to top of the stack\n");
}
//coress->stkbase = vadd+size;
}
// returns 0 if current frame is already at the top of the stack
// or returns 1 and moves up the stack once
int
CoreUtils::get_next_frame(core_stack_state * coress)
{
intptr_t i = 0;
intptr_t framep = coress->framep;
intptr_t index = find_vaddr(framep, arrayMem.length(), 0);
// finds vaddress less than framep
intptr_t vadd = arrayMem[index - 1].vaddr;
intptr_t off = arrayMem[index - 1].offset;
intptr_t off2 = abs(vadd - framep);
// seek to the framep offset
if (fseek(fp, off + off2, SEEK_SET) != -1) {
void **frameoff;
if ((frameoff = (void **)ats_malloc(sizeof(long)))) {
if (fread(frameoff, 4, 1, fp) == 1) {
coress->framep = (intptr_t) *frameoff;
if (*frameoff == NULL) {
ats_free(frameoff);
return 0;
}
if (fread(frameoff, 4, 1, fp) == 1) {
coress->pc = (intptr_t) *frameoff;
}
for (i = 0; i < NO_OF_ARGS; i++) {
if (fread(frameoff, 4, 1, fp) == 1) {
coress->arg[i] = (intptr_t) *frameoff;
}
}
}
ats_free(frameoff);
}
return 1;
}
return 0;
}
// prints the http header
void
CoreUtils::find_stuff(StuffTest_f f)
{
intptr_t framep = framepointer;
intptr_t pc = program_counter;
core_stack_state coress;
intptr_t i;
void *test_val;
int framecount = 0;
// Unwinding the stack
D(printf("\nStack Trace:\n"));
D(printf("stack frame#%d framep=%p pc=%p\n", framecount, (void*)framep, (void*)pc));
framecount++;
get_base_frame(framep, &coress);
f2 = framep;
do {
f1 = f2;
f2 = coress.framep;
D(printf
("stack frame#%d framep=%p pc=%p f1-f2=%p coress=%p %p %p %p %p\n", framecount, (void*)coress.framep, (void*)coress.pc,
(void*)(f2 - f1), (void*)coress.arg[0], (void*)coress.arg[1], (void*)coress.arg[2], (void*)coress.arg[3], (void*)coress.arg[4]));
for (i = 0; i < NO_OF_ARGS; i++) {
test_val = (void *) coress.arg[i];
f(test_val);
}
framecount++;
} while (get_next_frame(&coress) != 0);
}
#endif // linux check
// test whether a given register is an HttpSM
// if it is, call process_HttpSM on it
void
CoreUtils::test_HdrHeap(void *arg)
{
HdrHeap *hheap_test = (HdrHeap *) arg;
uint32_t *magic_ptr = &(hheap_test->m_magic);
uint32_t magic = 0;
if (read_from_core((intptr_t) magic_ptr, sizeof(uint32_t), (char *) &magic) != 0) {
if (magic == HDR_BUF_MAGIC_ALIVE ||
magic == HDR_BUF_MAGIC_DEAD || magic == HDR_BUF_MAGIC_CORRUPT || magic == HDR_BUF_MAGIC_MARSHALED) {
// This is not 64-bit correct ... /leif
printf("Found Hdr Heap @ 0x%p\n", arg);
}
}
}
// test whether a given register is an HttpSM
// if it is, call process_HttpSM on it
void
CoreUtils::test_HttpSM_from_tunnel(void *arg)
{
char *tmp = (char *) arg;
intptr_t offset = (intptr_t) &(((HttpTunnel *) NULL)->sm);
HttpSM **hsm_ptr = (HttpSM **) (tmp + offset);
HttpSM *hsm_test;
if (read_from_core((intptr_t) hsm_ptr, sizeof(HttpSM *), (char *) &hsm_test) == 0)
return;
unsigned int *magic_ptr = &(hsm_test->magic);
unsigned int magic = 0;
if (read_from_core((intptr_t) magic_ptr, sizeof(int), (char *) &magic) != 0) {
if (magic == HTTP_SM_MAGIC_ALIVE || magic == HTTP_SM_MAGIC_DEAD) {
process_HttpSM(hsm_test);
}
}
}
// test whether a given register is an HttpSM
// if it is, call process_HttpSM on it
void
CoreUtils::test_HttpSM(void *arg)
{
HttpSM *hsm_test = (HttpSM *) arg;
unsigned int *magic_ptr = &(hsm_test->magic);
unsigned int magic = 0;
if (read_from_core((intptr_t) magic_ptr, sizeof(int), (char *) &magic) != 0) {
if (magic == HTTP_SM_MAGIC_ALIVE || magic == HTTP_SM_MAGIC_DEAD) {
printf("test_HttpSM:******MATCH*****\n");
process_HttpSM(hsm_test);
}
}
}
void
CoreUtils::process_HttpSM(HttpSM * core_ptr)
{
#if defined(sparc)
int id = get_active_thread_Id();
#endif
// extracting the HttpSM from the core file
if (last_seen_http_sm != core_ptr) {
HttpSM *http_sm = (HttpSM *)ats_malloc(sizeof(HttpSM));
if (read_from_core((intptr_t) core_ptr, sizeof(HttpSM), (char *) http_sm) < 0) {
// This is not 64-bit correct ... /leif
printf("ERROR: Failed to read httpSM @ 0x%p from core\n", core_ptr);
ats_free(http_sm);
return;
}
if (http_sm->magic == HTTP_SM_MAGIC_ALIVE) {
last_seen_http_sm = core_ptr;
if (is_debug_tag_set("magic")) {
#if defined(linux)
printf("\n*****match-ALIVE*****\n");
#endif
#if defined(sparc)
printf("\n*****match-ALIVE*****!! lwpid: %d\n", arrayLwp[id].lwpId);
#endif
}
// I don't think this is 64-bit correct. /leif
printf("---- Found HttpSM --- id %" PRId64 " ------ @ 0x%p -----\n\n", http_sm->sm_id, http_sm);
print_http_hdr(&http_sm->t_state.hdr_info.client_request, "Client Request");
print_http_hdr(&http_sm->t_state.hdr_info.server_request, "Server Request");
print_http_hdr(&http_sm->t_state.hdr_info.server_response, "Server Response");
print_http_hdr(&http_sm->t_state.hdr_info.client_response, "Client Response");
dump_history(http_sm);
printf("------------------------------------------------\n\n\n");
} else if (http_sm->magic == HTTP_SM_MAGIC_DEAD) {
if (is_debug_tag_set("magic")) {
#if defined(linux)
printf("\n*****match-DEAD*****\n");
#endif
#if defined(sparc)
printf("\n*****match-DEAD*****!! lwpid: %d\n", arrayLwp[id].lwpId);
#endif
}
} else {
if (is_debug_tag_set("magic")) {
// Nothing here? /leif
;
}
}
ats_free(http_sm);
} else
printf("process_HttpSM : last_seen_http_sm == core_ptr\n");
}
void
CoreUtils::print_http_hdr(HTTPHdr * h, const char *name)
{
HTTPHdr new_handle;
if (h->m_heap && h->m_http) {
int r = load_http_hdr(h, &new_handle);
if (r > 0) {
printf("----------- %s ------------\n", name);
new_handle.m_mime = new_handle.m_http->m_fields_impl;
new_handle.print(NULL, 0, NULL, NULL);
printf("-----------------------------\n\n");
}
}
}
int
CoreUtils::load_http_hdr(HTTPHdr * core_hdr, HTTPHdr * live_hdr)
{
// Load HdrHeap chain
HTTPHdr *http_hdr = core_hdr;
HdrHeap *heap = (HdrHeap *) core_hdr->m_heap;
HdrHeap *heap_ptr = (HdrHeap *) http_hdr->m_heap;
char *buf = (char *)ats_malloc(sizeof(char) * sizeof(HdrHeap));
intptr_t ptr_heaps = 0;
intptr_t ptr_heap_size = 0;
intptr_t ptr_xl_size = 2;
intptr_t str_size = 0;
intptr_t str_heaps = 0;
MarshalXlate default_MarshalXlate = { 0, 0, 0 };
DynArray<struct MarshalXlate>ptr_xlation(&default_MarshalXlate, 2);
//MarshalXlate static_table[2];
//MarshalXlate* ptr_xlation = static_table;
intptr_t used;
intptr_t i;
intptr_t copy_size;
// extracting the header heap from the core file
do {
if (read_from_core((intptr_t) heap, sizeof(HdrHeap), buf) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
heap = (HdrHeap *) buf;
copy_size = (int) (heap->m_free_start - heap->m_data_start);
ptr_heap_size += copy_size;
heap = heap->m_next;
} while (heap && ((intptr_t) heap != 0x1));
swizzle_heap = (HdrHeap *)ats_malloc(sizeof(HdrHeap));
live_hdr->m_heap = swizzle_heap;
ptr_data = (char *)ats_malloc(sizeof(char) * ptr_heap_size);
//heap = (HdrHeap*)http_hdr->m_heap;
// Build Hdr Heap Translation Table
do {
if (read_from_core((intptr_t) heap_ptr, sizeof(HdrHeap), buf) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
heap_ptr = (HdrHeap *) buf;
copy_size = (int) (heap_ptr->m_free_start - heap_ptr->m_data_start);
if (read_from_core((intptr_t) heap_ptr->m_data_start, copy_size, ptr_data) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
// Expand ptr xlation table if necessary
if (ptr_heaps >= ptr_xl_size) {
ptr_xlation(ptr_heaps);
}
char *data, *free, *off;
data = heap_ptr->m_data_start;
free = heap_ptr->m_free_start;
off = (char *) (heap_ptr->m_data_start - ptr_data);
ptr_xlation[ptr_heaps].start = data;
ptr_xlation[ptr_heaps].end = free;
ptr_xlation[ptr_heaps].offset = off;
ptr_data += copy_size;
ptr_heaps++;
heap_ptr = heap_ptr->m_next;
} while (heap_ptr && ((intptr_t) heap_ptr != 0x1));
heap = (HdrHeap *) http_hdr->m_heap;
if (read_from_core((intptr_t) heap, sizeof(HdrHeap), buf) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
heap = (HdrHeap *) buf;
// filling in the live_hdr
swizzle_heap->m_free_start = NULL;
swizzle_heap->m_data_start = (char *) ptr_data - ptr_heap_size; // offset
swizzle_heap->m_magic = HDR_BUF_MAGIC_ALIVE;
swizzle_heap->m_writeable = false;
swizzle_heap->m_size = ptr_heap_size;
swizzle_heap->m_next = NULL;
swizzle_heap->m_free_size = 0;
swizzle_heap->m_read_write_heap.m_ptr = NULL;
// We'have one read-only string heap after marshalling
swizzle_heap->m_ronly_heap[0].m_heap_start = (char *)(intptr_t)swizzle_heap->m_size; // offset
swizzle_heap->m_ronly_heap[0].m_ref_count_ptr.m_ptr = NULL;
for (int i = 1; i < HDR_BUF_RONLY_HEAPS; i++)
swizzle_heap->m_ronly_heap[i].m_heap_start = NULL;
// Next order of business is to copy over string heaps
// As we are copying over the string heaps, build
// translation table for string marshaling in the heap
// objects
MarshalXlate str_xlation[HDR_BUF_RONLY_HEAPS + 1];
// Local String Heaps, building translation table
if (heap->m_read_write_heap) {
HdrStrHeap *hdr = (HdrStrHeap *) heap->m_read_write_heap.m_ptr;
char *copy_start = ((char *) heap->m_read_write_heap.m_ptr) + sizeof(HdrStrHeap);
char *str_hdr = (char *)ats_malloc(sizeof(char) * sizeof(HdrStrHeap));
if (read_from_core((intptr_t) hdr, sizeof(HdrStrHeap), str_hdr) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
char *free_start = (char *) (((HdrStrHeap *) str_hdr)->m_free_start);
int nto_copy = abs((char *) copy_start - free_start);
ats_free(str_hdr);
#if defined(__GNUC__)
char rw_heap[sizeof(char) * nto_copy];
#else
char *rw_heap = (char *)ats_malloc(sizeof(char) * nto_copy);
#endif
if (read_from_core((intptr_t) copy_start, nto_copy, rw_heap) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
// FIX ME - possible offset overflow issues?
str_xlation[str_heaps].start = copy_start;
str_xlation[str_heaps].end = copy_start + nto_copy;
str_xlation[str_heaps].offset = (char *) (copy_start - rw_heap);
str_size += nto_copy;
str_heaps++;
#if !defined(__GNUC__)
ats_free(rw_heap);
#endif
}
for (i = 0; i < HDR_BUF_RONLY_HEAPS; i++) {
if (heap->m_ronly_heap[i].m_heap_start != NULL) {
#if defined(__GNUC__)
char ro_heap[sizeof(char) * heap->m_ronly_heap[i].m_heap_len];
#else
char * ro_heap = (char *)ats_malloc(sizeof(char) * heap->m_ronly_heap[i].m_heap_len);
#endif
if (read_from_core((intptr_t) heap->m_ronly_heap[i].m_heap_start, heap->m_ronly_heap[i].m_heap_len, ro_heap) == -1) {
printf("Cannot read from core\n");
_exit(0);
}
// Add translation table entry for string heaps
str_xlation[str_heaps].start = heap->m_ronly_heap[i].m_heap_start;
str_xlation[str_heaps].end = heap->m_ronly_heap[i].m_heap_start + heap->m_ronly_heap[i].m_heap_len;
str_xlation[str_heaps].offset = (char *) (heap->m_ronly_heap[i].m_heap_start - ro_heap);
ink_assert(str_xlation[str_heaps].start <= str_xlation[str_heaps].end);
str_heaps++;
str_size += heap->m_ronly_heap[i].m_heap_len;
#if !defined(__GNUC__)
ats_free(ro_heap);
#endif
}
}
// Patch the str heap len
swizzle_heap->m_ronly_heap[0].m_heap_len = str_size;
char *obj_data = swizzle_heap->m_data_start;
char *mheap_end = swizzle_heap->m_data_start + swizzle_heap->m_size;
while (obj_data < mheap_end) {
HdrHeapObjImpl *obj = (HdrHeapObjImpl *) obj_data;
ink_assert(obj_is_aligned(obj));
switch (obj->m_type) {
case HDR_HEAP_OBJ_URL:
if (((URLImpl *) obj)->marshal(str_xlation, str_heaps) < 0) {
goto Failed;
}
break;
case HDR_HEAP_OBJ_HTTP_HEADER:
if (((HTTPHdrImpl *) obj)->marshal(ptr_xlation, ptr_heaps, str_xlation, str_heaps) < 0) {
goto Failed;
}
live_hdr->m_http = (HTTPHdrImpl *) obj;
break;
case HDR_HEAP_OBJ_FIELD_BLOCK:
if (((MIMEFieldBlockImpl *) obj)->marshal(ptr_xlation, ptr_heaps, str_xlation, str_heaps) < 0) {
goto Failed;
}
break;
case HDR_HEAP_OBJ_MIME_HEADER:
if (((MIMEHdrImpl *) obj)->marshal(ptr_xlation, ptr_heaps, str_xlation, str_heaps)) {
goto Failed;
}
break;
case HDR_HEAP_OBJ_EMPTY:
break;
case HDR_HEAP_OBJ_RAW:
// Check to make sure we aren't stuck
// in an infinite loop
if (obj->m_length <= 0) {
ink_assert(0);
goto Failed;
}
// Nothing to do
break;
default:
ink_release_assert(0);
}
obj_data = obj_data + obj->m_length;
}
// Add up the total bytes used
used = ptr_heap_size + str_size + HDR_HEAP_HDR_SIZE;
used = ROUND(used, HDR_PTR_SIZE);
return used;
Failed:
swizzle_heap->m_magic = HDR_BUF_MAGIC_CORRUPT;
return -1;
}
void
CoreUtils::dump_history(HttpSM * hsm)
{
printf("-------- Begin History -------------\n");
// Loop through the history and dump it
for (int i = 0; i < hsm->history_pos; i++) {
int r = (int) hsm->history[i].reentrancy;
int e = (int) hsm->history[i].event;
char *fileline = load_string(hsm->history[i].fileline);
fileline = (fileline != NULL) ? fileline : ats_strdup("UNKNOWN");
printf("%d %d %s", e, r, fileline);
char buffer[32];
const char *msg = event_int_to_string(e, sizeof(buffer), buffer);
printf(" event string: \"%s\"\n", msg);
ats_free(fileline);
}
printf("-------- End History -----------\n\n");
}
intptr_t
CoreUtils::read_heap_header(intptr_t vaddr, intptr_t bytes, HdrHeap h)
{
intptr_t index = find_vaddr(vaddr, arrayMem.length(), 0);
intptr_t vadd = arrayMem[index - 1].vaddr;
intptr_t offset = arrayMem[index - 1].offset;
intptr_t size = arrayMem[index - 1].fsize;
intptr_t offset2 = abs(vaddr - vadd);
if (bytes > (size - offset2))
return -1;
else {
if (fseek(fp, offset2 + offset, SEEK_SET) != -1) {
if (fread(&h, sizeof(HdrHeap), 1, fp) > 0)
return bytes;
else
return -1;
}
}
return -1;
}
void
CoreUtils::process_EThread(EThread * eth_test)
{
char *buf = (char *)ats_malloc(sizeof(char) * sizeof(EThread));
if (read_from_core((intptr_t) eth_test, sizeof(EThread), buf) != -1) {
EThread *loaded_eth = (EThread *) buf;
// This is not 64-bit correct. /leif
printf("----------- EThread @ 0x%p ----------\n", eth_test);
#if !defined(kfreebsd) && (defined(freebsd) || defined(darwin) || defined(openbsd))
printf(" thread_id: %p\n", loaded_eth->tid);
#else
printf(" thread_id: %i\n", (int) loaded_eth->tid);
#endif
// printf(" NetHandler: 0x%x\n\n", (int) loaded_eth->netHandler);
}
ats_free(buf);
}
static void
print_netstate(NetState * n)
{
// These might not be 64-bit correct. /leif
printf(" enabled: %d\n", n->enabled);
printf(" op: %d _cont: 0x%p\n", n->vio.op, n->vio._cont);
printf(" nbytes: %d done: %d\n", (int)n->vio.nbytes, (int)n->vio.ndone);
printf(" vc_server: 0x%p mutex: 0x%p\n\n", n->vio.vc_server, n->vio.mutex.m_ptr);
}
void
CoreUtils::process_NetVC(UnixNetVConnection * nvc_test)
{
char *buf = (char *)ats_malloc(sizeof(char) * sizeof(UnixNetVConnection));
if (read_from_core((intptr_t) nvc_test, sizeof(UnixNetVConnection), buf) != -1) {
UnixNetVConnection *loaded_nvc = (UnixNetVConnection *) buf;