Re: Debugging of native extensions on windows

Rokas Kupstys <[email protected]> Tue, 14 Mar 2023 09:13:08 +0200
Newsgroups gmane.comp.python.devel
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============2448879490954855584==
Content-Type: multipart/alternative;
 boundary="------------Q3bz73k0G00ZqTqATAPGtRkO"
Content-Language: en-US

This is a multi-part message in MIME format.
--------------Q3bz73k0G00ZqTqATAPGtRkO
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

As Steve suggested i think most friction-less path is to run python 
interpreter directly while specifying site-packages of virtualenv in 
PYTHONPATH. I already specify additional paths there anyway, since 
extensions are built with cmake and i wanted to achieve fast iteration 
times, being able to use extensions directly built by cmake, without 
going through installation problem.

Still, i think there can be an improvement in this area, and it would 
likely be quite cheap. The biggest problem is people being unaware what 
is going on. IsDebuggerPresent()/CheckRemoteDebuggerPresent() could be 
used for checking debugger presence and when debugging state of main 
process and child process do not match, launcher could print some link 
to documentation describing what is going on and how situation could be 
solved. I am just not sure about any possible race conditions (no idea 
how fast debuggers attack to child processes).

-- Rokas Kupstys

On 2023-03-14 08:35, Christopher Barker wrote:
> Is it easier to simply run python outside a virtualenv? They are 
> great, but maybe when debugging an extension module, it's not so hard 
> to just not use it :-)
>
> You also might want to give conda environments a try -- they include 
> Python, so probably won't have the same issue.
>
> -CHB
>
>
>
> On Mon, Mar 13, 2023 at 4:58 PM Steve Dower <[email protected]> 
> wrote:
>
>     Hi Rokas
>
>     The typical solution (which I myself use frequently) is to enable
>     your
>     debugger to attach to child processes automatically. It can make
>     things
>     a bit noisier, but it's generally manageable, especially if you've
>     got
>     breakpoints set in your own code.
>
>     Another option is to not use the virtual environment, but set
>     PYTHONPATH
>     to your environment's Lib\site-packages directory and then run the
>     base
>     interpreter directly. Most of the time, this will be identical,
>     but it
>     avoids the extra process.
>
>     Unfortunately, for a variety of reasons, we can't get away from the
>     redirector process as long as virtual environments keep their current
>     design. As changing the design would be just as disruptive, we've not
>     done anything yet, nor do we have any plans to change anything.
>
>     Finally, most discussion about Python occurs at
>     https://discuss.python.org/ these days. You'll likely find more
>     help and
>     discussion over there.
>
>     Cheers,
>     Steve
>
>     On 3/13/2023 3:25 PM, Rokas Kupstys wrote:
>     > Hello!
>     >
>     > I am dropping this mail to bring up an issue which cost me three
>     good
>     > evenings of time. Now that i figured it out i believe it is quite a
>     > serious usability problem.
>     >
>     > Gist of the problem: i have some C++ code wrapped with SWIG, so
>     a native
>     > extension. As with all software - there was a bug. However, no
>     matter
>     > what i did - i could not debug it in a native debugger. I ran
>     > ".venv/Scripts/python.exe script.py" in a native debugger and
>     > breakpoints would not be hit, application would crash
>     eventually. This
>     > was especially confusing, because exact same setup worked just
>     fine on
>     > linux. I eventually stumbled on to process list showing
>     > ".venv/Scripts/python.exe" having spawned a subprocess... Which
>     led me
>     > to "PC/launcher.c" which is what ".venv/Scripts/python.exe"
>     really is. I
>     > cant find much information about this behavior in documentation
>     even
>     > after the fact. All in all, this was quite confusing. So now
>     every time
>     > i want to debug a native extension i have to start a program and
>     then
>     > attach a debugger to it, instead of just hitting "Debug" button
>     in IDE.
>     > It gets worse if crash happens immediately, which means i have
>     to resort
>     > to things like adding a message box somewhere to block execution
>     and
>     > give me enough time to attach the debugger. It works in the end,
>     but
>     > user experience is really not great. But whats worse - this is
>     such a
>     > non-obvious behavior that many more people may trip on it and waste
>     > their time. Documenting this behavior would be of little help
>     too, as
>     > there is no clear path from the issue to the documentation on
>     the matter...
>     >
>     > So there it is. I am sure it is the way it is for a good reason.
>     > However, this is a very error-prone process which is likely to
>     waste
>     > people's time. So maybe this behavior could be reconsidered? Or
>     maybe
>     > there is a solution already, which escaped me?
>     >
>
>     _______________________________________________
>     Python-Dev mailing list -- [email protected]
>     To unsubscribe send an email to [email protected]
>     https://mail.python.org/mailman3/lists/python-dev.python.org/
>     Message archived at
>     https://mail.python.org/archives/list/[email protected]/message/424LIHYHRQWM5VLQF2PAMLKGCNCKSJDF/
>     Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
> -- 
> Christopher Barker, PhD (Chris)
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
--------------Q3bz73k0G00ZqTqATAPGtRkO
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>As Steve suggested i think most friction-less path is to run
      python interpreter directly while specifying site-packages of
      virtualenv in PYTHONPATH. I already specify additional paths there
      anyway, since extensions are built with cmake and i wanted to
      achieve fast iteration times, being able to use extensions
      directly built by cmake, without going through installation
      problem.</p>
    <p>Still, i think there can be an improvement in this area, and it
      would likely be quite cheap. The biggest problem is people being
      unaware what is going on.
      IsDebuggerPresent()/CheckRemoteDebuggerPresent() could be used for
      checking debugger presence and when debugging state of main
      process and child process do not match, launcher could print some
      link to documentation describing what is going on and how
      situation could be solved. I am just not sure about any possible
      race conditions (no idea how fast debuggers attack to child
      processes).<br>
    </p>
    <pre class="moz-signature" cols="72">-- Rokas Kupstys</pre>
    <div class="moz-cite-prefix">On 2023-03-14 08:35, Christopher Barker
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CALn7ch-ad-x1i72r4OGOPrHs24cAJPAsUL7+t=0B2hiG0zq7Fw@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">Is it easier to simply run python outside a
        virtualenv? They are great, but maybe when debugging an
        extension module, it's not so hard to just not use it :-)
        <div><br>
        </div>
        <div>You also might want to give conda environments a try --
          they include Python, so probably won't have the same issue.</div>
        <div><br>
        </div>
        <div>-CHB</div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">On Mon, Mar 13, 2023 at
          4:58 PM Steve Dower &lt;<a
            href="mailto:[email protected]" moz-do-not-send="true"
            class="moz-txt-link-freetext">[email protected]</a>&gt;
          wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px
          0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi
          Rokas<br>
          <br>
          The typical solution (which I myself use frequently) is to
          enable your <br>
          debugger to attach to child processes automatically. It can
          make things <br>
          a bit noisier, but it's generally manageable, especially if
          you've got <br>
          breakpoints set in your own code.<br>
          <br>
          Another option is to not use the virtual environment, but set
          PYTHONPATH <br>
          to your environment's Lib\site-packages directory and then run
          the base <br>
          interpreter directly. Most of the time, this will be
          identical, but it <br>
          avoids the extra process.<br>
          <br>
          Unfortunately, for a variety of reasons, we can't get away
          from the <br>
          redirector process as long as virtual environments keep their
          current <br>
          design. As changing the design would be just as disruptive,
          we've not <br>
          done anything yet, nor do we have any plans to change
          anything.<br>
          <br>
          Finally, most discussion about Python occurs at <br>
          <a href="https://discuss.python.org/" rel="noreferrer"
            target="_blank" moz-do-not-send="true"
            class="moz-txt-link-freetext">https://discuss.python.org/</a>
          these days. You'll likely find more help and <br>
          discussion over there.<br>
          <br>
          Cheers,<br>
          Steve<br>
          <br>
          On 3/13/2023 3:25 PM, Rokas Kupstys wrote:<br>
          &gt; Hello!<br>
          &gt; <br>
          &gt; I am dropping this mail to bring up an issue which cost
          me three good <br>
          &gt; evenings of time. Now that i figured it out i believe it
          is quite a <br>
          &gt; serious usability problem.<br>
          &gt; <br>
          &gt; Gist of the problem: i have some C++ code wrapped with
          SWIG, so a native <br>
          &gt; extension. As with all software - there was a bug.
          However, no matter <br>
          &gt; what i did - i could not debug it in a native debugger. I
          ran <br>
          &gt; ".venv/Scripts/python.exe script.py" in a native debugger
          and <br>
          &gt; breakpoints would not be hit, application would crash
          eventually. This <br>
          &gt; was especially confusing, because exact same setup worked
          just fine on <br>
          &gt; linux. I eventually stumbled on to process list showing <br>
          &gt; ".venv/Scripts/python.exe" having spawned a subprocess...
          Which led me <br>
          &gt; to "PC/launcher.c" which is what
          ".venv/Scripts/python.exe" really is. I <br>
          &gt; cant find much information about this behavior in
          documentation even <br>
          &gt; after the fact. All in all, this was quite confusing. So
          now every time <br>
          &gt; i want to debug a native extension i have to start a
          program and then <br>
          &gt; attach a debugger to it, instead of just hitting "Debug"
          button in IDE. <br>
          &gt; It gets worse if crash happens immediately, which means i
          have to resort <br>
          &gt; to things like adding a message box somewhere to block
          execution and <br>
          &gt; give me enough time to attach the debugger. It works in
          the end, but <br>
          &gt; user experience is really not great. But whats worse -
          this is such a <br>
          &gt; non-obvious behavior that many more people may trip on it
          and waste <br>
          &gt; their time. Documenting this behavior would be of little
          help too, as <br>
          &gt; there is no clear path from the issue to the
          documentation on the matter...<br>
          &gt; <br>
          &gt; So there it is. I am sure it is the way it is for a good
          reason. <br>
          &gt; However, this is a very error-prone process which is
          likely to waste <br>
          &gt; people's time. So maybe this behavior could be
          reconsidered? Or maybe <br>
          &gt; there is a solution already, which escaped me?<br>
          &gt; <br>
          <br>
          _______________________________________________<br>
          Python-Dev mailing list -- <a
            href="mailto:[email protected]" target="_blank"
            moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a><br>
          To unsubscribe send an email to <a
            href="mailto:[email protected]" target="_blank"
            moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a><br>
          <a
            href="https://mail.python.org/mailman3/lists/python-dev.python.org/"
            rel="noreferrer" target="_blank" moz-do-not-send="true"
            class="moz-txt-link-freetext">https://mail.python.org/mailman3/lists/python-dev.python.org/</a><br>
          Message archived at <a
