Skip to content

Commit c56271a

Browse files
committed
Go over brpt offset interpretation.
Also some cleanup added.
1 parent 2abdbad commit c56271a

7 files changed

Lines changed: 38 additions & 16 deletions

File tree

‎trepan/__main__.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def main(dbg=None, sys_argv=list(sys.argv)):
7474

7575
if dbg is None:
7676
dbg = trepan.debugger_obj = Trepan(dbg_opts)
77-
dbg.core.add_ignore(main)
77+
dbg.core.add_ignore(main, trepan.api.debug)
7878

7979
postprocess_options(dbg, opts)
8080

@@ -316,6 +316,8 @@ def write_wrapper(*args, **kwargs):
316316
break
317317
except DebuggerRestart:
318318
dbg.core.execution_status = "Restart requested"
319+
# for debug:
320+
# dbg.core.execution_status = "Running"
319321
if dbg.program_sys_argv:
320322
sys.argv = list(dbg.program_sys_argv)
321323
part1 = f"Restarting {dbg.core.filename(mainpyfile)} with arguments:"

‎trepan/api.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def debug(
187187
code = frame.f_code
188188
filename = code.co_filename
189189
line_number = frame.f_lineno
190+
last_i = frame.f_lasti
190191

191192
bp = bpmgr.find_breakpoint(filename, line_number)
192193
if bp is None:
@@ -199,14 +200,16 @@ def debug(
199200
code = frame.f_code
200201
filename = code.co_filename
201202
line_number = frame.f_lineno
203+
last_i = frame.f_lasti
202204

203205
bp = core.bpmgr.add_breakpoint(
204206
filename=filename,
205207
line_number=line_number,
206208
is_code_offset=False,
207209
condition=None,
208210
func_or_code=code,
209-
is_breakpoint_call=True
211+
is_breakpoint_call=True,
212+
offset=last_i,
210213
)
211214
elif not bp.enabled:
212215
core.step_ignore = -1

‎trepan/lib/breakpoint.py‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Breakpoint:
3737
how many times the breakpoint was hit,
3838
whether it is disabled, or has a condition associated with the breakpoint.
3939
40-
If is_code_offset is True, position is a code offset. Otherwise, it
40+
If attribute is_code_offset is True, position is a code offset. Otherwise, it
4141
is a 0-origin column offset.
4242
4343
The code offset for start of a method, module, or function is always 0.
@@ -50,6 +50,12 @@ class Breakpoint:
5050
5151
To do this, we need deep undertanding of Python code objects, which we get
5252
from pyficache.
53+
54+
Attribute offset is the code offset, when is_code_offset is true, it is ignored and
55+
should be the same value as position. However when is_code_offset is False, then
56+
we have a line or line/column position. Here offset can be the code offset when there
57+
is a unique value, but since line or line/column might have many offsets associated with them,
58+
the sentinal value -1 is used to indicate any possible offset.
5359
"""
5460

5561
def __init__(
@@ -62,7 +68,8 @@ def __init__(
6268
code: Optional[CodeType] = None,
6369
position: Optional[int] = None,
6470
is_code_offset: bool = True,
65-
is_breakpoint_call: bool = False
71+
is_breakpoint_call: bool = False,
72+
offset: int = -1 # default is any possible code offset
6673
):
6774
# FIXME: split out this top part into a part that fills out information
6875
if code is not None:
@@ -101,8 +108,7 @@ def __init__(
101108
pass
102109
else:
103110
self.column = position
104-
# TODO: Figure out code offset.
105-
self.offset = None
111+
self.offset = offset
106112

107113
self.condition = condition
108114
self.enabled = True
@@ -251,6 +257,7 @@ def add_breakpoint(
251257
condition: Optional[str] = None,
252258
func_or_code=None,
253259
is_breakpoint_call: bool = False,
260+
offset: int = -1 # default is any possible code offset
254261
):
255262
"""
256263
Add a breakpoint in ``filename`` at line number ``line_number``.
@@ -313,6 +320,7 @@ def add_breakpoint(
313320
position,
314321
is_code_offset,
315322
is_breakpoint_call,
323+
offset,
316324
)
317325

318326
# Build the internal lists of breakpoints
@@ -498,8 +506,13 @@ def needs_no_tracing(self) -> bool:
498506
support, i.e. they are handled either by explicit breakpoint() calls
499507
or the newer debug protocal handles the breakpoints
500508
"""
501-
# return len(self.bplist) == 0
502-
return all(not getattr(item, 'is_breakpoint_call', False) for item in self.bplist)
509+
for bp_list in self.bplist.values():
510+
for bp in bp_list:
511+
if hasattr(bp, "is_breakpoint_call") and not bp.is_breakpoint_call:
512+
# print(f"XXX0 {bp} needs tracing")
513+
return False
514+
# print(f"XXX does not need tracing")
515+
return True
503516

504517
def last(self):
505518
return len(self.bpbynumber) - 1

‎trepan/lib/core.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import sys
3131
import threading
3232
from types import FrameType
33-
from typing import Any, Dict, NewType, Optional
33+
from typing import Any, Optional
3434

3535
# External packages
3636
import pyficache
@@ -45,9 +45,7 @@
4545
from trepan.processor.cmdproc import CommandProcessor
4646
from trepan.processor.trace import PrintProcessor
4747

48-
InitOptions = NewType("InitOptions", Dict[str, Any])
49-
50-
DEFAULT_INIT_OPTS: InitOptions = {
48+
DEFAULT_INIT_OPTS: dict[str, Any] = {
5149
"processor": None,
5250
# How many step events to skip before
5351
# entering event processor? Zero (0) means stop at the next one.
@@ -58,7 +56,7 @@
5856

5957

6058
class TrepanCore:
61-
def __init__(self, debugger, opts: InitOptions = DEFAULT_INIT_OPTS):
59+
def __init__(self, debugger, opts = DEFAULT_INIT_OPTS):
6260
"""Create a debugger object. But depending on the value of
6361
key 'start' inside hash `opts', we may or may not initially
6462
start tracing events (i.e. enter the debugger).
@@ -232,7 +230,7 @@ def remove_ignore(self, frame_or_fn):
232230
be debugged"""
233231
return self.ignore_filter.remove(frame_or_fn)
234232

235-
def start(self, opts: InitOptions = DEFAULT_INIT_OPTS):
233+
def start(self, opts = DEFAULT_INIT_OPTS):
236234
"""We've already created a debugger object, but here we start
237235
debugging in earnest. We can also turn off debugging (but have
238236
the hooks suspended or not) using 'stop'.

‎trepan/processor/cmdproc.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def process_commands(self):
592592
pass
593593
run_hooks(self, self.postcmd_hooks)
594594
if self.fast_continue:
595-
if self.core.bpmgr.needs_no_tracing:
595+
if self.core.bpmgr.needs_no_tracing():
596596
# Remove tracing on frames and remove trace hook.
597597
frame = self.curframe
598598
while frame:

‎trepan/processor/command/info_subcmd/breakpoints.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def bpprint(self, bp):
7878
else:
7979
column_str = ""
8080
brkpt_type = "breakpoint()" if bp.is_breakpoint_call else "breakpoint"
81-
if bp.offset is None:
81+
if bp.offset == -1:
82+
# We have a line or line/number offset, so any offset matches.
8283
self.msg(
8384
"%-4d%-12s %s any at %s:%d%s"
8485
% (

‎trepan/processor/command/run.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class RunCommand(DebuggerCommand):
3939
DebuggerCommand.setup(locals(), category="support", max_args=0)
4040

4141
def run(self, args):
42+
# The below is a hack.
43+
# If for some reason we trace into this code,
44+
# setting "execution_status" to "Running" will allow
45+
# us to step or continue runningt the code.
46+
self.core.execution_status = "Running"
4247
confirmed = False
4348
if len(args) <= 1:
4449
if "!" != args[0][-1]:

0 commit comments

Comments
 (0)