Skip to content

Commit 9bab144

Browse files
committed
Update v3/docs/opt-trace-format.md
1 parent 4c66e66 commit 9bab144

1 file changed

Lines changed: 152 additions & 1 deletion

File tree

‎v3/docs/opt-trace-format.md‎

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,160 @@ def baz(c):
580580
result = foo(1, 2, 3)
581581
```
582582

583-
Let's jump straight to [Step 8 of 10](http://pythontutor.com/visualize.html#code=def+foo(x,+y,+z)%3A%0A++return+bar(x,+y)%0A++%0Adef+bar(a,+b)%3A%0A++return+baz(a)%0A++%0Adef+baz(c)%3A%0A++return+c%0A++%0Aresult+%3D+foo(1,+2,+3)%0A&mode=display&cumulative=false&py=2&curInstr=7)
583+
Let's jump straight to <a href="http://pythontutor.com/visualize.html#code=def+foo(x,+y,+z)%3A%0A++return+bar(x,+y)%0A++%0Adef+bar(a,+b)%3A%0A++return+baz(a)%0A++%0Adef+baz(c)%3A%0A++return+c%0A++%0Aresult+%3D+foo(1,+2,+3)%0A&mode=display&cumulative=false&py=2&curInstr=7">Step 8 of 10</a>,
584584
when the program is about to return from the call to `baz`.
585585

586+
Study the visualization for a bit. Note that there are four frames currently on the stack: globals, `foo`, `bar`, and `baz`.
587+
Each frame consists of a name and a mapping between constituent variable names and values. There is a special
588+
variable called `Return value`, which represents the value that this function is about to return to its caller.
589+
590+
Let's now look at the execution point object corresponding to this visualization:
591+
592+
```javascript
593+
{
594+
"ordered_globals": [
595+
"foo",
596+
"bar",
597+
"baz"
598+
],
599+
"stdout": "",
600+
"func_name": "baz",
601+
"stack_to_render": [
602+
{
603+
"frame_id": 1,
604+
"encoded_locals": {
605+
"y": 2,
606+
"x": 1,
607+
"z": 3
608+
},
609+
"is_highlighted": false,
610+
"is_parent": false,
611+
"func_name": "foo",
612+
"is_zombie": false,
613+
"parent_frame_id_list": [],
614+
"unique_hash": "foo_f1",
615+
"ordered_varnames": [
616+
"x",
617+
"y",
618+
"z"
619+
]
620+
},
621+
{
622+
"frame_id": 2,
623+
"encoded_locals": {
624+
"a": 1,
625+
"b": 2
626+
},
627+
"is_highlighted": false,
628+
"is_parent": false,
629+
"func_name": "bar",
630+
"is_zombie": false,
631+
"parent_frame_id_list": [],
632+
"unique_hash": "bar_f2",
633+
"ordered_varnames": [
634+
"a",
635+
"b"
636+
]
637+
},
638+
{
639+
"frame_id": 3,
640+
"encoded_locals": {
641+
"__return__": 1,
642+
"c": 1
643+
},
644+
"is_highlighted": true,
645+
"is_parent": false,
646+
"func_name": "baz",
647+
"is_zombie": false,
648+
"parent_frame_id_list": [],
649+
"unique_hash": "baz_f3",
650+
"ordered_varnames": [
651+
"c",
652+
"__return__"
653+
]
654+
}
655+
],
656+
"globals": {
657+
"bar": [
658+
"REF",
659+
2
660+
],
661+
"foo": [
662+
"REF",
663+
1
664+
],
665+
"baz": [
666+
"REF",
667+
3
668+
]
669+
},
670+
"heap": {
671+
"1": [
672+
"FUNCTION",
673+
"foo(x, y, z)",
674+
null
675+
],
676+
"2": [
677+
"FUNCTION",
678+
"bar(a, b)",
679+
null
680+
],
681+
"3": [
682+
"FUNCTION",
683+
"baz(c)",
684+
null
685+
]
686+
},
687+
"line": 8,
688+
"event": "return"
689+
},
690+
```
691+
692+
First things first: This is a `return` event occurring on line 8 (the `return c` line in `baz`). The currently-active
693+
function is `baz`. There are three global variables: `foo`, `bar`, and `baz`, which all point to function objects
694+
on the heap.
695+
696+
The only new kind of field is `stack_to_render`, which is (unsurprisingly) a list of stack frames to render.
697+
In this case, `stack_to_render` contains three elements -- the frames for `foo`, `bar`, and `baz`, in that exact order.
698+
The frontend simply walks down `stack_to_render` and renders each frame in a similar way that it renders global variables.
699+
700+
Let's now zoom in on one particular frame in `stack_to_render`. Here is the frame for `bar`:
701+
702+
```javascript
703+
{
704+
"frame_id": 2,
705+
"encoded_locals": {
706+
"a": 1,
707+
"b": 2
708+
},
709+
"is_highlighted": false,
710+
"is_parent": false,
711+
"func_name": "bar",
712+
"is_zombie": false,
713+
"parent_frame_id_list": [],
714+
"unique_hash": "bar_f2",
715+
"ordered_varnames": [
716+
"a",
717+
"b"
718+
]
719+
},
720+
```
721+
722+
For starters, `func_name` is the name of the function, and `is_highlighted` is true only if the current
723+
frame is the "top-most" one (telling the frontend to highlight it in a brighter color).
724+
725+
`encoded_locals` is a mapping from local variable names to their values, similar to how `globals`
726+
provides a mapping from global variable names to their values.
727+
728+
`ordered_varnames` is an ordered list of keys from `encoded_locals`, usually sorted by order of appearance
729+
during execution. The global analogue for this field is `ordered_globals`. (I suppose this field should be named
730+
`ordered_locals`, but I haven't gotten around to renaming yet.)
731+
732+
733+
(Ignore `is_parent`, `is_zombie`, and `parent_frame_id_list` for now. We'll cover those in the more advanced
734+
"Closures and Zombie Frames" section below.)
735+
736+
586737
## Closures and Zombie Frames (advanced)
587738

588739
(TODO: WRITE ME!)

0 commit comments

Comments
 (0)