[go: up one dir, main page]

File: sclpdbf

package info (click to toggle)
s390-tools 2.35.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,220 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (1172 lines) | stat: -rwxr-xr-x 23,849 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
#!/bin/bash
#
# sclpdbf - Tool to display sclp kernel traces
#
# Copyright IBM Corp. 2021
#
# Usage: sclpdbf [TRACEFILE] [OPTIONS]
#
# Display the contents of the s390dbf kernel trace area for the sclp component
# in a human readable format.
#
# TRACEFILE may specify the name of a file containing the trace data in
# hex_ascii view format. If no TRACEFILE is specified, trace data of the
# currently running kernel is read from its default location at:
#
#   /sys/kernel/debug/s390dbf/sclp/hex_ascii      (default source)
#   /sys/kernel/debug/s390dbf/sclp_err/hex_ascii  (if -e is specified)
#
# OPTIONS
#   -h, --help            Print this help, then exit
#       --version         Print version information, then exit
#   -v, --verbose         Display verbose trace output
#   -e, --errlog          Display sclp error log instead of normal log
#   -r, --raw             Display log in raw format
#   -s, --symtab SYMFILE  Use symbol table in SYMFILE (default /proc/kallsyms)
#   -S, --no-symtab       Do not resolve kernel addresses to symbol names
#   -f, --flush           Flush current trace buffers
#   -l, --level LEVEL     Set tracing level to LEVEL (-1=off, 0 to 6)
#   -p, --pages PAGES     Set trace area size to PAGES
#   -P, --no-pager        Do not pipe trace output into pager

TOOLPATH="$(readlink -f "${BASH_SOURCE[0]}")"
TOOLNAME="${0##*/}"

DEBUGFS="/sys/kernel/debug"

# Enable color usage and pager when writing to terminal
if [[ -t 1 ]] ; then
	BOLD="\033[1m"
	RED="\033[7;31m"
	GREEN="\033[1;32m"
	BLUE="\033[1;34m"
	YELLOW="\033[33m"
	RESET="\033[0m"

	PAGER="less -r"
else
	PAGER="cat"
fi

# List of known trace entry IDs
declare -A IDS=(
	["SRV1"]="handle_SRV1"
	["SRV2"]="handle_SRV2"
	["INT"]="handle_INT"
	["UNEX"]="handle_UNEX"
	["RQAD"]="handle_RQAD"
	["RQOK"]="handle_RQOK"
	["RQAB"]="handle_RQAB"
	["RQTM"]="handle_RQTM"
	["EVNT"]="handle_EVNT"
	["STCG"]="handle_STCG"
	["REG"]="handle_REG"
	["UREG"]="handle_UREG"
	["TMO"]="handle_TMO"
	["SYN1"]="handle_SYN1"
	["SYN2"]="handle_SYN2"
)

# Symbolic names for SCLP request status
declare -a REQSTATUS=(
	"FILLED"
	"QUEUED"
	"RUNNING"
	"DONE"
	"${RED}FAILED${RESET}"
	"${RED}QUEUED_TIMEOUT${RESET}"
)

# Symbolic names for SCLP command words
declare -A CMDWS=(
	[0x00010001]="READ_CPU_INFO"
	[0x00020001]="READ_SCP_INFO"
	[0x00040001]="READ_STORAGE_INFO"
	[0x00120001]="READ_SCP_INFO_FORCED"
	[0x00770005]="READ_EVENT_DATA"
	[0x00760005]="WRITE_EVENT_DATA"
	[0x00780005]="WRITE_EVENT_MASK"
)

# Symbolic names for RC values
declare -a RC=(
	[0]="OK"
	[5]="${RED}EIO${RESET}"
	[16]="${RED}EBUSY${RESET}"
	[22]="${RED}EINVAL${RESET}"
)

# Colors to be used for hex dump display
FIELDCOLS=(
	[0]=""
	[1]="$BLUE"
	[2]="$GREEN"
)

# Color index for hex bytes in SCCB hex dump display
SCCBFIELDS="1 1 2 2 2 2 1 1"

# Color index for hex bytes in event buffer hex dump display
EVNTFIELDS="1 1 2 1"

# ASCII -> printable ASCII mapping table
ASCII='................................ !"#$%&'\''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~.................................................................................................................................'

# EBCDIC 500 -> printable ASCII mapping table
EBCDIC='................................................................ .........[.<(+!&.........]$*);^-/.........,%_>?........p`:#@'\''=".abcdefghi.......jklmnopqr.......~stuvwxyz.................|....{ABCDEFGHI......}JKLMNOPQR......\.STUVWXYZ......0123456789......'


function print_help() {
	local line started=0

	while read -r line ; do
		[[ $started -eq 0 && $line =~ Usage: ]] && started=1
		[[ $started -eq 1 && $line =~ ^$ ]] && break
		[[ $started -eq 1 ]] && echo "${line:2}"
	done < "$TOOLPATH"
}

function print_version() {
	local line

	echo "${TOOLNAME}: version %S390_TOOLS_VERSION%"
	while read -r line ; do
		[[ ! $line =~ Copyright ]] && continue
		echo "${line:2}"
		break
	done < "$TOOLPATH"
}

#
# die MSG
#
# Terminate with the specified error message and a non-zero exit code.
#
function die() {
	local msg="$*"

	echo "Error: $msg" >&2
	exit 1
}

#
# indent NUM
#
# Indent all data read from stdin by NUM spaces.
#
function indent() {
	local num=$1 spaces

	printf -v spaces "%*s" "$num" ""
	sed -e "s/^/$spaces/g"
}

#
# repeat STR NUM
#
# Print STR for NUM times.
#
function repeat() {
	local str="$1" num="$2"

	eval "printf '$str%.0s' {1..$num}"
}

