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 # SORT for determinism
251 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
252 LC_ALL=C sort >&2
253
254 ## STDOUT:
255 2
256 ## END
257 ## STDERR:
258 . 12345 builtin echo 1
259 . 12345 builtin echo 2
260 . 12345 exec sort
261 < 12345 proc myfunc
262 > 12345 proc myfunc
263 ; process 12345: status 0
264 ; process 12345: status 0
265 ; process 12345: status 0
266 | command 12345: wc -l
267 | part 12345
268 | part 12345
269 . builtin ':' begin
270 . builtin set '+x'
271 < pipeline
272 > pipeline
273 ## END
274
275 #### singleton pipeline
276
277 # Hm extra tracing
278
279 shopt --set oil:upgrade
280 set -x
281
282 : begin
283 ! false
284 : end
285
286 ## stdout-json: ""
287 ## STDERR:
288 . builtin ':' begin
289 . builtin false
290 . builtin ':' end
291 ## END
292
293 #### Background pipeline (separate code path)
294
295 shopt --set oil:upgrade
296 shopt --unset errexit
297 set -x
298
299 myfunc() {
300 echo 2
301 echo 1
302 }
303
304 {
305 : begin
306 myfunc | sort | grep ZZZ &
307 wait
308 echo status=$?
309 set +x
310 } 2>err.txt
311
312 # SORT for determinism
313 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
314 LC_ALL=C sort >&2
315
316 ## STDOUT:
317 status=0
318 ## END
319 ## STDERR:
320 . 12345 builtin echo 1
321 . 12345 builtin echo 2
322 . 12345 exec grep ZZZ
323 . 12345 exec sort
324 ; process 12345: status 0
325 ; process 12345: status 0
326 ; process 12345: status 1
327 < 12345 proc myfunc
328 > 12345 proc myfunc
329 . builtin ':' begin
330 . builtin echo 'status=0'
331 . builtin set '+x'
332 < wait
333 > wait
334 | part 12345
335 | part 12345
336 | part 12345
337 ## END
338
339 #### Background process with fork and & (nondeterministic)
340 shopt --set oil:upgrade
341 set -x
342
343 {
344 sleep 0.1 &
345 wait
346
347 shopt -s oil:upgrade
348
349 fork {
350 sleep 0.1
351 }
352 wait
353 set +x
354 } 2>err.txt
355
356 # SORT for determinism
357 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
358 LC_ALL=C sort >&2
359
360 ## stdout-json: ""
361 ## STDERR:
362 . 12345 exec sleep 0.1
363 . 12345 exec sleep 0.1
364 ; process 12345: status 0
365 ; process 12345: status 0
366 . builtin fork
367 . builtin set '+x'
368 . builtin shopt -s 'oil:upgrade'
369 < wait
370 < wait
371 > wait
372 > wait
373 | fork 12345
374 | fork 12345
375 ## END
376
377 # others: redirects?
378
379 #### here doc
380 shopt --set oil:upgrade
381 shopt --unset errexit
382 set -x
383
384 {
385 : begin
386 tac <<EOF
387 3
388 2
389 EOF
390
391 set +x
392 } 2>err.txt
393
394 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
395
396 ## STDOUT:
397 2
398 3
399 ## END
400 ## STDERR:
401 . builtin ':' begin
402 | here doc 12345
403 | command 12345: tac
404 ; process 12345: status 0
405 ; process 12345: status 0
406 . builtin set '+x'
407 ## END
408
409 #### Two here docs
410
411 # BUG: This trace shows an extra process?
412
413 shopt --set oil:upgrade
414 shopt --unset errexit
415 set -x
416
417 {
418 cat - /dev/fd/3 <<EOF 3<<EOF2
419 xx
420 yy
421 EOF
422 zz
423 EOF2
424
425 set +x
426 } 2>err.txt
427
428 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
429
430 ## STDOUT:
431 xx
432 yy
433 zz
434 ## END
435 ## STDERR:
436 | here doc 12345
437 | here doc 12345
438 | command 12345: cat - '/dev/fd/3'
439 ; process 12345: status 0
440 ; process 12345: status 0
441 ; process 12345: status 0
442 . builtin set '+x'
443 ## END
444
445 #### Control Flow
446 shopt --set oil:upgrade
447 set -x
448
449 for i in 1 2 3 {
450 echo $i
451 if (i === '2') {
452 break
453 }
454 }
455
456 for j in a b {
457 for k in y z {
458 echo $j $k
459 if (k === 'y') {
460 continue
461 }
462 }
463 }
464
465 proc zero {
466 return 0
467 }
468
469 proc one {
470 return 1
471 }
472
473 zero
474 # one
475
476 ## STDOUT:
477 1
478 2
479 a y
480 a z
481 b y
482 b z
483 ## END
484 ## STDERR:
485 . builtin echo 1
486 . builtin echo 2
487 + break 1
488 . builtin echo a y
489 + continue 1
490 . builtin echo a z
491 . builtin echo b y
492 + continue 1
493 . builtin echo b z
494 > proc zero
495 + return 0
496 < proc zero
497 ## END
498
499 #### QSN encoded argv
500 shopt --set oil:upgrade
501 set -x
502
503 echo $'one two\n' $'\u03bc'
504 ## STDOUT:
505 one two
506 μ
507 ## END
508 ## STDERR:
509 . builtin echo 'one two\n' 'μ'
510 ## END