href="https://mail.python.org/archives/list/[email protected]/message/424LIHYHRQWM5VLQF2PAMLKGCNCKSJDF/"
            rel="noreferrer" target="_blank" moz-do-not-send="true"
            class="moz-txt-link-freetext">https://mail.python.org/archives/list/[email protected]/message/424LIHYHRQWM5VLQF2PAMLKGCNCKSJDF/</a><br>
          Code of Conduct: <a
            href="http://python.org/psf/codeofconduct/" rel="noreferrer"
            target="_blank" moz-do-not-send="true"
            class="moz-txt-link-freetext">http://python.org/psf/codeofconduct/</a><br>
        </blockquote>
      </div>
      <br clear="all">
      <div><br>
      </div>
      <span class="gmail_signature_prefix">-- </span><br>
      <div dir="ltr" class="gmail_signature">
        <div dir="ltr">Christopher Barker, PhD (Chris)<br>
          <br>
          Python Language Consulting<br>
            - Teaching<br>
            - Scientific Software Development<br>
            - Desktop GUI and Web Development<br>
            - wxPython, numpy, scipy, Cython<br>
        </div>
      </div>
    </blockquote>
  </body>
</html>

--------------Q3bz73k0G00ZqTqATAPGtRkO--

--===============2448879490954855584==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline