-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathxtrace1.sh
executable file
·371 lines (288 loc) · 5.71 KB
/
xtrace1.sh
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
#!/usr/bin/env bash
#
# Usage:
# demo/xtrace1.sh <function name>
#set -o nounset
#set -o pipefail
#set -o errexit
myfunc() {
: "myfunc $1"
}
banner() {
echo
echo "$@"
echo
}
# bash is the only shell with the "first char repeated" behavior
first_char() {
for sh in bash dash mksh zsh bin/osh; do
echo
echo $sh
export PS4='$PWD '
#export PS4='$ '
$sh -x -c 'echo $(echo hi)-'
done
}
# bash repeats the + for command sub, eval, source. Other shells don't.
posix() {
banner COMMANDSUB
set -x
foo=$(myfunc commandsub)
set +x
# Hm this gives you ++
banner EVAL
set -x
eval myfunc evalarg
set +x
# Also gives you ++
banner SOURCE
set -x
. spec/testdata/source-argv.sh 1 2
set +x
}
# Various stacks:
# - proc call stack (similar: FUNCNAME)
# - process stack (similar: BASHPID)
# - interpreter stack (eval, source. xtrace already respects this)
# - and maybe Oil subinterpreters
# User level:
# - Color
# - Indentation
# - HTML
#
# What you really want PARSEABLE traces. Which means each trace item is ONE
# LINE. And emitted by a single write() call.
#
# Related debugging features of OSH:
#
# - pp cell_ (ASDL), pp proc (QTT)
# - osh -n (ASDL)
# - Oil expressions: = keyword (ASDL)
shopt -s expand_aliases
alias e=echo
simple() {
e alias
myfunc invoke
( myfunc subshell )
}
main() {
banner ALIAS
set -x
e alias
set +x
banner FUNC
set -x
myfunc invoke
set +x
banner SUBSHELL
# No increase in +
# pid and SHLVL do NOT increase. BASHPID increases.
set -x
: pid=$$ BASHPID=$BASHPID SHLVL=$SHLVL
( myfunc subshell; : pid=$$ BASHPID=$BASHPID SHLVL=$SHLVL )
set +x
# Now it changes to ++
banner COMMANDSUB
set -x
foo=$(myfunc commandsub)
set +x
banner PIPELINE
set -x
myfunc pipeline | sort
set +x
banner THREE
# Increase to three
set -x
foo=$(echo $(myfunc commandsub))
echo $foo
set +x
# Hm this gives you ++
banner EVAL
set -x
eval myfunc evalarg
set +x
# Also gives you ++
banner SOURCE
set -x
source spec/testdata/source-argv.sh 1 2
set +x
banner RECURSIVE
set -x
$0 myfunc dollar-zero
set +x
# TODO: SHELLOPTS not set here?
banner "SHELLOPTS=$SHELLOPTS"
export SHELLOPTS
set -x
$0 myfunc dollar-zero-shellopts
set +x
}
main2() {
set -x
# OK this is useful.
# https://unix.stackexchange.com/questions/355965/how-to-check-which-line-of-a-bash-script-is-being-executed
PS4='+${LINENO}: '
# Test runtime errors like this
#PS4='+${LINENO}: $(( 1 / 0 ))'
myfunc ps4
foo=$(myfunc ps4-commandsub)
echo foo
}
slowfunc() {
for i in "$@"; do
sleep 0.$i
done
}
concurrency() {
set -x
# PID prefix would be nice here
slowfunc 1 3 | slowfunc 2 4 5 | slowfunc 6
}
task() {
for i in "$@"; do
echo $i
sleep 0.$i
done
}
through_xargs() {
set -x
export PS4='+ $$ '
# This doesn't work because xargs invokes $0! Not OSH.
export OILS_HIJACK_SHEBANG=1
# This makes us trace through xargs.
#
# problem: $0 invokes bash because of the shebang.
# We can't use $SHELL $0.
# - bash is the only shell that sets $SHELL.
# - It's not the right value. "If it is not set when the shell starts, bash
# assigns to it the full pathname of the current user's login shell."
export SHELLOPTS
seq 6 | xargs -n 2 -P 3 -- $0 task
}
my_ps4() {
for i in {1..3}; do
echo -n $i
done
}
# The problem with this is you don't want to fork the shell for every line!
call_func_in_ps4() {
set -x
PS4='[$(my-ps4)] '
echo one
echo two
}
# EXPANDED argv is displayed, NOT the raw input.
# - OK just do assignments?
# - bash shows the 'for x in 1 2 3' all on one line
# - dash doesn't show the 'for'
# - neither does zsh and mksh
# - zsh shows line numbers and the function name!
# - two statements on one line are broken up
# - bash doesn't show 'while'
# The $((i+1)) is evaluated. Hm.
# Hm we don't implement this, only works at top level
# set -v
loop() {
set -x
for x in 1 \
2 \
3; do
echo $x; echo =$(echo {x}-)
done
i=0
while test $i -lt 3; do
echo $x; echo ${x}-
i=$((i+1))
if true; then continue; fi
done
}
atoms1() {
set -x
foo=bar
# This messes up a lot of printing.
x='one
two'
i=1
[[ -n $x ]]; echo "$x"
# $i gets expanded, not i
(( y = 42 + i + $i )); echo yo
[[ -n $x
]]
(( y =
42 +
i +
$i
))
}
atoms2() {
set -x
x='one
two'
declare -a a
a[1]="$x"
# This works
declare -A A
A["$x"]=1
a=(1 2 3)
A=([k]=v)
a=("$x" $x)
A=([k]="$x")
# Assignment builtins
declare -g -r d=0 foo=bar
typeset t=1
local lo=2
export e=3 f=foo
readonly r=4
}
compound() {
set -x
# Nothing for time
time sleep 0
# There is no tracing for () and {}
{ echo b1
echo b2
}
( echo c1
echo c2
)
# no tracing for if; just the conditions
if test -d /; then
echo yes
else
echo no
fi
# Hm this causes a concurrency problem.
# I think we want to buffer the line
ls | wc -l | sort
# There IS tracing for 'case' line
case foo in
fo*)
echo case
;;
*)
echo default
;;
esac
f() {
echo hi
}
}
oil_constructs() {
echo TODO
# BareDecl, VarDecl, PlaceMutation, Expr
}
details() {
PS4='+ ${BASH_SOURCE[0]}:${FUNCNAME[0]}:${LINENO} '
# X_pid can have a space after it, or call it ${X_tag}
# Or should we could call the whole thing ? ${XTRACE_PREFIX} ?
# Idea: $PS5 and $PS6 for push and pop?
# Problem: remove the first char behavior? Or only respect it in PS4.
#
# This string can be OIL_XTRACE_PREFIX='' or something.
PS4=' ${X_indent}${X_punct}${X_tag}${BASH_SOURCE[0]}:${FUNCNAME[0]}:${LINENO} '
set -x
echo hi
echo command=$(echo inner)
eval 'echo eval'
}
"$@"