Updated Debugging segfaults and hard to decipher pybind11 bugs (markdown)

Eric Cousineau 2021-03-04 17:24:55 -05:00
parent cf07724242
commit 02e34745d4

@ -5,6 +5,7 @@ This is meant to aide in debugging hard-to-decipher pybind11 bugs (or surprises
Generally, it's easiest to get a sense of where things are failing by using the `trace` module. Here's some code that can be copied+pasted. For examples here, assume these functions are defined in `debug.py` (Python 3.5+ only): Generally, it's easiest to get a sense of where things are failing by using the `trace` module. Here's some code that can be copied+pasted. For examples here, assume these functions are defined in `debug.py` (Python 3.5+ only):
```py ```py
# -*- coding: utf-8 -*-
""" """
Utilities that should be synchronized with: Utilities that should be synchronized with:
https://drake.mit.edu/python_bindings.html#debugging-with-the-python-bindings https://drake.mit.edu/python_bindings.html#debugging-with-the-python-bindings
@ -16,24 +17,24 @@ def reexecute_if_unbuffered():
ONLY use this at your entrypoint. Otherwise, you may have code be ONLY use this at your entrypoint. Otherwise, you may have code be
re-executed that will clutter your console.""" re-executed that will clutter your console."""
import os import os
import shlex
import sys import sys
if os.environ.get("PYTHONUNBUFFERED") in (None, ""): if os.environ.get("PYTHONUNBUFFERED") in (None, ""):
os.environ["PYTHONUNBUFFERED"] = "1" os.environ["PYTHONUNBUFFERED"] = "1"
argv = list(sys.argv) argv = list(sys.argv)
if argv[0] != sys.executable: if argv[0] != sys.executable:
argv.insert(0, sys.executable) argv.insert(0, sys.executable)
cmd = " ".join([shlex.quote(arg) for arg in argv])
sys.stdout.flush() sys.stdout.flush()
os.execv(argv[0], argv) os.execv(argv[0], argv)
def traced(func, ignoredirs=None): def traced(func, ignoredirs=None):
"""Decorates func such that its execution is traced, but filters out any """Decorates func such that its execution is traced, but filters out any
Python code outside of the system prefix.""" Python code outside of the system prefix."""
import functools import functools
import sys import sys
import trace import trace
if ignoredirs is None: if ignoredirs is None:
ignoredirs = ["/usr", sys.prefix] ignoredirs = ["/usr", sys.prefix]
tracer = trace.Trace(trace=1, count=0, ignoredirs=ignoredirs) tracer = trace.Trace(trace=1, count=0, ignoredirs=ignoredirs)