Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 1 | All about co_lnotab, the line number table. |
| 2 | |
| 3 | Code objects store a field named co_lnotab. This is an array of unsigned bytes |
| 4 | disguised as a Python string. It is used to map bytecode offsets to source code |
| 5 | line #s for tracebacks and to identify line number boundaries for line tracing. |
| 6 | |
| 7 | The array is conceptually a compressed list of |
| 8 | (bytecode offset increment, line number increment) |
| 9 | pairs. The details are important and delicate, best illustrated by example: |
| 10 | |
| 11 | byte code offset source code line number |
| 12 | 0 1 |
| 13 | 6 2 |
| 14 | 50 7 |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 15 | 350 207 |
| 16 | 361 208 |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 17 | |
| 18 | Instead of storing these numbers literally, we compress the list by storing only |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 19 | the difference from one row to the next. Conceptually, the stored list might |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 20 | look like: |
| 21 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 22 | 0, 1, 6, 1, 44, 5, 300, 200, 11, 1 |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 23 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 24 | The above doesn't really work, but it's a start. An unsigned byte (byte code |
Victor Stinner | 9f78939 | 2016-01-21 18:12:29 +0100 | [diff] [blame] | 25 | offset) can't hold negative values, or values larger than 255, a signed byte |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 26 | (line number) can't hold values larger than 127 or less than -128, and the |
| 27 | above example contains two such values. So we make two tweaks: |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 28 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 29 | (a) there's a deep assumption that byte code offsets increase monotonically, |
| 30 | and |
| 31 | (b) if byte code offset jumps by more than 255 from one row to the next, or if |
| 32 | source code line number jumps by more than 127 or less than -128 from one row |
| 33 | to the next, more than one pair is written to the table. In case #b, |
| 34 | there's no way to know from looking at the table later how many were written. |
| 35 | That's the delicate part. A user of co_lnotab desiring to find the source |
| 36 | line number corresponding to a bytecode address A should do something like |
| 37 | this: |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 38 | |
| 39 | lineno = addr = 0 |
| 40 | for addr_incr, line_incr in co_lnotab: |
| 41 | addr += addr_incr |
| 42 | if addr > A: |
| 43 | return lineno |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 44 | if line_incr >= 0x80: |
| 45 | line_incr -= 0x100 |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 46 | lineno += line_incr |
| 47 | |
| 48 | (In C, this is implemented by PyCode_Addr2Line().) In order for this to work, |
| 49 | when the addr field increments by more than 255, the line # increment in each |
| 50 | pair generated must be 0 until the remaining addr increment is < 256. So, in |
| 51 | the example above, assemble_lnotab in compile.c should not (as was actually done |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 52 | until 2.2) expand 300, 200 to |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 53 | 255, 255, 45, 45, |
| 54 | but to |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 55 | 255, 0, 45, 128, 0, 72. |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 56 | |
| 57 | The above is sufficient to reconstruct line numbers for tracebacks, but not for |
| 58 | line tracing. Tracing is handled by PyCode_CheckLineNumber() in codeobject.c |
| 59 | and maybe_call_line_trace() in ceval.c. |
| 60 | |
| 61 | *** Tracing *** |
| 62 | |
| 63 | To a first approximation, we want to call the tracing function when the line |
| 64 | number of the current instruction changes. Re-computing the current line for |
| 65 | every instruction is a little slow, though, so each time we compute the line |
| 66 | number we save the bytecode indices where it's valid: |
| 67 | |
| 68 | *instr_lb <= frame->f_lasti < *instr_ub |
| 69 | |
| 70 | is true so long as execution does not change lines. That is, *instr_lb holds |
| 71 | the first bytecode index of the current line, and *instr_ub holds the first |
| 72 | bytecode index of the next line. As long as the above expression is true, |
| 73 | maybe_call_line_trace() does not need to call PyCode_CheckLineNumber(). Note |
| 74 | that the same line may appear multiple times in the lnotab, either because the |
| 75 | bytecode jumped more than 255 indices between line number changes or because |
| 76 | the compiler inserted the same line twice. Even in that case, *instr_ub holds |
| 77 | the first index of the next line. |
| 78 | |
| 79 | However, we don't *always* want to call the line trace function when the above |
| 80 | test fails. |
| 81 | |
| 82 | Consider this code: |
| 83 | |
| 84 | 1: def f(a): |
| 85 | 2: while a: |
| 86 | 3: print 1, |
| 87 | 4: break |
| 88 | 5: else: |
| 89 | 6: print 2, |
| 90 | |
| 91 | which compiles to this: |
| 92 | |
| 93 | 2 0 SETUP_LOOP 19 (to 22) |
| 94 | >> 3 LOAD_FAST 0 (a) |
| 95 | 6 POP_JUMP_IF_FALSE 17 |
| 96 | |
| 97 | 3 9 LOAD_CONST 1 (1) |
Victor Stinner | 9f78939 | 2016-01-21 18:12:29 +0100 | [diff] [blame] | 98 | 12 PRINT_ITEM |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 99 | |
Victor Stinner | 9f78939 | 2016-01-21 18:12:29 +0100 | [diff] [blame] | 100 | 4 13 BREAK_LOOP |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 101 | 14 JUMP_ABSOLUTE 3 |
Victor Stinner | 9f78939 | 2016-01-21 18:12:29 +0100 | [diff] [blame] | 102 | >> 17 POP_BLOCK |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 103 | |
| 104 | 6 18 LOAD_CONST 2 (2) |
Victor Stinner | 9f78939 | 2016-01-21 18:12:29 +0100 | [diff] [blame] | 105 | 21 PRINT_ITEM |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 106 | >> 22 LOAD_CONST 0 (None) |
Victor Stinner | 9f78939 | 2016-01-21 18:12:29 +0100 | [diff] [blame] | 107 | 25 RETURN_VALUE |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 108 | |
| 109 | If 'a' is false, execution will jump to the POP_BLOCK instruction at offset 17 |
| 110 | and the co_lnotab will claim that execution has moved to line 4, which is wrong. |
| 111 | In this case, we could instead associate the POP_BLOCK with line 5, but that |
| 112 | would break jumps around loops without else clauses. |
| 113 | |
| 114 | We fix this by only calling the line trace function for a forward jump if the |
| 115 | co_lnotab indicates we have jumped to the *start* of a line, i.e. if the current |
| 116 | instruction offset matches the offset given for the start of a line by the |
| 117 | co_lnotab. For backward jumps, however, we always call the line trace function, |
| 118 | which lets a debugger stop on every evaluation of a loop guard (which usually |
| 119 | won't be the first opcode in a line). |
| 120 | |
| 121 | Why do we set f_lineno when tracing, and only just before calling the trace |
| 122 | function? Well, consider the code above when 'a' is true. If stepping through |
| 123 | this with 'n' in pdb, you would stop at line 1 with a "call" type event, then |
| 124 | line events on lines 2, 3, and 4, then a "return" type event -- but because the |
| 125 | code for the return actually falls in the range of the "line 6" opcodes, you |
| 126 | would be shown line 6 during this event. This is a change from the behaviour in |
| 127 | 2.2 and before, and I've found it confusing in practice. By setting and using |
| 128 | f_lineno when tracing, one can report a line number different from that |
| 129 | suggested by f_lasti on this one occasion where it's desirable. |