1 # Oil xtrace
2
3 #### Customize PS4
4 shopt -s oil:basic
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 4 . builtin echo 1
18 4 . builtin echo 2
19 5 . builtin echo 3
20 ## END
21
22
23 #### xtrace_details doesn't show [[ ]] etc.
24 shopt -s oil:basic
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:basic 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:basic
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:basic
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:basic
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:basic
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:basic
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 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
157
158 ## stdout-json: ""
159 ## STDERR:
160 . builtin ':' begin
161 | forkwait 12345
162 . 12345 builtin ':' 1
163 > 12345 proc p
164 . 12345 builtin ':' p
165 < 12345 proc p
166 + 12345 exit 3
167 ; process 12345: status 3
168 . builtin set '+x'
169 ## END
170
171 #### command sub
172 shopt --set oil:basic
173 set -x
174
175 {
176 echo foo=$(echo bar)
177 set +x
178
179 } 2>err.txt
180
181 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
182
183 ## STDOUT:
184 foo=bar
185 ## END
186 ## STDERR:
187 | command sub 12345
188 . 12345 builtin echo bar
189 ; process 12345: status 0
190 . builtin echo 'foo=bar'
191 . builtin set '+x'
192 ## END
193
194 #### process sub (nondeterministic)
195 shopt --set oil:basic
196 shopt --unset errexit
197 set -x
198
199 # we wait() for them all at the end
200
201 {
202 : begin
203 cat <(seq 2) <(echo 1)
204 set +x
205 } 2>err.txt
206
207 # SORT for determinism
208 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
209 LC_ALL=C sort >&2
210 #cat err.txt >&2
211
212 ## STDOUT:
213 1
214 2
215 1
216 ## END
217
218 ## STDERR:
219 . 12345 builtin echo 1
220 . 12345 exec seq 2
221 . builtin ':' begin
222 . builtin set '+x'
223 ; process 12345: status 0
224 ; process 12345: status 0
225 ; process 12345: status 0
226 | command 12345: cat '/dev/fd/N' '/dev/fd/N'
227 | proc sub 12345
228 | proc sub 12345
229 ## END
230
231 #### pipeline (nondeterministic)
232 shopt --set oil:basic
233 set -x
234
235 myfunc() {
236 echo 1
237 echo 2
238 }
239
240 {
241 : begin
242 myfunc | sort | wc -l
243 set +x
244 } 2>err.txt
245
246 # SORT for determinism
247 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
248 LC_ALL=C sort >&2
249
250 ## STDOUT:
251 2
252 ## END
253 ## STDERR:
254 . 12345 builtin echo 1
255 . 12345 builtin echo 2
256 . 12345 exec sort
257 < 12345 proc myfunc
258 > 12345 proc myfunc
259 ; process 12345: status 0
260 ; process 12345: status 0
261 ; process 12345: status 0
262 | command 12345: wc -l
263 | part 12345
264 | part 12345
265 . builtin ':' begin
266 . builtin set '+x'
267 < pipeline
268 > pipeline
269 ## END
270
271 #### singleton pipeline
272
273 # Hm extra tracing
274
275 shopt --set oil:basic
276 set -x
277
278 : begin
279 ! false
280 : end
281
282 ## stdout-json: ""
283 ## STDERR:
284 . builtin ':' begin
285 > pipeline
286 . builtin false
287 < pipeline
288 . builtin ':' end
289 ## END
290
291 #### Background pipeline (separate code path)
292
293 shopt --set oil:basic
294 shopt --unset errexit
295 set -x
296
297 myfunc() {
298 echo 2
299 echo 1
300 }
301
302 {
303 : begin
304 myfunc | sort | grep ZZZ &
305 wait
306 echo status=$?
307 set +x
308 } 2>err.txt
309
310 # SORT for determinism
311 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
312 LC_ALL=C sort >&2
313
314 ## STDOUT:
315 status=0
316 ## END
317 ## STDERR:
318 . 12345 builtin echo 1
319 . 12345 builtin echo 2
320 . 12345 exec grep ZZZ
321 . 12345 exec sort
322 ; process 12345: status 0
323 ; process 12345: status 0
324 ; process 12345: status 1
325 < 12345 proc myfunc
326 > 12345 proc myfunc
327 . builtin ':' begin
328 . builtin echo 'status=0'
329 . builtin set '+x'
330 < wait
331 > wait
332 | part 12345
333 | part 12345
334 | part 12345
335 ## END
336
337 #### Background process with fork and & (nondeterministic)
338 shopt --set oil:basic
339 set -x
340
341 {
342 sleep 0.1 &
343 wait
344
345 shopt -s oil:basic
346
347 fork {
348 sleep 0.1
349 }
350 wait
351 set +x
352 } 2>err.txt
353
354 # SORT for determinism
355 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
356 LC_ALL=C sort >&2
357
358 ## stdout-json: ""
359 ## STDERR:
360 . 12345 exec sleep 0.1
361 . 12345 exec sleep 0.1
362 ; process 12345: status 0
363 ; process 12345: status 0
364 . builtin fork
365 . builtin set '+x'
366 . builtin shopt -s 'oil:basic'
367 < wait
368 < wait
369 > wait
370 > wait
371 | fork 12345
372 | fork 12345
373 ## END
374
375 # others: redirects?
376
377 #### here doc
378 shopt --set oil:basic
379 shopt --unset errexit
380 set -x
381
382 {
383 : begin
384 tac <<EOF
385 3
386 2
387 EOF
388
389 set +x
390 } 2>err.txt
391
392 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
393
394 ## STDOUT:
395 2
396 3
397 ## END
398 ## STDERR:
399 . builtin ':' begin
400 | here doc 12345
401 | command 12345: tac
402 ; process 12345: status 0
403 ; process 12345: status 0
404 . builtin set '+x'
405 ## END
406
407 #### Two here docs
408
409 # BUG: This trace shows an extra process?
410
411 shopt --set oil:basic
412 shopt --unset errexit
413 set -x
414
415 {
416 cat - /dev/fd/3 <<EOF 3<<EOF2
417 xx
418 yy
419 EOF
420 zz
421 EOF2
422
423 set +x
424 } 2>err.txt
425
426 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
427
428 ## STDOUT:
429 xx
430 yy
431 zz
432 ## END
433 ## STDERR:
434 | here doc 12345
435 | here doc 12345
436 | command 12345: cat - '/dev/fd/3'
437 ; process 12345: status 0
438 ; process 12345: status 0
439 ; process 12345: status 0
440 . builtin set '+x'
441 ## END
442
443 #### Control Flow
444 shopt --set oil:basic
445 set -x
446
447 for i in 1 2 3 {
448 echo $i
449 if (i === '2') {
450 break
451 }
452 }
453 ## STDOUT:
454 1
455 2
456 ## END
457 ## STDERR:
458 . builtin echo 1
459 . builtin echo 2
460 + break
461 ## END
462
463 #### QSN encoded argv
464 shopt --set oil:basic
465 set -x
466
467 echo $'one two\n' $'\u03bc'
468 ## STDOUT:
469 one two
470 μ
471 ## END
472 ## STDERR:
473 . builtin echo 'one two\n' 'μ'
474 ## END