1 # Oil xtrace
2
3 #### Customize PS4
4 shopt -s oil:upgrade
5 set -x
6
7 # Reuse the default
8 PS4='$LINENO '"$PS4"
9 echo 1; echo 2
10 echo 3
11 ## STDOUT:
12 1
13 2
14 3
15 ## END
16 ## STDERR:
17 5 . builtin echo 1
18 5 . builtin echo 2
19 6 . builtin echo 3
20 ## END
21
22
23 #### xtrace_details doesn't show [[ ]] etc.
24 shopt -s oil:upgrade
25 set -x
26
27 dir=/
28 if [[ -d $dir ]]; then
29 (( a = 42 ))
30 fi
31 cd /
32
33 ## stdout-json: ""
34 ## STDERR:
35 . builtin cd '/'
36 ## END
37
38 #### xtrace_details AND xtrace_rich on
39 shopt -s oil:upgrade xtrace_details
40 shopt --unset errexit
41 set -x
42
43 {
44 env false
45 set +x
46 } 2>err.txt
47
48 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
49
50 ## STDOUT:
51 ## END
52 ## STDERR:
53 | command 12345: env false
54 ; process 12345: status 1
55 . builtin set '+x'
56 ## END
57
58 #### proc and shell function
59 shopt --set oil:upgrade
60 set -x
61
62 shfunc() {
63 : $1
64 }
65
66 proc p {
67 : $1
68 }
69
70 shfunc 1
71 p 2
72 ## stdout-json: ""
73 ## STDERR:
74 > proc shfunc 1
75 . builtin ':' 1
76 < proc shfunc
77 > proc p 2
78 . builtin ':' 2
79 < proc p
80 ## END
81
82 #### eval
83 shopt --set oil:upgrade
84 set -x
85
86 eval 'echo 1; echo 2'
87 ## STDOUT:
88 1
89 2
90 ## END
91 ## STDERR:
92 > eval
93 . builtin echo 1
94 . builtin echo 2
95 < eval
96 ## END
97
98 #### source
99 echo 'echo source-argv: "$@"' > lib.sh
100
101 shopt --set oil:upgrade
102 set -x
103
104 source lib.sh 1 2 3
105
106 ## STDOUT:
107 source-argv: 1 2 3
108 ## END
109 ## STDERR:
110 > source lib.sh 1 2 3
111 . builtin echo 'source-argv:' 1 2 3
112 < source lib.sh
113 ## END
114
115 #### external and builtin
116 shopt --set oil:upgrade
117 shopt --unset errexit
118 set -x
119
120 {
121 env false
122 true
123 set +x
124 } 2>err.txt
125
126 # normalize PIDs, assumed to be 2 or more digits
127 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
128
129 ## stdout-json: ""
130 ## STDERR:
131 | command 12345: env false
132 ; process 12345: status 1
133 . builtin true
134 . builtin set '+x'
135 ## END
136
137 #### subshell
138 shopt --set oil:upgrade
139 shopt --unset errexit
140 set -x
141
142 proc p {
143 : p
144 }
145
146 {
147 : begin
148 (
149 : 1
150 p
151 exit 3
152 )
153 set +x
154 } 2>err.txt
155
156 # Hack: sort for determinism
157 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt | LANG=C sort >&2
158
159 ## stdout-json: ""
160 ## STDERR:
161 . 12345 builtin ':' p
162 + 12345 exit 3
163 . 12345 builtin ':' 1
164 < 12345 proc p
165 > 12345 proc p
166 . builtin ':' begin
167 . builtin set '+x'
168 ; process 12345: status 3
169 | forkwait 12345
170 ## END
171
172 #### command sub
173 shopt --set oil:upgrade
174 set -x
175
176 {
177 echo foo=$(echo bar)
178 set +x
179
180 } 2>err.txt
181
182 # HACK: sort because xtrace output has non-determinism.
183 # This is arguably a bug -- see issue #995.
184 # The real fix might be to sys.stderr.flush() in few places?
185 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt | LANG=C sort >&2
186
187 ## STDOUT:
188 foo=bar
189 ## END
190 ## STDERR:
191 . 12345 builtin echo bar
192 . builtin echo 'foo=bar'
193 . builtin set '+x'
194 ; process 12345: status 0
195 | command sub 12345
196 ## END
197
198 #### process sub (nondeterministic)
199 shopt --set oil:upgrade
200 shopt --unset errexit
201 set -x
202
203 # we wait() for them all at the end
204
205 {
206 : begin
207 cat <(seq 2) <(echo 1)
208 set +x
209 } 2>err.txt
210
211 # SORT for determinism
212 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
213 LC_ALL=C sort >&2
214 #cat err.txt >&2
215
216 ## STDOUT:
217 1
218 2
219 1
220 ## END
221
222 ## STDERR:
223 . 12345 builtin echo 1
224 . 12345 exec seq 2
225 . builtin ':' begin
226 . builtin set '+x'
227 ; process 12345: status 0
228 ; process 12345: status 0
229 ; process 12345: status 0
230 | command 12345: cat '/dev/fd/N' '/dev/fd/N'
231 | proc sub 12345
232 | proc sub 12345
233 ## END
234
235 #### pipeline (nondeterministic)
236 shopt --set oil:upgrade
237 set -x
238
239 myfunc() {
240 echo 1
241 echo 2
242 }
243
244 {
245 : begin
246 myfunc | sort | wc -l
247 set +x
248 } 2>err.txt
249
250 do_sort=1
251
252 if test -n $do_sort; then
253 # SORT for determinism
254 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
255 LC_ALL=C sort >&2
256 else
257 cat err.txt
258 fi
259
260 ## STDOUT:
261 2
262 ## END
263 ## STDERR:
264 . 12345 builtin echo 1
265 . 12345 builtin echo 2
266 . 12345 exec sort
267 < 12345 proc myfunc
268 > 12345 proc myfunc
269 ; process 12345: status 0
270 ; process 12345: status 0
271 ; process 12345: status 0
272 | command 12345: wc -l
273 | part 12345
274 | part 12345
275 . builtin ':' begin
276 . builtin set '+x'
277 < pipeline
278 > pipeline
279 ## END
280
281 #### singleton pipeline
282
283 # Hm extra tracing
284
285 shopt --set oil:upgrade
286 set -x
287
288 : begin
289 ! false
290 : end
291
292 ## stdout-json: ""
293 ## STDERR:
294 . builtin ':' begin
295 . builtin false
296 . builtin ':' end
297 ## END
298
299 #### Background pipeline (separate code path)
300
301 shopt --set oil:upgrade
302 shopt --unset errexit
303 set -x
304
305 myfunc() {
306 echo 2
307 echo 1
308 }
309
310 {
311 : begin
312 myfunc | sort | grep ZZZ &
313 wait
314 echo status=$?
315 set +x
316 } 2>err.txt
317
318 # SORT for determinism
319 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
320 LC_ALL=C sort >&2
321
322 ## STDOUT:
323 status=0
324 ## END
325 ## STDERR:
326 . 12345 builtin echo 1
327 . 12345 builtin echo 2
328 . 12345 exec grep ZZZ
329 . 12345 exec sort
330 ; process 12345: status 0
331 ; process 12345: status 0
332 ; process 12345: status 1
333 < 12345 proc myfunc
334 > 12345 proc myfunc
335 . builtin ':' begin
336 . builtin echo 'status=0'
337 . builtin set '+x'
338 < wait
339 > wait
340 | part 12345
341 | part 12345
342 | part 12345
343 ## END
344
345 #### Background process with fork and & (nondeterministic)
346 shopt --set oil:upgrade
347 set -x
348
349 {
350 sleep 0.1 &
351 wait
352
353 shopt -s oil:upgrade
354
355 fork {
356 sleep 0.1
357 }
358 wait
359 set +x
360 } 2>err.txt
361
362 # SORT for determinism
363 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
364 LC_ALL=C sort >&2
365
366 ## stdout-json: ""
367 ## STDERR:
368 . 12345 exec sleep 0.1
369 . 12345 exec sleep 0.1
370 ; process 12345: status 0
371 ; process 12345: status 0
372 . builtin fork
373 . builtin set '+x'
374 . builtin shopt -s 'oil:upgrade'
375 < wait
376 < wait
377 > wait
378 > wait
379 | fork 12345
380 | fork 12345
381 ## END
382
383 # others: redirects?
384
385 #### here doc
386 shopt --set oil:upgrade
387 shopt --unset errexit
388 set -x
389
390 {
391 : begin
392 tac <<EOF
393 3
394 2
395 EOF
396
397 set +x
398 } 2>err.txt
399
400 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
401
402 ## STDOUT:
403 2
404 3
405 ## END
406 ## STDERR:
407 . builtin ':' begin
408 | here doc 12345
409 | command 12345: tac
410 ; process 12345: status 0
411 ; process 12345: status 0
412 . builtin set '+x'
413 ## END
414
415 #### Two here docs
416
417 # BUG: This trace shows an extra process?
418
419 shopt --set oil:upgrade
420 shopt --unset errexit
421 set -x
422
423 {
424 cat - /dev/fd/3 <<EOF 3<<EOF2
425 xx
426 yy
427 EOF
428 zz
429 EOF2
430
431 set +x
432 } 2>err.txt
433
434 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
435
436 ## STDOUT:
437 xx
438 yy
439 zz
440 ## END
441 ## STDERR:
442 | here doc 12345
443 | here doc 12345
444 | command 12345: cat - '/dev/fd/3'
445 ; process 12345: status 0
446 ; process 12345: status 0
447 ; process 12345: status 0
448 . builtin set '+x'
449 ## END
450
451 #### Control Flow
452 shopt --set oil:upgrade
453 set -x
454
455 for i in 1 2 3 {
456 echo $i
457 if (i === '2') {
458 break
459 }
460 }
461
462 for j in a b {
463 for k in y z {
464 echo $j $k
465 if (k === 'y') {
466 continue
467 }
468 }
469 }
470
471 proc zero {
472 return 0
473 }
474
475 proc one {
476 return 1
477 }
478
479 zero
480 # one
481
482 ## STDOUT:
483 1
484 2
485 a y
486 a z
487 b y
488 b z
489 ## END
490 ## STDERR:
491 . builtin echo 1
492 . builtin echo 2
493 + break 1
494 . builtin echo a y
495 + continue 1
496 . builtin echo a z
497 . builtin echo b y
498 + continue 1
499 . builtin echo b z
500 > proc zero
501 + return 0
502 < proc zero
503 ## END
504
505 #### QSN encoded argv
506 shopt --set oil:upgrade
507 set -x
508
509 echo $'one two\n' $'\u03bc'
510 ## STDOUT:
511 one two
512 μ
513 ## END
514 ## STDERR:
515 . builtin echo 'one two\n' 'μ'
516 ## END