microsoft / PTVS

Python Tools for Visual Studio
https://aka.ms/PTVS
Apache License 2.0
2.53k stars 674 forks source link

Debug IPython kernel #774

Closed zooba closed 3 years ago

zooba commented 9 years ago

We should be able to attach VS to a running IPython kernel, submit code, and debug cells as they execute.

(Note that this will require some work on Jupyter and IPython to support it. Without this work, we will be extremely limited in what we can provide. Notes are included below as to which project needs changes to support the flow.)

Users would obtain some connection info from Jupyter (potential UI changes to Jupyter? - possibly a way to extract/request it from the URL?) and paste it into an interactive window in PTVS.

PTVS would attach to the IPython kernel, without disconnecting the notebook, and start mirroring every interaction. When a cell is executed from the notebook, the code and output are both added to the REPL as a previously executed input and its output. (Some way for Jupyter to recognize output for its own requests?)

Users can execute code against the kernel from the REPL and view the output. This code/output does not appear in the notebook. Users cannot access/modify the notebook contents from this entry point (this feature is about the kernel, not the notebook).

Users can hit a checkbox to enable debugging, either in PTVS or in Jupyter (TBD). This will signal the kernel to begin tracing execution and transmitting debug messages (line, call, return, ...? probably needs a server-side filter and breakpoint handling). The front-ends can transmit debug requests to get information like file contents (full source), directory listing, current stack trace, locals info, value repr, evaluate expression, ... and present this in whatever UI they choose. For PTVS, this will be the usual debugger interface, but Jupyter could also present this debugging information in some way.

When debugging, code submitted through the PTVS REPL automatically breaks at the first line, and users can set breakpoints in any open file from the kernel (initially, they'll probably just have to know the full path if it isn't already open, but we could eventually have a browser).

Potentially users could also set breakpoints in Jupyter that would break in PTVS (depends on Jupyter getting breakpoint UI).

Jupyter is unable to execute cells while a cell is being debugged, though it can still queue them for execution after the current cell completes.

Debug Requests

To implement full-featured debugging via the Jupyter protocol, we'd need to be able to make these requests of kernel. (Feel free to edit directly or comment if you have more suggestions/questions). ('id' means a unique identifier so we can call back into the kernel and identify the item; 'str' means a human-readable representation)

To implement full-featured debugging via the Jupyter protocol, we'd need the kernel to send the following messages when debugging is enabled. Other filters may apply to events, primarily to improve performance while debugging by sending fewer events.

zooba commented 9 years ago

Hi @ellisonbg, @fperez, @takluyver and @minrk (and anyone else you guys would like to call in), here's something we're hoping to do over our next major release, and it's going to need some collaboration. We're obviously willing to pick up a decent chunk of the IPython work to make this possible, but right now we're still at the "what's feasible" stage, so we'd love input like "that's impossible", "we tried already", "already works" etc.

ISTM that the multiple-frontend setup could be problematic (particularly that I think we'd want the PTVS frontend to get all messages but the notebook frontend to only get its own), though I haven't tried to do this yet or even looked at whether it would work.

I'd also like @int19h to pitch in and list any extra debug messages/requests we'd need to make this fully functional in VS.

(The whole point of us putting these sort of plans up publicly is to clarify them, so feel free to ask as many clarifying questions as you like until every little piece is clear.)

ellisonbg commented 9 years ago

We are really interested in improving the debugging support in jupyter. There are other jupyter devs who are looking at this. I am wondering if it would make sense to bring this up on the main jupyter google group so everyone interested could participate.

den-run-ai commented 9 years ago

+1 and should work according to cross-platform debugger model in PTVS, otherwise *nix users will be left out.

den-run-ai commented 9 years ago

and azure :)

zooba commented 9 years ago

@ellisonbg Is that "improving" or "adding"? :)

It's definitely worth bringing up on the group, but I'd hope this is an open enough forum where we can also cross-ref issues and work with the other repos and update the initial post as a plan-of-record (at least I can do the updates). In my experience, mailing list threads have a tendency to die and be forgotten, or worse, refuse to die and become noisy for everyone who isn't interested.

ellisonbg commented 9 years ago