# Trace file may be wrapped (4 5 6 1 2 3) - ensure ascending timestamp order
function sort_file() {
	local filename="$1"
	local before=() after=() area t data t_start line

	{
		read -r area t_start data
		after+=("$area $t_start $data")

		while read -r area t data ; do
			# Look for wrap in time sequence
			if [[ "$t" < "$t_start" ]] ; then
				before+=("$area $t $data")
				break
			fi
			after+=("$area $t $data")
		done

		while read -r area t data ; do
			before+=("$area $t $data")
		done
	} <"$filename"

	if [[ ${#before[@]} -gt 0 ]] ; then
		printf "%s\n" "${before[@]}"
	fi

	if [[ -n "$t_start" ]] ; then
		printf "%s\n" "${after[@]}"
	fi
}

function check_tracefile() {
	local file="$1" area ts level ex cpu caller data

	if [[ ! -e "$file" ]] ; then
		die "Could not find trace data file $file"
	fi

	if [[ ! -r "$file" ]] ; then
		die "Missing access permissions for $file"
	fi

	read -r area ts level ex cpu caller data <"$file"

	if [[ -z "$area" ]] ; then
		echo "No traces found in $file"
		exit 0
	fi

	if [[ -z "$ts" ]] || [[ -z "$level" ]] || [[ -z "$ex" ]] ||
	   [[ -z "$cpu" ]] || [[ -z "$caller" ]] || [[ -z "$data" ]] ; then
		die "Unrecognized trace data format in $file"
	fi
}

#
# print_ascii HEX
#
# Convert ASCII bytes in hexadecimal notation to printable ASCII.
#
function print_ascii() {
	local hex="$1" asc

	printf -v asc '${ASCII:0x%s:1}' $hex
	eval echo -n "$asc"
}

#
# print_ebcdic HEX
#
# Convert EBCDIC bytes in hexadecimal notation to printable ASCII.
#
function print_ebcdic() {
	local hex="$1" ebc

	printf -v ebc '${EBCDIC:0x%s:1}' $hex
	eval echo -n "$ebc"
}

#
# hex_to_num HEX START LEN CONV VAR
#
# Convert hex bytes in HEX starting at position START with length LEN to
# an integer. The result will be stored in VAR as hexadecimal number if CONV
# is 0, decimal if CONV is 1.
#
function hex_to_num() {
	local _hex="$1" _start="$2" _len="$3" _conv="$4" _var="$5" _value _fmt

	# Remove spaces
	_hex="${_hex// /}"

	# Extract relevant hex digits as decimal number
	(( _start*=2 ))
	(( _len*=2 ))
	_value="0x${_hex:$_start:$_len}"
	(( _value=_value ))

	# Store as requested output format
	if [[ "$_conv" -eq 1 ]] ; then
		_fmt="%d"
	else
		_fmt="0x%x"
	fi

	printf -v "$_var" "$_fmt" "$_value"
}

#
# is_id TAG
#
# Return 0 if TAG is a known trace tag, non-zero otherwise.
#
function is_id() {
	local tag="$1"

	[[ -z "$tag" ]] && return 1
	[[ "$tag" =~ [^A-Z0-9] ]] && return 1
	[[ -n "${IDS[$tag]}" ]]
}

#
# reqstatus NUM
#
# Convert the specified SCLP request status to a named representation.
#
function reqstatus() {
	local num="$1" _var="$2" txt

	[[ -n "$_var" ]] && _var="-v $_var"

	txt="${REQSTATUS[$num]}"

	eval "printf $_var '${txt:-Unknown} ($num)'"
}

#
# cmdwname CMDW
#
# Convert the specified SCLP command word to a symbolic name.
#
function cmdwname() {
	local _cmdw="$1" _var="$2" c

	[[ -n "$_var" ]] && _var="-v $_var"
	printf -v c "0x%08x" $(( _cmdw ))

	if [[ -n "${CMDWS[$c]}" ]] ; then
		if [[ $OPT_VERBOSE -eq 1 ]] ; then
			_cmdw="${CMDWS[$c]} ($_cmdw)"
		else
			_cmdw="${CMDWS[$c]}"
		fi
	fi

	eval "printf $_var \"$_cmdw\""
}

#
# rcname RC
#
# Convert the specified return code to a symbolic name.
#
function rcname() {
	local _rc="$1" _var="$2"

	if [[ -n "${RC[$_rc]}" ]] ; then
		if [[ $OPT_VERBOSE -eq 1 ]] ; then
			_rc="${RC[$_rc]} ($_rc)"
		else
			_rc="${RC[$_rc]}"
		fi
	fi

	printf -v "$_var" "$_rc"
}

#
# respcol RESP
#
# Add color to the specified SCCB response code
#
function respcol() {
	local _resp="$1" _var="$2" _c

	_c=$(( _resp&0xff ))
	if [[ $_c -ne 0x10 ]] && [[ $_c -ne 0x20 ]] && [[ $_c -ne 0 ]] ; then
		_resp="$RED$_resp$RESET"
	fi

	printf -v "$_var" "$_resp"
}

#
# lencol LEN
#
# Add color to the specified SCCB length
#
function lencol() {
	local _len="$1" _var="$2"

	if [[ $_len -lt 8 ]] ; then
		_len="$RED$_len$RESET"
	fi

	printf -v "$_var" "$_len"
}

#
# sccbcol SCCB
#
# Add color to the specified SCCB address
#
function sccbcol() {
	local _sccb="$1" _var="$2"

	if [[ $(( _sccb&0x7 )) -ne 0 ]] ; then
		_sccb="$RED$_sccb$RESET"
	fi

	printf -v "$_var" "$_sccb"
}

#
# bitlist NUM
#
# Print list of bit numbers set in the 64 bit word specified by NUM.
#
function bitlist() {
	local num="$1" _var="$2" list="" i c="" w=64

	i=1
	while [[ $i -lt $w ]] ; do
		if [[ $(( num&1<<(w-i) )) -ne 0 ]] ; then
			list="$list$c$i"
			c=','
		fi
		(( i++ ))
	done

	[[ -z "$list" ]] && list="-"

	eval "$_var='$list'"
}


function read_symtab() {
	local filename="$1" symtab=() i

	echo "Reading symbol table from $OPT_SYMTAB"
	readarray symtab <"$filename"

	for i in "${symtab[@]}" ; do
		set -- $i
		(( i=0x$1 ))
		SYMS[$i]="$3"
	done

	echo "Found ${#SYMS[@]} symbols"
}

#
# get_sym ADDR
#
# Convert the specified kernel address to a symbolic name.
#
function get_sym() {
	local _addr="$1" _var="$2" from to i

	[[ -n "$_var" ]] && _var="-v $_var"

	_addr=0x${_addr#0x}

	if [[ $OPT_NOSYM -eq 1 ]] ; then
		eval printf $_var "0x%x" $_addr
		return
	fi

	from=$_addr
	(( to=from-0x10000 ))
	[[ $to -lt 1 ]] && to=1

	i=$from
	while [[ $i -ge $to ]] ; do
		if [[ -z "${SYMS[$i]}" ]] ; then
			(( i-- ))
			continue
		fi

		if [[ $_addr -eq $i ]] ; then
			eval printf $_var "${SYMS[$i]}"
		else
			eval printf $_var "%s+0x%x" "${SYMS[$i]}" $(( _addr-$i ))
		fi
		return
	done

	eval printf $_var "0x%x" $_addr
}

# Print hex data in alternating colors to help visual grouping
function hex_color() {
	local hex="$1" delim="$2" fields=($3) i byte currcol lastcol

	lastcol=0
	i=0
	for byte in $hex ; do
		currcol="${fields[$i]}"
		[[ -z "$currcol" ]] && currcol=0

		if [[ "$currcol" -ne "$lastcol" ]] ; then
			echo -en "$RESET${FIELDCOLS[$currcol]}"
		fi
		echo -n "$byte$delim"

		lastcol="$currcol"
		(( i++ ))
	done
	echo -ne "$RESET"
}

function handle_buffer() {
	local name="$1" fields="$2" hex=("${@:3}") offset=0 v size

	[[ ${#hex[@]} -eq 0 ]] && return

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		for v in "${hex[@]}" ; do
			hex_color "$v" "" "$fields"
			echo -n "  "
			print_ascii "$v"
			echo -n "  "
			print_ebcdic "$v"
			echo
			fields=""
		done
		return
	fi

	[[ -n "$name" ]] && printf "\n%s:\n" "$name"
	hex_to_num "${hex[0]}" 0 2 1 size

	printf "$BOLD%-4s  %-48s %-16s  %-16s$RESET\n" "OFF" "HEX" "ASCII" "EBCDIC"

	for v in "${hex[@]}" ; do
		printf "%04x: " "$offset"
		hex_color "$v" " " "$fields"
		echo -n " "
		print_ascii "$v"
		echo -n "  "
		print_ebcdic "$v"
		echo
		fields=""

		(( offset+=16 ))
	done

	if [[ -n "$size" ]] && [[ $offset -lt $size ]] ; then
		(( size-=offset ))
		echo "Truncated $size bytes"
	fi
}

function short() {
	set -- $*
	echo -en "$BOLD$1$RESET"
	printf " %s" "${@:2}"
}

function long() {
	echo -e "$BOLD$*$RESET"
}

function handle_SRV1() {
	local hex1="$1" hex2=("${@:2}") cmdw sccb len response

	hex_to_num "$hex1" 4 4 0 cmdw
	cmdwname "$cmdw" cmdw
	hex_to_num "$hex1" 8 8 0 sccb
	sccbcol "$sccb" sccb

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "SERVC cmd=$cmdw sccb=$sccb"
		echo
		handle_buffer "SCCB contents" "$SCCBFIELDS" "${hex2[@]}" |
			indent 4
		return
	fi

	hex_to_num "${hex2[0]}" 0 2 1 len
	lencol "$len" len
	hex_to_num "${hex2[0]}" 6 2 0 response
	respcol "$response" response

	long "Service call about to be issued"
	echo "  SCLP command ....: $cmdw"
	echo -e "  SCCB address ....: $sccb"
	echo -e "  SCCB length .....: $len"
	echo -e "  SCCB response ...: $response"

	handle_buffer "SCCB contents" "$SCCBFIELDS" "${hex2[@]}" | indent 2
}

function handle_SRV2() {
	local hex="$1" rc seq

	hex_to_num "$hex" 4 4 1 rc
	rcname "$rc" rc

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "SERVC rc=$rc"
		echo
		return
	fi

	hex_to_num "$hex" 8 8 1 seq

	long "Service call completed"
	echo -e "  Result code ...........: $rc"
	echo "  SRVC sequence number ..: $seq"
}

function handle_INT() {
	local hex1="$1" hex2=("${@:2}") parm32 pending sccb cmdw len response

	hex_to_num "$hex1" 4 4 1 parm32
	(( pending=parm32 & 0x3 ))
	(( sccb=parm32 & 0xfffffff8 ))
	printf -v sccb "0x%x" "$sccb"
	sccbcol "$sccb" sccb
	hex_to_num "$hex1" 12 4 0 cmdw
	cmdwname "$cmdw" cmdw
	hex_to_num "${hex2[0]}" 6 2 0 response
	respcol "$response" response

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "INT pend=$pending sccb=$sccb resp=$response"
		echo
		handle_buffer "SCCB contents" "$SCCBFIELDS" "${hex2[@]}" |
			indent 4
		return
	fi

	hex_to_num "${hex2[0]}" 0 2 1 len
	lencol "$len" len

	long "Interrupt received"
	echo "  Event pending indicator ..: $pending"
	echo "  Active command ...........: $cmdw"
	echo -e "  SCCB address .............: $sccb"
	echo -e "  SCCB length ..............: $len"
	echo -e "  SCCB response ............: $response"

	handle_buffer "SCCB contents" "$SCCBFIELDS" "${hex2[@]}" | indent 2
}

function handle_UNEX() {
	local hex="$1" sccb

	hex_to_num "$hex" 4 4 0 sccb
	sccbcol "$sccb" sccb

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "${RESET}${RED}UNEXPECTED${RESET} sccb=$sccb"
		echo
		return
	fi

	long "${RESET}${RED}Unexpected SCCB completion${RESET}"
	echo "  SCCB address ...: $sccb"
}

function handle_req() {
	local hex="$1"
	local sccb status response timeout start_count

	hex_to_num "$hex" 4 4 0 sccb
	sccbcol "$sccb" sccb

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		echo -n "sccb=$sccb"
		return
	fi

	hex_to_num "$hex" 8 2 1 status
	reqstatus "$status" status
	hex_to_num "$hex" 10 2 0 response
	respcol "$response" response
	hex_to_num "$hex" 12 2 1 timeout
	hex_to_num "$hex" 14 2 1 start_count

	echo -e "Request status ........: $status"
	echo "Request timeout .......: $timeout"
	echo "Request start count ...: $start_count"
	echo -e "SCCB address ..........: $sccb"
	echo -e "SCCB response .........: $response"
}

function handle_RQAD() {
	local hex="$1" sccb caller

	hex_to_num "$hex" 4 4 0 sccb
	sccbcol "$sccb" sccb
	hex_to_num "$hex" 8 8 0 caller
	get_sym "$caller" caller

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "RQADD caller=$caller"
		echo
		return
	fi

	long "Request is added"
	echo -e "  SCCB address...........: $sccb"
	echo "  Caller ................: $caller"
}

function handle_RQOK() {
	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "RQOK "
		handle_req "$1"
		echo
		return
	fi

	long "Request completed successfully"

	handle_req "$1" | indent 2
}

function handle_RQAB() {
	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "${RESET}${RED}RQABORT${RESET} "
		handle_req "$1"
		echo
		return
	fi

	long "${RESET}${RED}Request was aborted${RESET}"

	handle_req "$1" | indent 2
}

function handle_RQTM() {
	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "${RESET}${RED}RQTIMEOUT${RESET} "
		handle_req "$1"
		echo
		return
	fi

	long "${RESET}${RED}Request timed out${RESET}"

	handle_req "$1" | indent 2
}

function handle_EVNT() {
	local hex="$1" hex2=("${@:2}") cb len type

	hex_to_num "$hex" 8 8 0 cb
	get_sym "$cb" cb

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "EVENT callback=$cb"
		echo
		handle_buffer "Event buffer contents" "$EVNTFIELDS" \
			"${hex2[@]}" | indent 4
		return
	fi

	hex_to_num "${hex2[0]}" 0 2 1 len
	hex_to_num "${hex2[0]}" 2 1 1 type

	long "Dispatching event buffer"
	echo "  Receiving callback ......: $cb"
	echo "  Event buffer length .....: $len"
	echo "  Event type ..............: $type"

	handle_buffer "Event buffer contents" "$EVNTFIELDS" "${hex2[@]}" |
		indent 2
}

function handle_STCG() {
	local hex="$1" cb

	hex_to_num "$hex" 8 8 0 cb
	get_sym $cb cb

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "STATECHANGE callback=$cb"
		echo
		return
	fi

	long "Dispatching state change"
	echo "  Receiving callback ...: $cb"
}

function handle_register() {
	local hex="$1" receive send

	hex_to_num "$hex" 0 8 0 receive
	bitlist "$receive" receive
	hex_to_num "$hex" 8 8 0 send
	bitlist "$send" send

	echo "Receive mask ...: $receive"
	echo "Send mask ......: $send"
}

function handle_REG() {
	local hex1="$1" hex2="$2" retip

	hex_to_num "$hex1" 8 8 0 retip
	get_sym "$retip" retip

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "LISTENERADD caller=$retip"
		echo
		return
	fi

	long "Register event listener"
	echo "  Caller .........: $retip"

	handle_register "$hex2" | indent 2
}

function handle_UREG() {
	local hex1="$1" hex2="$2" retip

	hex_to_num "$hex1" 8 8 0 retip
	get_sym "$retip" retip

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "LISTENERDEL caller=$retip"
		echo
		return
	fi

	long "Unregister event listener"
	echo "  Caller .........: $retip"

	handle_register "$hex2" | indent 2
}

function handle_TMO() {
	local hex="$1" force

	hex_to_num "$hex" 4 4 1 force

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "${RESET}${RED}TIMEOUT${RESET} force=$force"
		echo
		return
	fi

	long "${RESET}${RED}Timeout occurred${RESET}"
	echo "  Force restart ..: $force"
}

function handle_SYN1() {
	local hex="$1" state seq c cr

	hex_to_num "$hex" 4 4 1 state
	hex_to_num "$hex" 8 8 1 seq

	if [[ $state -eq 0 ]] ; then
		# Sync wait despite running state=idle
		c="${RESET}${RED}"
		cr="${RESET}"
	fi

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "${c}SYNCWAIT_START${cr}"
		echo
		return
	fi

	long "Synchronous wait start"
	echo -e "  SCLP running state ...... ...: ${c}$state${cr}"
	echo "  Sync wait sequence number ...: $seq"
}

function handle_SYN2() {
	local hex="$1" state c cr

	hex_to_num "$hex" 4 4 1 state
	hex_to_num "$hex" 8 8 1 seq

	if [[ $state -eq 1 ]] ; then
		# Sync wait exit despite running state=running
		c="${RESET}${RED}"
		cr="${RESET}"
	fi

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "${c}SYNCWAIT_END${cr}"
		echo
		return
	fi

	long "Synchronous wait end"
	echo -e "  SCLP running state ...... ...: ${c}$state${cr}"
	echo "  Sync wait sequence number ...: $seq"
}

#
# handle_unknown LINES
#
# Handle unrecognized trace data.
#
function handle_unknown() {
	local hex=("$@")

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		short "INCOMPLETE_ENTRY"
		echo
		handle_buffer "" "" "${hex[@]}" | indent 4
	else
		long "Incomplete trace entry"
		handle_buffer "Raw data" "" "${hex[@]}" | indent 2
	fi
}

#
# print_ts
#
# Convert timestamp to date format, assuming local timezone.
#
function print_ts() {
	local ts="$1" s ns

	s=${ts%:*}
	ns=${ts#*:}
	echo -n "$(date '+%Y-%m-%d %H:%M:%S' -d @"$s").$ns"
}

#
# print_ts_diff NOW LAST
#
# Print delta between timestamps NOW and LAST.
#
function print_ts_diff() {
	local now="$1" last="$2" _var="$3"
	local now_s now_us last_s last_us dist_s dist_us

	[[ -z "$last" ]] && return
	[[ -n "$_var" ]] && _var="-v $_var"

	now_s=${now%:*}
	now_us=${now#*:}
	last_s=${last%:*}
	last_us=${last#*:}

	now_s=$(( 10#$now_s ))
	now_us=$(( 10#$now_us ))
	last_s=$(( 10#$last_s ))
	last_us=$(( 10#$last_us ))

	(( dist_us=now_us-last_us ))
	(( dist_s=now_s-last_s ))
	if [[ $dist_us -lt 0 ]] ; then
		(( dist_us+=1000000 ))
		(( dist_s--))
	fi

	if [[ $dist_s -gt 3599 ]] ; then
		eval printf $_var "+%dh%dm" "$((dist_s/3600 ))" "$(( (dist_s%3600)/60 ))"
	elif [[ $dist_s -gt 59 ]] ; then
		eval printf $_var "+%dm%ds" "$((dist_s/60 ))" "$(( dist_s%60 ))"
	elif [[ $dist_s -gt 0 ]] ; then
		eval printf $_var "+%d.%ds" $dist_s "$(( dist_us/100000 ))"
	elif [[ $dist_us -gt 1000 ]] ; then
		eval printf $_var "+%dms" "$(( dist_us/1000 ))"
	else
		eval printf $_var "+%dus" $dist_us
	fi
}

function header() {
	local ts="$1" cpu="$2" caller="$3" ts2 tsdiff=""

	cpu=$(( 10#$cpu ))

	if [[ $OPT_VERBOSE -eq 0 ]] ; then
		echo -ne "$YELLOW"
		ts2=${ts//:/.}
		print_ts_diff "$ts" "$LAST_TS" tsdiff
		printf "%03d %-18s (%7s)" "$cpu" "$ts2" "$tsdiff"
		echo -ne ": $RESET"
	else
		echo -ne "${YELLOW}CPU $cpu at "
		print_ts "$ts"
		if [[ -n "$LAST_TS" ]] ; then
			printf " ("
			print_ts_diff "$ts" "$LAST_TS"
			printf ")"
		fi
		get_sym "$caller" caller
		echo -e " at $caller$RESET"
		echo -ne "$YELLOW"
		repeat "=" 78
		echo -e "$RESET"
	fi
	LAST_TS="$ts"
}

function handle_lines() {
	local id="$1" fn

	shift
	[[ $# -eq 0 ]] && return

	if [[ -z "$id" ]] ; then
		fn="handle_unknown"
	else
		fn="${IDS[$id]}"
	fi

	"$fn" "$@"

	[[ $OPT_VERBOSE -eq 1 ]] && echo
}

# Read trace file in s390dbf hex_ascii format and convert to readable format
function process() {
	local area ts level ex cpu caller data hex ascii id
	local active_id lines=() 

	while read -r area ts level ex cpu caller data ; do
		hex="${data%% |*}"
		ascii="${data#*| }"

		id=${ascii:0:4}
		id=${id%%.*}

		if [[ "$once" != 1 ]] ; then
			echo -n "  Start time ...: "
			print_ts "$ts"
			printf "\n\n"
			if [[ $OPT_VERBOSE -eq 0 ]] ; then
				echo -en "$BOLD"
				printf "%3s %18s (%7s): %s" \
					"CPU" "TIME" "DELTA" "TRACE ENTRY"
				echo -e "$RESET"
			fi
			
		fi

		if ! is_id "$id" ; then
			if [[ -z "$active_id" ]] &&
			   [[ "${#lines[@]}" -eq 0 ]]; then
				header "$ts" "$cpu" "$caller"
			fi

			# Add to data for active ID
			lines+=("$hex")

			continue
		fi

		# Handle previous data
		handle_lines "$active_id" "${lines[@]}"

		active_id="$id"
		header "$ts" "$cpu" "$caller"
		lines=("$hex")
	done

	# Handle final data
	handle_lines "$active_id" "${lines[@]}"
}


#
# Main
#

declare -a SYMS
declare TRACEFILE TRACEBASE TRACELEVEL TRACEPAGES LAST_TS SHORTOPTS LONGOPTS
declare OPT_VERBOSE OPT_SYMTAB OPT_NOSYM OPT_RAW OPTS

TRACEBASE="$DEBUGFS/s390dbf/sclp"
TRACENAME="default log"
SHORTOPTS="vhs:fl:SePp:r"
LONGOPTS="help,version,verbose,symtab:,no-symtab,raw,flush,level:,pages:,no-pager,errlog"

# Parse parameters
OPTS=$(getopt -o "$SHORTOPTS" -l "$LONGOPTS" -n "Usage error" -- "$@") || exit 1

eval set -- $OPTS

OPT_VERBOSE=0
OPT_SYMTAB="/proc/kallsyms"
OPT_NOSYM=0
OPT_RAW=0
OPT_NOTRACE=0

while [[ "$1" != "--" ]] ; do
	case "$1" in
	-h|--help)
		print_help
		exit 0
		;;
	--version)
		print_version
		exit 0
		;;
	-v|--verbose)
		OPT_VERBOSE=1
		;;
	-s|--symtab)
		[[ ! -e "$2" ]] && die "Cannot access symbol table file $2"
		OPT_SYMTAB="$2"
		shift
		;;
	-S|--no-symtab)
		OPT_NOSYM=1
		;;
	-r|--raw)
		OPT_RAW=1
		;;
	-f|--flush)
		echo "Flushing trace buffers for $TRACENAME"
		if ! echo - > "$TRACEBASE/flush" ; then
			die "Cannot flush trace buffer"
		fi
		OPT_NOTRACE=1
		;;
	-l|--level)
		shift
		echo "Setting trace level for $TRACENAME to $1"
		if ! echo "$1" > "$TRACEBASE/level" ; then
			die "Cannot set trace level"
		fi
		OPT_NOTRACE=1
		;;
	-p|--pages)
		shift
		echo "Setting trace size for $TRACENAME to $1 pages ($(($1*4))kb)"
		if ! echo "$1" > "$TRACEBASE/pages" ; then
			die "Cannot set trace size"
		fi
		OPT_NOTRACE=1
		;;
	-P|--no-pager)
		PAGER="cat"
		;;
	-e|--errlog)
		TRACEBASE="$DEBUGFS/s390dbf/sclp_err"
		TRACENAME="error log"
		echo "Switching to $TRACENAME"
		;;
	esac
	shift
done
shift

if [[ $OPT_NOTRACE -eq 1 ]] ; then
	echo "Done"
	exit 0
fi

if [[ $# -gt 0 ]] ; then
	TRACEFILE="$(readlink -f "$1")"
	[[ ! -e "$TRACEFILE" ]] && die "Cannot access $TRACEFILE: File not found"
else
	TRACEFILE="$TRACEBASE/hex_ascii"
	[[ ! -d "$DEBUGFS" ]] && die "Cannot access $DEBUGFS: Directory not found"
	[[ ! -x "$DEBUGFS" ]] && die "Cannot read $DEBUGFS: Permission denied"
	[[ ! -d "$TRACEBASE" ]] && die "Kernel is missing required support"
	read TRACELEVEL <"$TRACEBASE/level"
	read TRACEPAGES <"$TRACEBASE/pages"
fi

check_tracefile "$TRACEFILE"

if [[ $OPT_RAW -eq 1 ]] ; then
	sort_file "$TRACEFILE" | $PAGER
	exit 0
fi

if [[ $OPT_NOSYM -eq 0 ]] ; then
	read_symtab "$OPT_SYMTAB"
fi

{
	echo "Trace data:"
	echo "  Source .......: $TRACEFILE"
	[[ -n "$TRACELEVEL" ]] && echo "  Level ........: $TRACELEVEL"
	[[ -n "$TRACEPAGES" ]] &&
		echo "  Size .........: $TRACEPAGES pages ($((4*TRACEPAGES))kb)"
	process  < <(sort_file "$TRACEFILE")
} | $PAGER

exit 0