forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lkcd_x86_trace.c
6215 lines (5653 loc) · 138 KB
/
lkcd_x86_trace.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
/*
* Copyright 1999 Silicon Graphics, Inc. All rights reserved.
*/
/*
* lkcd_x86_trace.c
*
* Copyright (C) 2002-2012, 2017-2018 David Anderson
* Copyright (C) 2002-2012, 2017-2018 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Adapted as noted from the following LKCD files:
*
* lkcdutils-4.1/lcrash/arch/i386/lib/dis.c
* lkcdutils-4.1/lcrash/arch/i386/lib/trace.c
* lkcdutils-4.1/libutil/kl_queue.c
*/
#ifdef X86
#ifdef REDHAT
#include "lkcd_x86_trace.h"
#undef XEN_HYPER_MODE
static int XEN_HYPER_MODE(void) { return (pc->flags & XEN_HYPER) != 0; }
static void *kl_alloc_block(int, int);
static void kl_free_block(void *);
static void GET_BLOCK(kaddr_t, unsigned, void *);
static void kl_get_kaddr(kaddr_t, void *);
static char *kl_funcname(kaddr_t);
static kaddr_t kl_funcaddr(kaddr_t);
static syment_t *kl_lkup_symaddr(kaddr_t);
static k_error_t kl_get_task_struct(kaddr_t, int, void *);
static kaddr_t kl_kernelstack(kaddr_t);
static kaddr_t get_call_pc(kaddr_t);
static kaddr_t get_call_pc_v2(kaddr_t);
static int get_jmp_instr(kaddr_t, kaddr_t, kaddr_t *, char *, char **);
static int is_push(unsigned int);
static int is_pop(unsigned int);
static int get_framesize(kaddr_t, struct bt_info *);
static int cache_framesize(int, kaddr_t funcaddr, int *, void **);
struct framesize_cache;
static int framesize_modify(struct framesize_cache *);
struct framesize_mods;
static int compiler_matches(struct framesize_mods *);
static sframe_t *alloc_sframe(trace_t *, int);
static void free_sframes(trace_t *);
static void free_trace_rec(trace_t *);
static void clean_trace_rec(trace_t *);
static int setup_trace_rec(kaddr_t, kaddr_t, int, trace_t *);
static int valid_ra(kaddr_t);
static int valid_ra_function(kaddr_t, char *);
static int eframe_incr(kaddr_t, char *);
static int find_trace(kaddr_t, kaddr_t, kaddr_t, kaddr_t, trace_t *, int);
static void dump_stack_frame(trace_t *, sframe_t *, FILE *);
static void print_trace(trace_t *, int, FILE *);
static int eframe_type(uaddr_t *);
static char *funcname_display(char *, ulong, struct bt_info *, char *);
static void print_eframe(FILE *, uaddr_t *);
static void trace_banner(FILE *);
static void print_kaddr(kaddr_t, FILE *, int);
int do_text_list(kaddr_t, int, FILE *);
int print_traces(struct bt_info *, int, int, FILE *);
static int get_instr_info(kaddr_t, instr_rec_t *);
static instr_rec_t *get_instr_stream(kaddr_t, int, int);
static void free_instr_stream(instr_rec_t *);
static trace_t *alloc_trace_rec(int);
static void kl_enqueue(element_t**, element_t*);
static element_t *kl_dequeue(element_t**);
static void handle_trace_error(struct bt_info *, int, FILE *);
static int verify_back_trace(struct bt_info *);
static int recoverable(struct bt_info *, FILE *);
static void fill_instr_cache(kaddr_t, char *);
static void do_bt_reference_check(struct bt_info *, sframe_t *);
static void print_stack_entry(struct bt_info *, int, ulong, ulong, char *,
sframe_t *, FILE *);
static struct syment *eframe_label(char *, ulong);
static int dump_framesize_cache(FILE *, struct framesize_cache *);
static int modify_framesize_cache_entry(FILE *, ulong, int);
static int framesize_debug(struct bt_info *, FILE *);
static int kernel_entry_from_user_space(sframe_t *, struct bt_info *);
k_error_t klib_error = 0;
static void *
kl_alloc_block(int size, int flags)
{
return ((void *)GETBUF(size));
}
static void
kl_free_block(void *blk)
{
if (blk)
FREEBUF(blk);
}
static void
GET_BLOCK(kaddr_t addr, unsigned size, void *buffer)
{
KL_ERROR = 0;
if (!readmem(addr, KVADDR, (void *)buffer, (ulong)size,
"GET_BLOCK", RETURN_ON_ERROR|QUIET)) {
console("GET_BLOCK: %lx (%d/0x%x)\n", addr, size, size);
KL_ERROR = KLE_INVALID_READ;
}
}
static void
kl_get_kaddr(kaddr_t addr, void *bp)
{
KL_ERROR = 0;
GET_BLOCK(addr, 4, bp);
}
static char *
kl_funcname(kaddr_t pc)
{
struct syment *sp;
char *buf, *name;
struct load_module *lm;
if ((sp = value_search(pc, NULL))) {
if (STREQ(sp->name, "_stext") &&
(sp->value == (sp+1)->value))
sp++;
switch (sp->type)
{
case 'r':
if (strstr(sp->name, "_interrupt") ||
STREQ(sp->name, "call_do_IRQ"))
return sp->name;
break;
case 't':
case 'T':
return sp->name;
}
if (is_kernel_text(pc))
return sp->name;
}
if (IS_MODULE_VADDR(pc)) {
buf = GETBUF(BUFSIZE);
name = &buf[BUFSIZE/2];
if (module_symbol(pc, NULL, NULL, buf, output_radix)) {
sprintf(name, "(%s)", buf);
return name;
} else {
FREEBUF(buf);
return "(unknown module)";
}
}
if ((lm = init_module_function(pc)))
return ("init_module");
return NULL;
}
static kaddr_t
kl_funcaddr(kaddr_t pc)
{
struct syment *sp;
struct load_module *lm;
if ((sp = value_search(pc, NULL))) {
switch (sp->type)
{
case 'r':
if (strstr(sp->name, "_interrupt") ||
STREQ(sp->name, "call_do_IRQ"))
return sp->value;
break;
case 't':
case 'T':
return sp->value;
}
if (is_kernel_text(pc))
return sp->value;
}
if ((lm = init_module_function(pc)))
return lm->mod_init_module_ptr;
return((kaddr_t)NULL);
}
static struct syment init_module_syment = {
.name = "init_module",
.type = 't',
};
static syment_t *
kl_lkup_symaddr(kaddr_t addr)
{
struct syment *sp;
struct load_module *lm;
if ((sp = value_search(addr, NULL)))
return sp;
if ((lm = init_module_function(addr))) {
init_module_syment.value = lm->mod_init_module_ptr;
return &init_module_syment;
}
return NULL;
}
static k_error_t
kl_get_task_struct(kaddr_t value, int mode, void *tsp)
{
KL_ERROR = 0;
if (value == tt->last_task_read)
BCOPY(tt->task_struct, tsp, TASK_STRUCT_SZ);
else
GET_BLOCK(value, TASK_STRUCT_SZ, tsp);
return KL_ERROR;
}
static kaddr_t
kl_kernelstack(kaddr_t task)
{
kaddr_t saddr;
return (saddr = (task + KSTACK_SIZE));
}
static void
print_kaddr(kaddr_t kaddr, FILE *ofp, int flag)
{
fprintf(ofp, "%lx", (ulong)kaddr);
}
#endif /* REDHAT */
/*
* lkcdutils-4.1/lcrash/arch/i386/lib/trace.c
*/
#ifndef REDHAT
/*
* Copyright 1999 Silicon Graphics, Inc. All rights reserved.
*/
#include <lcrash.h>
#include <asm/lc_dis.h>
#include <strings.h>
#endif /* !REDHAT */
/*
* get_call_pc()
*/
kaddr_t
get_call_pc(kaddr_t ra)
{
kaddr_t addr = 0;
instr_rec_t *irp;
if (!(irp = get_instr_stream(ra, 1, 0))) {
return((kaddr_t)NULL);
}
if (!irp->prev) {
free_instr_stream(irp);
return((kaddr_t)NULL);
}
if ((irp->prev->opcode == 0x00e8) || (irp->prev->opcode == 0xff02)) {
addr = irp->prev->addr;
}
free_instr_stream(irp);
/*
* If the old LKCD code fails, try disassembling...
*/
if (!addr)
return get_call_pc_v2(ra);
return(addr);
}
kaddr_t
get_call_pc_v2(kaddr_t ra)
{
int c ATTRIBUTE_UNUSED;
int line, len;
kaddr_t addr, addr2;
ulong offset;
struct syment *sp;
char *arglist[MAXARGS];
char buf[BUFSIZE];
if ((sp = value_search(ra, &offset))) {
if (offset == 0)
return 0;
} else
return 0;
addr = 0;
for (len = 2; len < 8; len++) {
open_tmpfile2();
sprintf(buf, "x/2i 0x%x", ra - len);
if (!gdb_pass_through(buf, pc->tmpfile2, GNU_RETURN_ON_ERROR)) {
close_tmpfile2();
return 0;
}
rewind(pc->tmpfile2);
line = 1;
while (fgets(buf, BUFSIZE, pc->tmpfile2)) {
c = parse_line(buf, arglist);
if ((line == 1) && !STREQ(arglist[2], "call"))
break;
if (line == 2) {
addr2 = (kaddr_t)htol(arglist[0], RETURN_ON_ERROR|QUIET, 0);
if (addr2 == ra) {
addr = ra - len;
break;
}
}
line++;
}
close_tmpfile2();
if (addr) {
if (CRASHDEBUG(1)) {
fprintf(fp, "get_call_pc_v2(ra: %x) -> %x -> ", ra, addr);
if (value_to_symstr(addr, buf, 0))
fprintf(fp, "%s", buf);
fprintf(fp, "\n");
}
break;
}
}
return addr;
}
/*
* get_jmp_instr()
*/
int
get_jmp_instr(kaddr_t addr, kaddr_t isp, kaddr_t *caddr, char *fname,
char **cfname)
{
kaddr_t a;
int offset;
instr_rec_t *irp;
if (!(irp = get_instr_stream(addr, 1, 0))) {
return(1);
}
if (!irp->prev) {
free_instr_stream(irp);
return(1);
}
irp = irp->prev;
if (!(irp->opcode == 0x00e8) && !(irp->opcode == 0xff02)) {
free_instr_stream(irp);
return(1);
}
/* Check for the easiest case first...
*/
if (irp->opcode == 0xe8) {
a = irp->operand[0].op_addr;
if ((*cfname = kl_funcname(a))) {
*caddr = a;
}
} else if (irp->opcode == 0xff02) {
switch (irp->modrm) {
case 0x14:
if (irp->sib == 0x85) {
kl_get_kaddr(addr - 4, &a);
if (KL_ERROR) {
free_instr_stream(irp);
return(1);
}
if (strstr(fname, "system_call")) {
GET_BLOCK(isp + 28, 4, &offset);
a += (offset * 4);
kl_get_kaddr(a, &a);
if ((*cfname =
kl_funcname(a))) {
*caddr = a;
}
}
}
break;
case 0xc2: /* EAX */
case 0xca: /* ECX */
case 0xd2: /* EDX */
case 0xda: /* EBX */
case 0xea: /* EBP */
case 0xf2: /* ESI */
case 0xfa: /* EDI */
break;
}
}
free_instr_stream(irp);
return(0);
}
/*
* is_push()
*/
int
is_push(unsigned int opcode)
{
switch(opcode) {
case 0x0006:
case 0x000e:
case 0x0016:
case 0x001e:
case 0x0050:
case 0x0051:
case 0x0052:
case 0x0053:
case 0x0054:
case 0x0055:
case 0x0056:
case 0x0057:
case 0x0068:
case 0x006a:
case 0x009c:
case 0x0fa0:
case 0x0fa8:
case 0xff06:
return(1);
case 0x0060:
return(2);
}
return(0);
}
/*
* is_pop()
*/
int
is_pop(unsigned int opcode)
{
switch(opcode) {
case 0x0007:
case 0x0017:
case 0x001f:
case 0x0058:
case 0x0059:
case 0x005a:
case 0x005b:
case 0x005c:
case 0x005d:
case 0x005e:
case 0x005f:
case 0x008f:
case 0x009d:
case 0x0fa1:
case 0x0fa9:
return(1);
case 0x0061:
return(2);
}
return(0);
}
#ifdef REDHAT
#define FRAMESIZE_VALIDATE (0x1)
struct framesize_cache {
kaddr_t pc;
int flags;
int frmsize;
int bp_adjust;
};
#define FRAMESIZE_CACHE (200)
static struct framesize_cache framesize_cache[FRAMESIZE_CACHE] = {{0}};
static struct framesize_cache framesize_cache_empty = {0};
#define FSZ_QUERY (1)
#define FSZ_VALIDATE (2)
#define FSZ_ENTER (3)
#define FRAMESIZE_CACHE_QUERY(pc,szp) cache_framesize(FSZ_QUERY, pc, szp, NULL)
#define FRAMESIZE_CACHE_ENTER(pc,szp) cache_framesize(FSZ_ENTER, pc, szp, NULL)
#define FRAMESIZE_CACHE_VALIDATE(pc,fcpp) cache_framesize(FSZ_VALIDATE, pc, NULL, fcpp)
static int
cache_framesize(int cmd, kaddr_t funcaddr, int *fsize, void **ptr)
{
int i;
static ulong last_cleared = 0;
retry:
for (i = 0; i < FRAMESIZE_CACHE; i++) {
if (framesize_cache[i].pc == funcaddr) {
switch (cmd)
{
case FSZ_VALIDATE:
*ptr = &framesize_cache[i];
return TRUE;
case FSZ_QUERY:
*fsize = framesize_cache[i].frmsize;
return TRUE;
case FSZ_ENTER:
*fsize = framesize_cache[i].frmsize;
return TRUE;
}
}
/*
* The entry does not exist.
*
* If FSZ_QUERY or FSZ_VALIDATE, return their
* no-such-entry indications.
*
* Otherwise, load up the entry with the new data, and
* and modify it with known kludgery.
*/
if (framesize_cache[i].pc == 0) {
switch (cmd)
{
case FSZ_QUERY:
return FALSE;
case FSZ_VALIDATE:
*ptr = &framesize_cache_empty;
return FALSE;
case FSZ_ENTER:
framesize_cache[i].pc = funcaddr;
framesize_cache[i].frmsize = *fsize;
framesize_cache[i].bp_adjust = 0;
framesize_modify(&framesize_cache[i]);
*fsize = framesize_cache[i].frmsize;
return TRUE;
}
}
}
console("framesize_cache is full\n");
/*
* No place to put it, or it doesn't exist.
*/
switch (cmd)
{
case FSZ_VALIDATE:
*ptr = &framesize_cache_empty;
return FALSE;
case FSZ_QUERY:
return FALSE;
case FSZ_ENTER:
BZERO(&framesize_cache[last_cleared % FRAMESIZE_CACHE],
sizeof(struct framesize_cache));
last_cleared++;
goto retry;
}
return FALSE; /* can't get here -- for compiler happiness */
}
/*
* More kludgery for compiler oddities.
*/
#define COMPILER_VERSION_MASK (1) /* deprecated -- usable up to 3.3.3 */
#define COMPILER_VERSION_EQUAL (2)
#define COMPILER_VERSION_START (3)
#define COMPILER_VERSION_RANGE (4)
struct framesize_mods {
char *funcname;
char *called_function;
ulong compiler_flag;
ulong compiler1;
ulong compiler2;
int pre_adjust;
int post_adjust;
} framesize_mods[] = {
{ "do_select", "schedule_timeout",
COMPILER_VERSION_START, GCC(3,3,2), 0, 0, 0 },
{ "svc_recv", "schedule_timeout",
COMPILER_VERSION_START, GCC(3,3,2), 0, 0, 0 },
{ "__down_interruptible", "schedule",
COMPILER_VERSION_START, GCC(3,3,2), 0, 0, 0 },
{ "netconsole_netdump", NULL,
COMPILER_VERSION_START, GCC(3,3,2), 0, 0, -28 },
{ "generic_file_write", NULL,
COMPILER_VERSION_EQUAL, GCC(2,96,0), 0, 0, 20 },
{ "block_prepare_write", NULL,
COMPILER_VERSION_EQUAL, GCC(2,96,0), 0, 0, 72 },
{ "receive_chars", NULL,
COMPILER_VERSION_EQUAL, GCC(2,96,0), 0, 0, 48 },
{ "default_idle", NULL,
COMPILER_VERSION_START, GCC(2,96,0), 0, -4, 0 },
{ "hidinput_hid_event", NULL,
COMPILER_VERSION_START, GCC(4,1,2), 0, 0, 28 },
{ NULL, NULL, 0, 0, 0, 0, 0 },
};
static int
framesize_modify(struct framesize_cache *fc)
{
char *funcname;
struct framesize_mods *fmp;
if (!(funcname = kl_funcname(fc->pc)))
return FALSE;
if (fc->frmsize < 0) {
if (CRASHDEBUG(1))
error(INFO,
"bogus framesize: %d for pc: %lx (%s)\n",
fc->frmsize, fc->pc, funcname);
fc->frmsize = 0;
}
for (fmp = &framesize_mods[0]; fmp->funcname; fmp++) {
if (STREQ(funcname, fmp->funcname) &&
compiler_matches(fmp))
break;
}
if (!fmp->funcname)
return FALSE;
if (fmp->pre_adjust)
fc->frmsize += fmp->pre_adjust;
if (fmp->post_adjust)
fc->bp_adjust = fmp->post_adjust;
if (fmp->called_function) {
if (STREQ(fmp->called_function,x86_function_called_by(fc->pc)))
fc->flags |= FRAMESIZE_VALIDATE;
}
return TRUE;
}
static int
compiler_matches(struct framesize_mods *fmp)
{
switch (fmp->compiler_flag)
{
case COMPILER_VERSION_MASK:
if (fmp->compiler1 & (kt->flags & GCC_VERSION_DEPRECATED))
return TRUE;
break;
case COMPILER_VERSION_EQUAL:
if (THIS_GCC_VERSION == fmp->compiler1)
return TRUE;
break;
case COMPILER_VERSION_START:
if (THIS_GCC_VERSION >= fmp->compiler1)
return TRUE;
break;
case COMPILER_VERSION_RANGE:
if ((THIS_GCC_VERSION >= fmp->compiler1) &&
(THIS_GCC_VERSION <= fmp->compiler2))
return TRUE;
break;
}
return FALSE;
}
static int
dump_framesize_cache(FILE *ofp, struct framesize_cache *fcp)
{
int i, count;
struct syment *sp, *spm;
ulong offset;
int once;
for (i = once = count = 0; i < FRAMESIZE_CACHE; i++) {
if (framesize_cache[i].pc == 0)
break;
count++;
if (fcp && (fcp != &framesize_cache[i]))
continue;
if (!once) {
fprintf(ofp,
"RET ADDR FSZ BPA V FUNCTION\n");
once++;
}
fprintf(ofp, "%8x %4d %4d %s ",
framesize_cache[i].pc,
framesize_cache[i].frmsize,
framesize_cache[i].bp_adjust,
framesize_cache[i].flags & FRAMESIZE_VALIDATE ?
"V" : "-");
if ((sp = value_search(framesize_cache[i].pc, &offset)) ||
(spm = kl_lkup_symaddr(framesize_cache[i].pc))) {
if (sp)
fprintf(ofp, "(%s+", sp->name);
else {
fprintf(ofp, "(%s+", spm->name);
offset = framesize_cache[i].pc - spm->value;
}
switch (pc->output_radix)
{
case 10:
fprintf(ofp, "%ld)", offset);
break;
default:
case 16:
fprintf(ofp, "%lx)", offset);
break;
}
}
fprintf(ofp, "\n");
if (fcp)
return 0;
}
if (!count)
fprintf(ofp, "framesize cache emtpy\n");
if (kt->flags & RA_SEEK)
fprintf(ofp, "RA_SEEK: ON\n");
if (kt->flags & NO_RA_SEEK)
fprintf(ofp, "NO_RA_SEEK: ON\n");
return count;
}
static int
modify_framesize_cache_entry(FILE *ofp, ulong eip, int framesize)
{
int i, found, all_cleared;
for (i = found = all_cleared = 0; i < FRAMESIZE_CACHE; i++) {
if (!eip) {
switch (framesize)
{
case -1:
framesize_cache[i].flags |= FRAMESIZE_VALIDATE;
break;
case -2:
framesize_cache[i].flags &= ~FRAMESIZE_VALIDATE;
break;
default:
framesize_cache[i].pc = 0;
framesize_cache[i].frmsize = 0;
framesize_cache[i].flags = 0;
all_cleared = TRUE;
break;
}
continue;
}
if (framesize_cache[i].pc == 0)
break;
if (framesize_cache[i].pc == eip) {
found++;
switch (framesize)
{
case -1:
framesize_cache[i].flags |= FRAMESIZE_VALIDATE;
break;
case -2:
framesize_cache[i].flags &= ~FRAMESIZE_VALIDATE;
break;
default:
framesize_cache[i].frmsize = framesize;
break;
}
dump_framesize_cache(ofp, &framesize_cache[i]);
return TRUE;
}
}
if (eip && !found)
fprintf(ofp, "eip: %lx not found in framesize cache\n", eip);
if (all_cleared)
fprintf(ofp, "framesize cache cleared\n");
return FALSE;
}
/*
* If eip, look for it and replace its frmsize with the passed-in value.
* If no eip, frmsize of zero means clear the cache, non-zero displays it.
*/
static int
framesize_debug(struct bt_info *bt, FILE *ofp)
{
ulong eip;
int frmsize;
eip = bt->hp->eip;
frmsize = (int)bt->hp->esp;
if (!eip) {
switch (frmsize)
{
case 0:
case -1:
case -2:
return modify_framesize_cache_entry(ofp, 0, frmsize);
default:
return dump_framesize_cache(ofp, NULL);
}
}
return modify_framesize_cache_entry(ofp, eip, frmsize);
}
#endif /* REDHAT */
/*
#define FRMSIZE_DBG 1
#define FRMSIZE2_DBG 1
*/
/*
* get_framesize()
*/
int
#ifdef REDHAT
get_framesize(kaddr_t pc, struct bt_info *bt)
#else
get_framesize(kaddr_t pc)
#endif
{
int size, ret, frmsize = 0;
kaddr_t addr;
instr_rec_t irp;
syment_t *sp;
#ifdef REDHAT
int check_IRQ_stack_switch = 0;
syment_t *jmpsp, *trampsp;
ulong offset;
int frmsize_restore = 0;
int last_add = 0;
if (FRAMESIZE_CACHE_QUERY(pc, &frmsize))
return frmsize;
frmsize = 0;
#endif
if (!(sp = kl_lkup_symaddr(pc))) {
return(0);
}
#ifdef REDHAT
if (STREQ(sp->name, "do_IRQ") && (tt->flags & IRQSTACKS))
check_IRQ_stack_switch++;
if (STREQ(sp->name, "stext_lock") || STRNEQ(sp->name, ".text.lock.")) {
jmpsp = x86_text_lock_jmp(pc, &offset);
if (jmpsp) {
console("get_framesize: stext_lock %lx => %s\n",
pc, jmpsp->name);
pc = jmpsp->value + offset;
sp = jmpsp;
}
}
if ((trampsp = x86_is_entry_tramp_address(pc, &offset))) {
if (STREQ(sp->name, "system_call"))
return 0;
pc = trampsp->value + offset;
}
#endif
#ifdef FRMSIZE_DBG
fprintf(stderr, "get_framesize(): pc=0x%x (0x%x:%s)\n",
pc, sp->s_addr, sp->s_name);
#endif
addr = sp->s_addr;
while (addr <= pc) {
bzero(&irp, sizeof(irp));
irp.aflag = 1;
irp.dflag = 1;
if (!(size = get_instr_info(addr, &irp))) {
fprintf(stderr, "ZERO SIZE!!\n");
return(-1);
}
if (size != irp.size) {
fprintf(stderr, "SIZE DOES NOT MATCH!!\n");
}
#ifdef REDHAT
/*
* Account for do_IRQ() stack switch.
*/
if (check_IRQ_stack_switch && (irp.opcode == 0xff02) &&
(irp.operand[0].op_reg == 0x7))
break;
/*
* Account for embedded "ret" instructions screwing up
* the frame size calculation.
*/
if (irp.opcode == 0xc3) {
frmsize += frmsize_restore;
frmsize_restore = 0;
last_add = FALSE;
} else if ((irp.opcode == 0x8300) &&
(irp.operand[0].op_reg == R_eSP)) {
frmsize_restore += irp.operand[1].op_addr;
last_add = TRUE;
} else if ((irp.opcode == 0x8100) &&
(irp.operand[0].op_reg == R_eSP)) {
frmsize_restore += irp.operand[1].op_addr;
last_add = TRUE;
} else if ((ret = is_pop(irp.opcode))) {
if (ret == 2)
frmsize_restore += (8 * 4);
else
frmsize_restore += 4;
last_add = FALSE;
} else {
if (last_add)
last_add = FALSE;
else
frmsize_restore = 0;
}
#endif /* REDHAT */
#ifdef REDHAT
if ((irp.opcode == 0x8300) || (irp.opcode == 0x8100)) {
#else
if (irp.opcode == 0x8300) {
#endif
/* e.g., addl $0x8,%esp */
if (irp.operand[0].op_reg == R_eSP) {
frmsize -= irp.operand[1].op_addr;
#ifdef FRMSIZE_DBG
fprintf(stderr, " addl --> 0x%x: -%d\n",
addr, irp.operand[1].op_addr);
#endif
}
} else if ((irp.opcode == 0x8305) || (irp.opcode == 0x8105)) {
/* e.g., subl $0x40,%esp */
if (irp.operand[0].op_reg == R_eSP) {
frmsize += irp.operand[1].op_addr;
#ifdef FRMSIZE_DBG
fprintf(stderr, " subl --> 0x%x: +%d\n",
addr, irp.operand[1].op_addr);
#endif
}
} else if ((ret = is_push(irp.opcode))) {
if (ret == 2) {
frmsize += (8 * 4);
#ifdef FRMSIZE_DBG
fprintf(stderr, " pusha --> 0x%x: +%d\n",
addr, (8 * 4));
#endif
} else {
frmsize += 4;
#ifdef FRMSIZE_DBG
fprintf(stderr, " pushl --> 0x%x: +%d\n" ,
addr, 4);
#endif
}
} else if ((ret = is_pop(irp.opcode))) {
if (ret == 2) {
frmsize -= (8 * 4);
#ifdef FRMSIZE_DBG
fprintf(stderr, " popa --> 0x%x: -%d\n",
addr, (8 * 4));
#endif
} else {
frmsize -= 4;
#ifdef FRMSIZE_DBG
fprintf(stderr, " popl --> 0x%x: -%d\n",
addr, 4);
#endif
}
#ifdef FRMSIZE2_DBG
} else {