...well we do have extremely simple command line debugging in the python kernel through the %debug magic... ;-)

I agree that eventually it will be more useful to have actual work be on specific repos/issues/PRs, but initially, I want to make sure to post widely to make sure everyone who is interested can participate.

On Thu, Aug 20, 2015 at 10:05 AM, Steve Dower notifications@github.com wrote:

@ellisonbg https://github.com/ellisonbg Is that "improving" or "adding"? :)

It's definitely worth bringing up on the group, but I'd hope this is an open enough forum where we can also cross-ref issues and work with the other repos and update the initial post as a plan-of-record (at least I can do the updates). In my experience, mailing list threads have a tendency to die and be forgotten, or worse, refuse to die and become noisy for everyone who isn't interested.

— Reply to this email directly or view it on GitHub https://github.com/Microsoft/PTVS/issues/774#issuecomment-133077668.

Brian E. Granger Associate Professor of Physics and Data Science Cal Poly State University, San Luis Obispo @ellisonbg on Twitter and GitHub bgranger@calpoly.edu and ellisonbg@gmail.com

zooba commented 9 years ago

I'll make a post to ipython-dev.

ellisonbg commented 9 years ago

Could you post to the jipyter google group instead - I think there are more people there interested in this

Sent from my iPhone

On Aug 20, 2015, at 10:22 AM, Steve Dower notifications@github.com wrote:

I'll make a post to ipython-dev.

— Reply to this email directly or view it on GitHub.

zooba commented 9 years ago

@ellisonbg Done. As long as nobody replies, since I largely ignore the email address I had to use :)

Carreau commented 9 years ago

Subscribing :-) I'll try to ping a few people from Julia.

zooba commented 9 years ago

@Carreau Great! While we won't directly support Julia (at least initially) in VS, we certainly don't want to make it impossible to do so by making poor decisions now.

zooba commented 9 years ago

I made a first attempt at a list of requests and events we'd want from a kernel while stepping, based on what we currently use for debugging Python. Very interested in more suggestions, and more than happy to clarify what we need them for.

SylvainCorlay commented 9 years ago

Also subscribed.

SylvainCorlay commented 9 years ago

@zooba, did you start this work on the list of requests and events somewhere where we can already see it?

zooba commented 9 years ago

@SylvainCorlay Top of the post. It's easier to keep track of what the final plan is by updating the original post (which most of my team can do, so we'll transfer stuff up there as appropriate - not quite as good as a totally open wiki, but I think it'll work out okay).

int19h commented 9 years ago

I've added a few more commands and events.

SylvainCorlay commented 9 years ago

Thanks! A lot of it seems to be about requesting information about the state (loaded modules, values of variables), and are somewhat separated from the debug actions commands related to stepping and breakpoints.

minrk commented 9 years ago

I think it's going to be really hard to do this in a language-agnostic way. There is a reason that we actually eliminated all structural information from the inspection replies, because it wasn't feasible to do it in a way for all languages. Even something as basic as a signature has loads of language-specific features.

zooba commented 9 years ago

@SylvainCorlay Certainly the "evaluate this arbitrary expression" command is kind of already there (and many of the others can be built on top of this), so I guess that really just needs a way to avoid sending the output to every listener (which may exist, I'm not that familiar with the protocol).

It looks like 'inspect' is good at getting the code for a particular class/function? We'd really want to get the entire file and use the IDE formatting. This is probably a simple extension on that, so I'll note it in the description.

@minrk: I agree, but most of what we really need is generic (we plug it into the VS debugger UI, which is also language agnostic). There will certainly be some pieces specific to the type of kernel, which is why (e.g.) Julia support won't be free for all debuggers (once the kernel supports it), but a lot of the language-specific parts are easily handled or ignored by the debugger anyway.

For example, we don't need to do anything special with parameter annotations while debugging - even if they cause an exception on function definition - and they don't even need to appear in a traceback. As long as we can get the original source file the user will be able to see they exist, and if we need to resolve their value then the user will have to somehow indicate that to the debugger and then we just request an expression evaluation..

zooba commented 9 years ago

A basic IPython comm example: https://gist.github.com/jasongrout/64d2548eff5f4cb41298 (Might be useful here)