首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nixos:如何获得包含在包中的调试信息的python?

Nixos:如何获得包含在包中的调试信息的python?
EN

Stack Overflow用户
提问于 2018-07-13 21:49:00
回答 2查看 1.1K关注 0票数 10

我正试图在NixOS上进行cython调试。我可以很容易地在nix中安装cython (为示例的简单性而选择),如下所示:

代码语言:javascript
复制
$ nix-shell -p 'python27.withPackages( p: [ p.cython ])'

$ cat /nix/store/s0w3phb2saixi0a9bzk8pjbczjaz8d7r-python-2.7.14-env/bin/python
#! /nix/store/jgw8hxx7wzkyhb2dr9hwsd9h2caaasdc-bash-4.4-p12/bin/bash -e
export PYTHONHOME="/nix/store/s0w3phb2saixi0a9bzk8pjbczjaz8d7r-python-2.7.14-env"
export PYTHONNOUSERSITE="true"
exec "/nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python"  "${extraFlagsArray[@]}" "$@"

然后我们对python做一个普通的nix,看看我们得到了哪个版本的python。

代码语言:javascript
复制
[henry@bollum:~/Projects/eyeserver/nixshell]$ nix-shell -p 'python27'
$ which python
/nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python
#          ^^^^^^^
#          non-debug

一切都很好--在这两种情况下,我们都得到了一个非调试的python。如果我们使用gdb,我们会得到(没有找到调试符号)。看最后一行。

代码语言:javascript
复制
$ gdb 
/nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python...(no debugging symbols found)...done.

当我们在python上单独使用enableDebugging时,我们得到了不同的结果。$ nix-shell -p 'enableDebugging python27‘

代码语言:javascript
复制
$ which python
/nix/store/a4wd8mcpqr54hmw0x95fw8fhvk8avh5a-python-2.7.14/bin/python
#          ^^^^^^
#          debug

$ gdb 
/nix/store/a4wd8mcpqr54hmw0x95fw8fhvk8avh5a-python-2.7.14/bin/python
GNU gdb (GDB) 8.0.1
...
Reading symbols from /nix/store/a4wd8mcpqr54hmw0x95fw8fhvk8avh5a-python-2.7.14/bin/python...done.

当我们尝试使用包括cython (或任何其他包)时,就会出现问题。

代码语言:javascript
复制
 $ nix-shell -p '(enableDebugging python27).withPackages( p: [ p.cython ])'
代码语言:javascript
复制
$ cat `which python`
#! /nix/store/jgw8hxx7wzkyhb2dr9hwsd9h2caaasdc-bash-4.4-p12/bin/bash -e
export PYTHONHOME="/nix/store/s0w3phb2saixi0a9bzk8pjbczjaz8d7r-python-2.7.14-env"
export PYTHONNOUSERSITE="true"
exec "/nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python"  "${extraFlagsArray[@]}" "$@"
#                ^^^^^^^
#                non-debug
$ gdb /nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python
GNU gdb (GDB) 8.0.1
...
Reading symbols from /nix/store/i3bx1iw2d0i3vh9sa1nf92ynlrw324w8-python-2.7.14/bin/python...(no debugging symbols found)...done.
(gdb) 
quit

环境中的python版本现在是不调试的,试图调试它会让人感到恐惧(没有找到调试符号)。这使得gdb对于调试cython程序的用处要小得多。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-19 18:17:07

解决方案

代码语言:javascript
复制
with import <nixpkgs> {};

let
   self = enableDebugging python;
in [ gdb ((python.override{inherit self;}).withPackages(ps: with ps; [ cython ])) ]

包装器中引用的可执行文件现在具有调试符号。

背景

如果我们查看pkgs/all-packages.nix,就会看到enableDebugging函数的实现:

代码语言:javascript
复制
enableDebugging = pkg: pkg.override { stdenv = stdenvAdapters.keepDebugInfo pkg.stdenv; };

它重写一个派生来使用不同的stdenv。在您的示例中,您希望重写Python解释器,它是python.withPackages获得的派生的依赖项。

您对enableDebugging python的尝试是正确的,但是,python.withPackages使用了对python的引用,该引用也需要更新。

票数 5
EN

Stack Overflow用户

发布于 2018-10-22 07:21:27

@fridh答案需要额外的少量代码才能使其可用。

基本上,您需要一个包含以下内容的shell.nix

代码语言:javascript
复制
with import <nixpkgs> {};

let
   python = enableDebugging pkgs.python;
in
  stdenv.mkDerivation {
    name = "test";
    buildInputs = [ python ];
  }

然后,当您输入nix-shell时,您可以运行gdb -p <pid of python process>

请注意,这只会调试Python解释器本身。这不启用gdb的python扩展,允许您调试应用程序级别的python代码。

enableDebugging也不会传播到依赖项。如果要启用这些调试符号,每个依赖项都需要自己的enableDebugging。是否应该有一个函数来递归地启用调试?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51333232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档