forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s390dbf.c
1472 lines (1305 loc) · 35.3 KB
/
s390dbf.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
/*
* s390 debug feature command for crash
*
* Copyright (C) IBM Corp. 2006
* Author(s): Michael Holzheu <[email protected]>
*
* 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.
*/
#if defined(S390) || defined(S390X)
#include "defs.h"
#include <iconv.h>
#include <ctype.h>
/*
* Compat layer to integrate lcrash commands into crash
* Maps lcrash API to crash functions
*/
#define KL_NBPW sizeof(long)
#define KL_ERRORFP stderr
#define MAX_ARGS 128
#define MAX_CMDLINE 256
#define C_FALSE 0x00000001 /* Command takes no arguments */
#define C_TRUE 0x00000002 /* Command requires arguments */
#define C_ALL 0x00000004 /* All elements */
#define C_PERM 0x00000008 /* Allocate perminant blocks */
#define C_TEMP 0x00000000 /* For completeness */
#define C_FULL 0x00000010 /* Full output */
#define C_LIST 0x00000020 /* List items */
#define C_NEXT 0x00000040 /* Follow links */
#define C_WRITE 0x00000080 /* Write output to file */
#define C_NO_OPCHECK 0x00000100 /* Don't reject bad cmd line options */
#define C_ITER 0x00000200 /* set iteration threshold */
#define C_LFLG_SHFT 12
#define KL_ARCH_S390 0
#define KL_ARCH_S390X 1
#ifdef __s390x__
#define KL_ARCH KL_ARCH_S390X
#define FMTPTR "l"
#define KL_PTRSZ 8
#else
#define KL_ARCH KL_ARCH_S390
#define FMTPTR "ll"
#define KL_PTRSZ 4
#endif
/* Start TOD time of kernel in usecs for relative time stamps */
static uint64_t tod_clock_base_us;
typedef unsigned long uaddr_t;
typedef unsigned long kaddr_t;
typedef struct _syment {
char *s_name;
kaddr_t s_addr;
} syment_t;
typedef struct option_s {
struct option_s *op_next;
char op_char;
char *op_arg;
} option_t;
typedef struct command_s {
int flags;
char cmdstr[MAX_CMDLINE];
char *command;
char *cmdline;
option_t *options;
int nargs;
char *args[MAX_ARGS];
char *pipe_cmd;
FILE *ofp;
FILE *efp;
} command_t;
static inline syment_t* kl_lkup_symaddr(kaddr_t addr)
{
static syment_t sym;
struct syment *crash_sym;
crash_sym = value_search(addr, &sym.s_addr);
if (!crash_sym)
return NULL;
sym.s_name = crash_sym->name;
return &sym;
}
static inline syment_t* kl_lkup_symname(char* name)
{
static syment_t sym;
sym.s_addr = symbol_value(name);
sym.s_name = NULL;
if(!sym.s_addr)
return NULL;
else
return &sym;
}
static inline void GET_BLOCK(kaddr_t addr, int size, void* ptr)
{
readmem(addr, KVADDR,ptr,size,"GET_BLOCK",FAULT_ON_ERROR);
}
static inline kaddr_t KL_VREAD_PTR(kaddr_t addr)
{
unsigned long ptr;
readmem(addr, KVADDR,&ptr,sizeof(ptr),"GET_BLOCK",FAULT_ON_ERROR);
return (kaddr_t)ptr;
}
static inline uint32_t KL_GET_UINT32(void* ptr)
{
return *((uint32_t*)ptr);
}
static inline uint64_t KL_GET_UINT64(void* ptr)
{
return *((uint64_t*)ptr);
}
static inline kaddr_t KL_GET_PTR(void* ptr)
{
return *((kaddr_t*)ptr);
}
static inline void* K_PTR(void* addr, char* struct_name, char* member_name)
{
return addr+MEMBER_OFFSET(struct_name,member_name);
}
static inline unsigned long KL_ULONG(void* ptr, char* struct_name, char*
member_name)
{
return ULONG(ptr+MEMBER_OFFSET(struct_name,member_name));
}
static inline uint32_t KL_VREAD_UINT32(kaddr_t addr)
{
uint32_t rc;
readmem(addr, KVADDR,&rc,sizeof(rc),"KL_VREAD_UINT32",FAULT_ON_ERROR);
return rc;
}
static inline uint32_t KL_INT(void* ptr, char* struct_name, char* member_name)
{
return UINT(ptr+MEMBER_OFFSET(struct_name,member_name));
}
static inline int set_cmd_flags(command_t *cmd, int flags, char *extraops)
{
return 0;
}
#define USEC_PER_SEC 1000000L
/* Time of day clock value for 1970/01/01 */
#define TOD_UNIX_EPOCH (0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096))
/* Time of day clock value for 1970/01/01 in usecs */
#define TOD_UNIX_EPOCH_US (TOD_UNIX_EPOCH >> 12)
static inline void kl_s390tod_to_timeval(uint64_t todval, struct timeval *xtime)
{
uint64_t todval_us;
/* Convert TOD to usec (51th bit of TOD is us) */
todval_us = todval >> 12;
/* Add base if we have relative time stamps */
todval_us += tod_clock_base_us;
/* Subtract EPOCH that we get time in usec since 1970 */
todval_us -= TOD_UNIX_EPOCH_US;
xtime->tv_sec = todval_us / USEC_PER_SEC;
xtime->tv_usec = todval_us % USEC_PER_SEC;
}
static inline int kl_struct_len(char* struct_name)
{
return STRUCT_SIZE(struct_name);
}
static inline kaddr_t kl_funcaddr(kaddr_t addr)
{
struct syment *crash_sym;
crash_sym = value_search(addr, &addr);
if (!crash_sym)
return -1;
else
return crash_sym->value;
}
#define CMD_USAGE(cmd, s) \
fprintf(cmd->ofp, "Usage: %s %s\n", cmd->command, s); \
fprintf(cmd->ofp, "Enter \"help %s\" for details.\n",cmd->command);
/*
* s390 debug feature implementation
*/
#ifdef DBF_DYNAMIC_VIEWS /* views defined in shared libs */
#include <dlfcn.h>
#endif
/* Local flags
*/
#define LOAD_FLAG (1 << C_LFLG_SHFT)
#define VIEWS_FLAG (2 << C_LFLG_SHFT)
#define SAVE_DBF_FLAG (4 << C_LFLG_SHFT)
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
/* Stuff which has to match with include/asm-s390/debug.h */
#define DBF_VERSION_V1 1
#define DBF_VERSION_V2 2
#define DBF_VERSION_V3 3
#define PAGE_SIZE 4096
#define DEBUG_MAX_VIEWS 10 /* max number of views in proc fs */
#define DEBUG_MAX_PROCF_LEN 64 /* max length for a proc file name */
#define DEBUG_SPRINTF_MAX_ARGS 10
/* define debug-structures for lcrash */
#define DEBUG_DATA(entry) (char*)(entry + 1)
typedef struct debug_view_s debug_view_t;
/*
* struct to hold contents of struct __debug_entry from dump
* for DBF_VERSION_V1 and DBF_VERSION_V2
*/
typedef struct debug_entry_v1_s {
union {
struct {
unsigned long long clock:52;
unsigned long long exception:1;
unsigned long long level:3;
unsigned long long cpuid:8;
} fields;
unsigned long long stck;
} id;
kaddr_t caller; /* changed from void* to kaddr_t */
} __attribute__((packed)) debug_entry_v1_t;
/* for DBF_VERSION_V3 */
typedef struct debug_entry_v3_s {
unsigned long long clock:60;
unsigned long long exception:1;
unsigned long long level:3;
kaddr_t caller; /* changed from void* to kaddr_t */
unsigned short cpuid;
} __attribute__((packed)) debug_entry_v3_t;
static unsigned int dbf_version;
/* struct is used to manage contents of structs debug_info from dump
* in lcrash
*/
typedef struct debug_info_s {
struct debug_info_s *next;
struct debug_info_s *prev;
kaddr_t next_dbi; /* store next ptr of struct in dump */
kaddr_t prev_dbi; /* store prev ptr of struct in dump */
int level;
int nr_areas;
int page_order;
int buf_size;
int entry_size;
void **areas; /* contents of debug areas from dump */
int active_area;
int *active_entry; /* change to uint32_t ? */
debug_view_t *views[DEBUG_MAX_VIEWS];
char name[DEBUG_MAX_PROCF_LEN];
kaddr_t addr;
int pages_per_area_v2;
void ***areas_v2;
} debug_info_t;
/* functions to generate dbf output
*/
typedef int (debug_header_proc_t) (debug_info_t* id, debug_view_t* view,
int area, void* entry,
char* out_buf);
typedef int (debug_format_proc_t) (debug_info_t* id, debug_view_t* view,
char* out_buf, const char* in_buf);
typedef int (debug_prolog_proc_t) (debug_info_t* id, debug_view_t* view,
char* out_buf);
struct debug_view_s {
char name[DEBUG_MAX_PROCF_LEN];
debug_prolog_proc_t* prolog_proc;
debug_header_proc_t* header_proc;
debug_format_proc_t* format_proc;
void* private_data;
};
#define LCRASH_DB_VIEWS 1000
static debug_info_t *debug_area_first = NULL;
static debug_info_t *debug_area_last = NULL;
static debug_view_t *debug_views[LCRASH_DB_VIEWS];
static int initialized = 0;
static iconv_t ebcdic_ascii_conv = 0;
void s390dbf_usage(command_t * cmd);
static int add_lcrash_debug_view(debug_view_t *);
static int dbe_size = 0;
static void
EBCASC(char *inout, size_t len)
{
iconv(ebcdic_ascii_conv, &inout, &len, &inout, &len);
}
/*
* prints header for debug entry
*/
static int
dflt_header_fn(debug_info_t * id, debug_view_t *view,
int area, void *entry, char *out_buf)
{
struct timeval time_val = { 0, 0 };
int rc = 0;
unsigned long long time;
unsigned short level = 0, cpuid = 0;
char *except_str = "-";
kaddr_t caller = 0;
char *caller_name;
int name_width = 26;
int offset;
char caller_buf[30];
syment_t *caller_sym;
switch (dbf_version) {
case DBF_VERSION_V1:
case DBF_VERSION_V2:
level = ((debug_entry_v1_t *) entry)->id.fields.level;
cpuid = ((debug_entry_v1_t *) entry)->id.fields.cpuid;
time = ((debug_entry_v1_t *) entry)->id.stck;
if (((debug_entry_v1_t *) entry)->id.fields.exception)
except_str = "*";
caller = ((debug_entry_v1_t *) entry)->caller;
kl_s390tod_to_timeval(time, &time_val);
break;
case DBF_VERSION_V3:
level = ((debug_entry_v3_t *) entry)->level;
cpuid = ((debug_entry_v3_t *) entry)->cpuid;
time = ((debug_entry_v3_t *) entry)->clock;
if (((debug_entry_v3_t *) entry)->exception)
except_str = "*";
caller = ((debug_entry_v3_t *) entry)->caller;
time_val.tv_sec = time / USEC_PER_SEC;
time_val.tv_usec = time % USEC_PER_SEC;
break;
}
if (KL_ARCH == KL_ARCH_S390)
caller &= 0x7fffffff;
caller_sym = kl_lkup_symaddr(caller);
if (caller_sym) {
caller_name = caller_sym->s_name;
offset = caller - kl_funcaddr(caller);
}
else {
sprintf(caller_buf, "%llx", (unsigned long long)caller);
caller_name = caller_buf;
offset = 0;
}
rc += sprintf(out_buf,
"%02i %011lu:%06lu %1u %1s %04i <%-*s+%04i> ",
area, time_val.tv_sec, time_val.tv_usec, level,
except_str, cpuid,
name_width, caller_name, offset);
return rc;
}
/*
* prints debug data in hex/ascii format
*/
static int
hex_ascii_format_fn(debug_info_t * id, debug_view_t *view,
char *out_buf, const char *in_buf)
{
int i, rc = 0;
if (out_buf == NULL || in_buf == NULL) {
rc = id->buf_size * 4 + 3;
goto out;
}
for (i = 0; i < id->buf_size; i++) {
rc += sprintf(out_buf + rc, "%02x ",
((unsigned char *) in_buf)[i]);
}
rc += sprintf(out_buf + rc, "| ");
for (i = 0; i < id->buf_size; i++) {
unsigned char c = in_buf[i];
if (isascii(c) && isprint(c))
rc += sprintf(out_buf + rc, "%c", c);
else
rc += sprintf(out_buf + rc, ".");
}
rc += sprintf(out_buf + rc, "\n");
out:
return rc;
}
/*
* prints debug data in sprintf format
*/
static int
sprintf_format_fn(debug_info_t * id, debug_view_t *view,
char *out_buf, const char *in_buf)
{
#define _BUFSIZE 1024
char buf[_BUFSIZE];
int i, k, rc = 0, num_longs = 0, num_strings = 0;
int num_used_args ATTRIBUTE_UNUSED;
/* use kaddr_t to store long values of 32bit and 64bit archs here */
kaddr_t inbuf_cpy[DEBUG_SPRINTF_MAX_ARGS];
/* store ptrs to strings to be deallocated at end of this function */
uaddr_t to_dealloc[DEBUG_SPRINTF_MAX_ARGS];
kaddr_t addr;
memset(buf, 0, sizeof(buf));
memset(inbuf_cpy, 0, sizeof(inbuf_cpy));
memset(to_dealloc, 0, sizeof(to_dealloc));
if (out_buf == NULL || in_buf == NULL) {
rc = id->buf_size * 4 + 3;
goto out;
}
/* get the format string into buf */
addr = KL_GET_PTR((void*)in_buf);
GET_BLOCK(addr, _BUFSIZE, buf);
k = 0;
for (i = 0; buf[i] && (buf[i] != '\n'); i++) {
if (buf[i] != '%')
continue;
if (k == DEBUG_SPRINTF_MAX_ARGS) {
fprintf(KL_ERRORFP,
"\nToo much parameters in sprinf view (%i)\n"
,k + 1);
fprintf(KL_ERRORFP, "Format String: %s)\n", buf);
break;
}
/* for sprintf we have only unsigned long values ... */
if (buf[i+1] != 's'){
/* we use KL_GET_PTR here to read ulong value */
addr = KL_GET_PTR((void*) in_buf + ((k + 1)* KL_NBPW));
inbuf_cpy[k] = addr;
} else { /* ... or ptrs to strings in debug areas */
inbuf_cpy[k] = (uaddr_t) malloc(_BUFSIZE);
to_dealloc[num_strings++] = inbuf_cpy[k];
addr = KL_GET_PTR((void*) in_buf + ((k + 1)* KL_NBPW));
GET_BLOCK(addr, _BUFSIZE,
(void*)(uaddr_t)(inbuf_cpy[k]));
}
k++;
}
/* count of longs fit into one entry */
num_longs = id->buf_size / KL_NBPW; /* sizeof(long); */
if(num_longs < 1) /* bufsize of entry too small */
goto out;
if(num_longs == 1) { /* no args, just print the format string */
rc = sprintf(out_buf + rc, "%s", buf);
goto out;
}
/* number of arguments used for sprintf (without the format string) */
num_used_args = MIN(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
rc = sprintf(out_buf + rc, buf, (uaddr_t)(inbuf_cpy[0]),
(uaddr_t)(inbuf_cpy[1]), (uaddr_t)(inbuf_cpy[2]),
(uaddr_t)(inbuf_cpy[3]), (uaddr_t)(inbuf_cpy[4]),
(uaddr_t)(inbuf_cpy[5]), (uaddr_t)(inbuf_cpy[6]),
(uaddr_t)(inbuf_cpy[7]), (uaddr_t)(inbuf_cpy[8]),
(uaddr_t)(inbuf_cpy[9]));
out:
while (num_strings--){
free((char*)(to_dealloc[num_strings]));
}
return rc;
}
/***********************************
* functions for debug-views
***********************************/
/*
* prints out actual debug level
*/
static int
prolog_level_fn(debug_info_t * id,
debug_view_t *view, char *out_buf)
{
int rc = 0;
if (out_buf == NULL) {
rc = 2;
goto out;
}
rc = sprintf(out_buf, "%i\n", id->level);
out:
return rc;
}
/*
* prints out actual pages_per_area
*/
static int
prolog_pages_fn(debug_info_t * id,
debug_view_t *view, char *out_buf)
{
int rc = 0;
if (out_buf == NULL) {
rc = 2;
goto out;
}
rc = sprintf(out_buf, "%i\n", id->pages_per_area_v2);
out:
return rc;
}
/*
* prints out prolog
*/
static int
prolog_fn(debug_info_t * id,
debug_view_t *view, char *out_buf)
{
int rc = 0;
rc = sprintf(out_buf, "AREA TIME LEVEL EXCEPTION CP CALLING FUNCTION"
" + OFFSET DATA\n==============================="
"===========================================\n");
return rc;
}
/*
* prints debug data in hex format
*/
static int
hex_format_fn(debug_info_t * id, debug_view_t *view,
char *out_buf, const char *in_buf)
{
int i, rc = 0;
for (i = 0; i < id->buf_size; i++) {
rc += sprintf(out_buf + rc, "%02x ",
((unsigned char *) in_buf)[i]);
}
rc += sprintf(out_buf + rc, "\n");
return rc;
}
/*
* prints debug data in ascii format
*/
static int
ascii_format_fn(debug_info_t * id, debug_view_t *view,
char *out_buf, const char *in_buf)
{
int i, rc = 0;
if (out_buf == NULL || in_buf == NULL) {
rc = id->buf_size + 1;
goto out;
}
for (i = 0; i < id->buf_size; i++) {
unsigned char c = in_buf[i];
if (!isprint(c))
rc += sprintf(out_buf + rc, ".");
else
rc += sprintf(out_buf + rc, "%c", c);
}
rc += sprintf(out_buf + rc, "\n");
out:
return rc;
}
/*
* prints debug data in ebcdic format
*/
static int
ebcdic_format_fn(debug_info_t * id, debug_view_t *view,
char *out_buf, const char *in_buf)
{
int i, rc = 0;
if (out_buf == NULL || in_buf == NULL) {
rc = id->buf_size + 1;
goto out;
}
for (i = 0; i < id->buf_size; i++) {
char c = in_buf[i];
EBCASC(&c, 1);
if (!isprint(c))
rc += sprintf(out_buf + rc, ".");
else
rc += sprintf(out_buf + rc, "%c", c);
}
rc += sprintf(out_buf + rc, "\n");
out:
return rc;
}
debug_view_t ascii_view = {
"ascii",
&prolog_fn,
&dflt_header_fn,
&ascii_format_fn,
};
debug_view_t ebcdic_view = {
"ebcdic",
&prolog_fn,
&dflt_header_fn,
&ebcdic_format_fn,
};
debug_view_t hex_view = {
"hex",
&prolog_fn,
&dflt_header_fn,
&hex_format_fn,
};
debug_view_t level_view = {
"level",
&prolog_level_fn,
NULL,
NULL,
};
debug_view_t pages_view = {
"pages",
&prolog_pages_fn,
NULL,
NULL,
};
debug_view_t hex_ascii_view = {
"hex_ascii",
&prolog_fn,
&dflt_header_fn,
&hex_ascii_format_fn,
};
debug_view_t sprintf_view = {
"sprintf",
&prolog_fn,
&dflt_header_fn,
&sprintf_format_fn,
};
static debug_entry_v1_t *
debug_find_oldest_entry(debug_entry_v1_t *entries, int num, int entry_size)
{
debug_entry_v1_t *result, *current;
int i;
uint64_t clock1, clock2;
result = entries;
current = entries;
for (i=0; i < num; i++) {
if (current->id.stck == 0)
break;
clock1 = current->id.fields.clock;
clock2 = result->id.fields.clock;
clock1 = KL_GET_UINT64(&clock1);
clock2 = KL_GET_UINT64(&clock2);
if (clock1 < clock2)
result = current;
current = (debug_entry_v1_t *) ((char *) current + entry_size);
}
return result;
}
/*
* debug_format_output:
* - calls prolog, header and format functions of view to format output
*/
static int
debug_format_output_v1(debug_info_t * debug_area, debug_view_t *view,
FILE * ofp)
{
int i, j, len;
int nr_of_entries;
debug_entry_v1_t *act_entry, *last_entry;
char *act_entry_data;
char buf[2048];
size_t items ATTRIBUTE_UNUSED;
/* print prolog */
if (view->prolog_proc) {
len = view->prolog_proc(debug_area, view, buf);
items = fwrite(buf,len, 1, ofp);
memset(buf, 0, 2048);
}
/* print debug records */
if (!(view->format_proc) && !(view->header_proc))
goto out;
if(debug_area->entry_size <= 0){
fprintf(ofp, "Invalid entry_size: %i\n",debug_area->entry_size);
goto out;
}
nr_of_entries = (PAGE_SIZE << debug_area->page_order) / debug_area->entry_size;
for (i = 0; i < debug_area->nr_areas; i++) {
act_entry = debug_find_oldest_entry(debug_area->areas[i],
nr_of_entries,
debug_area->entry_size);
last_entry = (debug_entry_v1_t *) ((char *) debug_area->areas[i] +
(PAGE_SIZE << debug_area->page_order) -
debug_area->entry_size);
for (j = 0; j < nr_of_entries; j++) {
act_entry_data = (char*)act_entry + dbe_size;
if (act_entry->id.stck == 0)
break; /* empty entry */
if (view->header_proc) {
len = view->header_proc(debug_area, view, i,
act_entry, buf);
items = fwrite(buf,len, 1, ofp);
memset(buf, 0, 2048);
}
if (view->format_proc) {
len = view->format_proc(debug_area, view,
buf, act_entry_data);
items = fwrite(buf,len, 1, ofp);
memset(buf, 0, 2048);
}
act_entry =
(debug_entry_v1_t *) (((char *) act_entry) +
debug_area->entry_size);
if (act_entry > last_entry)
act_entry = debug_area->areas[i];
}
}
out:
return 1;
}
/*
* debug_format_output_v2:
* - calls prolog, header and format functions of view to format output
*/
static int
debug_format_output_v2(debug_info_t * debug_area,
debug_view_t *view, FILE * ofp)
{
int i, j, k, len;
void *act_entry;
char *act_entry_data;
char buf[2048];
size_t items ATTRIBUTE_UNUSED;
/* print prolog */
if (view->prolog_proc) {
len = view->prolog_proc(debug_area, view, buf);
items = fwrite(buf,len, 1, ofp);
memset(buf, 0, 2048);
}
/* print debug records */
if (!(view->format_proc) && !(view->header_proc))
goto out;
if(debug_area->entry_size <= 0){
fprintf(ofp, "Invalid entry_size: %i\n",debug_area->entry_size);
goto out;
}
for (i = 0; i < debug_area->nr_areas; i++) {
int nr_entries_per_page = PAGE_SIZE/debug_area->entry_size;
for (j = 0; j < debug_area->pages_per_area_v2; j++) {
act_entry = debug_area->areas_v2[i][j];
for (k = 0; k < nr_entries_per_page; k++) {
act_entry_data = (char*)act_entry + dbe_size;
if (dbf_version == DBF_VERSION_V3 &&
((debug_entry_v3_t *) act_entry)->clock == 0)
break; /* empty entry */
else if (dbf_version < DBF_VERSION_V3 &&
((debug_entry_v1_t *) act_entry)->id.stck == 0)
break; /* empty entry */
if (view->header_proc) {
len = view->header_proc(debug_area,
view, i, act_entry, buf);
items = fwrite(buf,len, 1, ofp);
memset(buf, 0, 2048);
}
if (view->format_proc) {
len = view->format_proc(debug_area,
view, buf, act_entry_data);
items = fwrite(buf,len, 1, ofp);
memset(buf, 0, 2048);
}
act_entry = ((char *) act_entry) +
debug_area->entry_size;
}
}
}
out:
return 1;
}
static debug_info_t *
find_debug_area(const char *area_name)
{
debug_info_t* act_debug_info = debug_area_first;
while(act_debug_info != NULL){
if (strcmp(act_debug_info->name, area_name) == 0)
return act_debug_info;
act_debug_info = act_debug_info->next;
}
return NULL;
}
static void tod_clock_base_init(void)
{
if (kernel_symbol_exists("tod_clock_base")) {
/*
* Kernels >= 4.14 that contain 6e2ef5e4f6cc5734 ("s390/time:
* add support for the TOD clock epoch extension")
*/
get_symbol_data("tod_clock_base", sizeof(tod_clock_base_us),
&tod_clock_base_us);
/* Bit for usecs is at position 59 - therefore shift 4 */
tod_clock_base_us >>= 4;
} else if (kernel_symbol_exists("sched_clock_base_cc") &&
!kernel_symbol_exists("tod_to_timeval")) {
/*
* Kernels >= 4.11 that contain ea417aa8a38bc7db ("s390/debug:
* make debug event time stamps relative to the boot TOD clock")
*/
get_symbol_data("sched_clock_base_cc",
sizeof(tod_clock_base_us), &tod_clock_base_us);
/* Bit for usecs is at position 51 - therefore shift 12 */
tod_clock_base_us >>= 12;
} else {
/* All older kernels use absolute time stamps */
tod_clock_base_us = 0;
}
}
static void
dbf_init(void)
{
if (!initialized) {
tod_clock_base_init();
if(dbf_version >= DBF_VERSION_V2)
add_lcrash_debug_view(&pages_view);
add_lcrash_debug_view(&ascii_view);
add_lcrash_debug_view(&level_view);
add_lcrash_debug_view(&ebcdic_view);
add_lcrash_debug_view(&hex_view);
add_lcrash_debug_view(&hex_ascii_view);
add_lcrash_debug_view(&sprintf_view);
ebcdic_ascii_conv = iconv_open("ISO-8859-1", "EBCDIC-US");
initialized = 1;
}
}
static debug_view_t*
get_debug_view(kaddr_t addr)
{
void* k_debug_view;
int k_debug_view_size;
debug_view_t* rc;
rc = (debug_view_t*)malloc(sizeof(debug_view_t));
memset(rc, 0, sizeof(debug_view_t));
k_debug_view_size = kl_struct_len("debug_view");
k_debug_view = malloc(k_debug_view_size);
GET_BLOCK(addr, k_debug_view_size, k_debug_view);
strncpy(rc->name,K_PTR(k_debug_view,"debug_view","name"),
DEBUG_MAX_PROCF_LEN);
free(k_debug_view);
return rc;
}
static void
free_debug_view(debug_view_t* view)
{
if(view)
free(view);
}
static void
debug_get_areas_v1(debug_info_t* db_info, void* k_dbi)
{
kaddr_t mem_pos;
kaddr_t dbe_addr;
int area_size, i;
/* get areas */
/* place to hold ptrs to debug areas in lcrash */
area_size = PAGE_SIZE << db_info->page_order;
db_info->areas = (void**)malloc(db_info->nr_areas * sizeof(void *));
memset(db_info->areas, 0, db_info->nr_areas * sizeof(void *));
mem_pos = KL_ULONG(k_dbi,"debug_info","areas");
for (i = 0; i < db_info->nr_areas; i++) {
dbe_addr = KL_VREAD_PTR(mem_pos);
db_info->areas[i] = (debug_entry_v1_t *) malloc(area_size);
/* read raw data for debug area */
GET_BLOCK(dbe_addr, area_size, db_info->areas[i]);
mem_pos += KL_NBPW;
}
}
static void
debug_get_areas_v2(debug_info_t* db_info, void* k_dbi)
{
kaddr_t area_ptr;
kaddr_t page_array_ptr;
kaddr_t page_ptr;
int i,j;
db_info->areas_v2=(void***)malloc(db_info->nr_areas * sizeof(void **));
area_ptr = KL_ULONG(k_dbi,"debug_info","areas");
for (i = 0; i < db_info->nr_areas; i++) {
db_info->areas_v2[i] = (void**)malloc(db_info->pages_per_area_v2
* sizeof(void*));
page_array_ptr = KL_VREAD_PTR(area_ptr);
for(j=0; j < db_info->pages_per_area_v2; j++) {
page_ptr = KL_VREAD_PTR(page_array_ptr);
db_info->areas_v2[i][j] = (void*)malloc(PAGE_SIZE);
/* read raw data for debug area */
GET_BLOCK(page_ptr, PAGE_SIZE, db_info->areas_v2[i][j]);
page_array_ptr += KL_NBPW;
}
area_ptr += KL_NBPW;
}
}
static debug_info_t*
get_debug_info(kaddr_t addr,int get_areas)
{
void *k_dbi;
kaddr_t mem_pos;
kaddr_t view_addr;
debug_info_t* db_info;
int i;
int dbi_size;
/* get sizes of kernel structures */
if(!(dbi_size = kl_struct_len("debug_info"))){
fprintf (KL_ERRORFP,
"Could not determine sizeof(struct debug_info)\n");
return(NULL);
}
if(!(dbe_size = kl_struct_len("__debug_entry"))){
fprintf(KL_ERRORFP,
"Could not determine sizeof(struct __debug_entry)\n");
return(NULL);
}
/* get kernel debug_info structure */
k_dbi = malloc(dbi_size);
GET_BLOCK(addr, dbi_size, k_dbi);
db_info = (debug_info_t*)malloc(sizeof(debug_info_t));
memset(db_info, 0, sizeof(debug_info_t));
/* copy members */
db_info->level = KL_INT(k_dbi,"debug_info","level");
db_info->nr_areas = KL_INT(k_dbi,"debug_info","nr_areas");
db_info->pages_per_area_v2= KL_INT(k_dbi,"debug_info","pages_per_area");
db_info->page_order = KL_INT(k_dbi,"debug_info","page_order");
db_info->buf_size = KL_INT(k_dbi,"debug_info","buf_size");
db_info->entry_size = KL_INT(k_dbi,"debug_info","entry_size");
db_info->next_dbi = KL_ULONG(k_dbi,"debug_info","next");
db_info->prev_dbi = KL_ULONG(k_dbi,"debug_info","prev");
db_info->addr = addr;
strncpy(db_info->name,K_PTR(k_dbi,"debug_info","name"),
DEBUG_MAX_PROCF_LEN);
if(get_areas){
if(dbf_version == DBF_VERSION_V1)