So far, I’ve written seven posts about my IronPython debugger, but frankly it isn’t very functional yet. It runs, breaks on the first line and can show a stack trace. Not exactly Jolt award material. In this post, I’m going to add one of the core functions of any debugger: stepping. Where previously I’ve written a bunch of code but had little to show in terms of features, now I’m getting three new features (basic step, step in and step out) at once!
def _input(self): #remaining _input code omitted for clarity elif k.Key == ConsoleKey.S: print "nStepping" self._do_step(False) return elif k.Key == ConsoleKey.I: print "nStepping In" self._do_step(True) return elif k.Key == ConsoleKey.O: print "nStepping Out" stepper = create_stepper(self.active_thread) stepper.StepOut() def _do_step(self, step_in): stepper = create_stepper(self.active_thread) mod = self.active_thread.ActiveFrame.Function.Module if mod not in self.symbol_readers: stepper.Step(step_in) else: range = get_step_ranges(self.active_thread, self.symbol_readers[mod]) stepper.StepRange(step_in, range)
Here you can see the _input clauses for step, step in and step out. Of the three, step out is the simplest to implement: create the stepper object and call StepOut. For step and step in, I could simply call Step (the boolean argument indicates if you want to step into or over functions) but that only steps a single IL statement. The vast majority of the time there are multiple IL instructions for every line of source code, so IL statement stepping is very tedious. As we learned when setting a breakpoint, debug symbols contain sequence points that map between source and IL locations. If they’re available, I use the sequence points to determine the range of IL statements to step over so that I can step single source statements instead.
The stepping code above depends on three helper functions defined at global scope.
def create_stepper(thread): stepper = thread.ActiveFrame.CreateStepper() stepper.SetUnmappedStopMask(CorDebugUnmappedStop.STOP_NONE) return stepper def create_step_range(start, end): range = Array.CreateInstance(COR_DEBUG_STEP_RANGE, 1) range = COR_DEBUG_STEP_RANGE(startOffset = UInt32(start), endOffset = UInt32(end)) return range def get_step_ranges(thread, reader): frame = thread.ActiveFrame offset, mapResult = frame.GetIP() method = reader.GetMethod(SymbolToken(frame.FunctionToken)) for sp in get_sequence_points(method): if sp.offset > offset: return create_step_range(offset, sp.offset) return create_step_range(offset, frame.Function.ILCode.Size)
The first function, create_stepper, simply constructs and configures the stepper object. The call to SetUnmappedStopMask tells the debugger not to stop if it encounters code that can’t be mapped to IL. If you need to debug at that level, ipydbg is *not* for you.
Next is create_step_range, which exists purely for .NET interop purposes. There are three interop warts hidden in this function. First is creating a .NET array of COR_DEBUG_STEP_RANGE structs. Every time I write Array code like this, I wish for a CreateFromCollection static method on Array. However, in this case it isn’t that big a deal since it’s a one element array. Second wart is having to set the values of COR_DEBUG_STEP_RANGE via constructor keyword arguments. It turns out that IronPython disallows direct updates to value type fields (read this for the reason why). Instead, I pass in the field values into the constructor as keyword arguments. Finally, you have to explicitly convert the start and end offsets to a unsigned int in order to set the offset fields in the COR_DEBUG_STEP_RANGE struct constructor.
Finally is get_step_ranges, which iterates thru the list of sequence points in the current method looking for the one with the smallest offset that is larger than the current offset position. If it can’t find a matching sequence point, it sets the range to the end of the current function. The start range offset is always the current offset. I did make a significant change to get_sequence_points – it no longer yields sequence points that have a start line of 0xfeefee. By convention, that indicates a sequence point to be skipped. Originally, the logic to ignore 0xfeefee sequence points was in get_location. But when I originally wrote get_step_ranges, it had essentially the same sequence point skipping logic, so I moved it to get_location instead.
Technically, I’ve built three new features but the reality is that if you end up in IronPython infrastructure code it’s really hard to find your way back to python code. Step in is particularly useless right now. Luckily, the .NET debugger API supports a feature called “Just My Code” that will make stepping much more useful. In the meantime, the latest version of ipydbg is up on GitHub as usual.