NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.06k stars 14.1k forks source link

Getting Python Extensions to GDB #48809

Open CMCDragonkai opened 6 years ago

CMCDragonkai commented 6 years ago

Issue description

GDB supports python extensions: https://devguide.python.org/gdb/

This is not the same as the python support, but this allows access to commands like py-bt.

This means you can actually stacktrace and inspect the Python application source code, not just the Python interpreter.

However there's no information on how to get this in NixOS. The gdb derivation doesn't seem to have any information on this.

CMCDragonkai commented 6 years ago

I want to do something like this: https://web.archive.org/web/20180531125031/http://grapsus.net/blog/post/Low-level-Python-debugging-with-GDB

deliciouslytyped commented 5 years ago

I was able to get this to work by manually sourcing in gdb the appropriate python file contained in the cpython repository (search for "py-bt"), along with a debug build of python (if you don't do that you'll get some sort of message about missing information, I can't remember). I'm leaving a comment so that I come back to this eventually.

For the debug python there was a bit of a mess with dealign with disallowedReferences, but I haven't spent time to figure out how to clean up what I have.

deliciouslytyped commented 4 years ago

I'm going to package this somehow at some point, but here is the link because I always forget how to get there - despite my note above. https://github.com/python/cpython/blob/7bf069b6110278102c8f4719975a5eb5a5af25f9/Tools/gdb/libpython.py

Note also it needs a python with debug symbols but nix-shell -p "(enableDebugging python37)" or such, works. It's so good...

[LWP 22238 exited]

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007fffeca0919d in clang_getCursorTLSKind () from /nix/store/bwnavb2jpbxz3nzkqa7pjjflfb4261hr-clang-10.0.0-lib/lib/libclang.so
(gdb) py-bt
Traceback (most recent call first):
  File "bindings/python/clang/cindex.py", line 1593, in tls_kind
    self._tls_kind = conf.lib.clang_getCursorTLSKind(self)
  <built-in method getattr of module object at remote 0x7fffefe07d10>
  File "pyclasvi.py", line 968, in _add_attr
    attrData = getattr(obj, attrName)
  File "pyclasvi.py", line 1132, in _add_obj
    res = self._add_attr(objStack, attrName, subFoldNode)
  File "pyclasvi.py", line 1150, in set_cursor
    self._add_obj([c], self.foldTree.get_root())
  File "pyclasvi.py", line 1549, in _set_active_cursor
    self.cursorOutputFrame.set_cursor(cursor)
  File "pyclasvi.py", line 1539, in _on_cursor_selection
    self._set_active_cursor(curCursor)
  File "pyclasvi.py", line 507, in _on_selection
    self.selectCmd()
  File "/nix/store/85a6mbc69gdvk65h0k6zq92snv4c4dqw-python3-3.7.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/nix/store/85a6mbc69gdvk65h0k6zq92snv4c4dqw-python3-3.7.7/lib/python3.7/tkinter/__init__.py", line 1283, in mainloop
    self.tk.mainloop(n)
  File "pyclasvi.py", line 1808, in main
    app.mainloop()
  File "pyclasvi.py", line 1811, in <module>
    main()
deliciouslytyped commented 4 years ago

Here's an expression

{ pkgs ? import <nixpkgs> {}
, _gdb ? pkgs.gdb
, python3 ? pkgs.python3
, enableDebugging ? pkgs.enableDebugging
, makeWrapper ? pkgs.makeWrapper
, runCommand ? pkgs.runCommand
}: rec {
  unpackSrc = drv: runCommand "${drv.name}-unpacked" { inherit (drv) src; } ''
    unpackPhase
    mv "$sourceRoot" "$out"
    '';

  debugpy = let
      self = enableDebugging (
        #output '/nix/store/fip9hficzysnpa4g3nkykp0p06bfm33v-python3-3.7.6' is not allowed to refer to the following paths:
        #  /nix/store/bs24q7v5hzg92zq5l56r7yhnp5ljzjv0-openssl-1.1.1d-dev
        python3.overrideAttrs (old: { disallowedReferences = []; }) #TODO bit of a hack? debug symbols or something probably ends up referring to openssl
        );
    in
      python3.override { inherit self; };

  gdb = runCommand "gdb-withpy" { buildInputs = [ makeWrapper ]; } ''
    mkdir -p "$out/bin"
    makeWrapper "${_gdb}/bin/gdb" "$out/bin/gdb" \
      --add-flags '-ex "source ${unpackSrc debugpy}/Tools/gdb/libpython.py"'
    '';

  }
stale[bot] commented 4 years ago

Hello, I'm a bot and I thank you in the name of the community for opening this issue.

To help our human contributors focus on the most-relevant reports, I check up on old issues to see if they're still relevant. This issue has had no activity for 180 days, and so I marked it as stale, but you can rest assured it will never be closed by a non-human.

The community would appreciate your effort in checking if the issue is still valid. If it isn't, please close it.

If the issue persists, and you'd like to remove the stale label, you simply need to leave a comment. Your comment can be as simple as "still important to me". If you'd like it to get more attention, you can ask for help by searching for maintainers and people that previously touched related code and @ mention them in a comment. You can use Git blame or GitHub's web interface on the relevant files to find them.

Lastly, you can always ask for help at our Discourse Forum or at #nixos' IRC channel.

spacekitteh commented 4 years ago

Still valid.

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

deliciouslytyped commented 3 years ago

I might make a PR, but I'm not sure in what form https://github.com/NixOS/nixpkgs/issues/48809#issuecomment-614224176 would be PR-able.

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

vlaci commented 2 years ago

For me just adding the extension from the python source worked without the need of debug build of Python on unstable.

luochen1990 commented 2 months ago

Still needed

luochen1990 commented 2 months ago

@deliciouslytyped Hello, could you please make a PR? so users can use it like gdb.overrideAttrs { pythonSupport = true; } or similar way.