Skip to content

Various debugging scenarios of embedded CPython

denfromufa edited this page Jul 5, 2017 · 25 revisions

These 3 debugging scenarios below are based on 2 articles from PTVS docs:

https://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-mode https://docs.microsoft.com/en-us/visualstudio/python/debugging-cross-platform-remote

Important pre-requisites:

  • Install CPython with debugging symbols and binaries, like described in PTVS docs

  • Enable native code debugging in Visual Studio project settings

  • Python.NET 2.3.0+ and Visual Studio 2017, ptvsd 3.1.0+, Python 3.5+ are tested for these 3 scenarios

  1. Attach mixed-mode cross-language debugger (Python, Managed, Native) to pythonnet:
  • switch to DEBUG=1.
  • select Managed (4.0+) + Native + Python debug engines manually.

  1. Attach Python debugger engine to Python code embedded with pythonnet:
  • switch to DEBUG=2.
  • select Python debug engine manually.

  1. Attach Python remote debugger (ptvsd) to Python code embedded with pythonnet:
  • switch to DEBUG=3.
  • select Python Remote Debugging option (ptvsd) in Connection Type.
  • specify transport string in Connection target: tcp://localhost@clr:5678

Program.cs

using System;
using Python.Runtime;
using System.Diagnostics;
using System.Threading;

namespace mixedmode
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic res;
            using (Py.GIL()) {
                Console.WriteLine("embedded Python engine");
                dynamic pymod = Py.Import("mixedmode");
                Console.WriteLine("Imported testing module");
                if ((int)pymod.DEBUG == 1)
                {
                    AttachDebugger(); // enable this for mixed-mode debugging
                }
                res = pymod.pyfunc();
            }
            Console.WriteLine(res);
        }
        static void AttachDebugger()
        {
            Console.WriteLine("waiting for .NET debugger to attach");
            while (!Debugger.IsAttached)
            {
                Thread.Sleep(100);
            }
            Console.WriteLine(".NET debugger is attached");

        }
    }
}

mixedmode.py

DEBUG=2 # 1 - mixed-mode cross-language debugger
        # 2 - attach Python debugger
        # 3 - ptvsd remote debugging

import clr

if DEBUG==2:
    import sys
    import time
    while not sys.gettrace():
        time.sleep(0.1)

elif DEBUG==3:
    import ptvsd
    ptvsd.enable_attach('clr')
    ptvsd.wait_for_attach()

def pyfunc(args=None):
    if args:
        return args
    else:
        return "testing"