thinkershare

如何还原一个正在运行中的 Python 的程序代码?

  •  
  •   thinkershare · Apr 18, 2022 · 2314 views
    This topic created in 1495 days ago, the information mentioned may be changed or developed.

    我有一个正在运行中的 PyTorch 代码, 训练了一个神经网络(跑了大概四天了), 后来发现当时运行这的代码和参数都没有备份, 而原始代码被无意间覆盖了, git 也没有正确提交, 导致无法还原出参数了, 正在跑着的结果是最佳的, 我重新再次调试, 无论怎么也无法还原出现在正在训练着的这个结果. 现在这个 python 程序还在运行(非调试模式),大概一天后运行结束.想咨询下是否有办法 dump 整个进程, 然后反编译出原始代码, 程序跑在 Ubuntu 20.04(3090 的单卡, 没有使用分布式), Python 3.9

    5 replies    2022-04-18 13:47:49 +08:00
    ThirdFlame
        1
    ThirdFlame  
       Apr 18, 2022
    找找 pyc 文件
    moyechen
        2
    moyechen  
       Apr 18, 2022   ❤️ 1
    我在之前的编程经历中使用过 gdb 调试 python 程序,通过 python-gdb 扩展可以看到当前执行的行的源代码,不知道是否可以看到全部的源代码。但是可能有风险,导致进程死掉,建议博主自己测试下

    https://wiki.python.org/moin/DebuggingWithGdb
    如果进程已在运行,只要您知道进程 ID ,就可以附加到该进程。
    $ gdb python <pid of running process>
    附加到像这样正在运行的进程将导致它停止。您可以告诉它继续使用 c 命令运行。

    https://sourceware.org/gdb/onlinedocs/gdb/List.html#List
    thinkershare
        3
    thinkershare  
    OP
       Apr 18, 2022
    @moyechen 好些, 我先试一试, pyc 文件也被覆盖掉了, 就比较悲催
    moyechen
        4
    moyechen  
       Apr 18, 2022
    @thinkershare 抱歉,原来这个 gdb 也是打开源文件才能显示代码的。我删除以后显示不了了
    (gdb) py-list
    Unable to open t.py: [Errno 2] No such file or directory: b't.py'
    thinkershare
        5
    thinkershare  
    OP
       Apr 18, 2022
    @moyechen 我现在的想法是先将整个进程 dump 出来, 然后在使用分析工具找到已加载的字节码, 然后看不能不能反编译字节码
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2842 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 01:46 · PVG 09:46 · LAX 18:46 · JFK 21:46
    ♥ Do have faith in what you're doing.