Setting breakpoints dynamically at runtime in Java

2019-05-24 16:46发布

问题:

Both firebug and the built in console in webkit browsers make it possible to set breakpoints in running Javascript code, so you can debug it as you would with any other language.

What I'm wondering is if there is any way that I can instruct firebug or webkit that I'd like to set a breakpoint on line X in file Y at runtime, and to be able to examine variables in the specific scope that I have paused in.

I need something that can work in both Chrome (or any other webkit browser) and Firefox. For the latter Firebug is an acceptable dependency. Supporting IE is not a requirement.

I've been building an in-browser IDE ( quick video for the interested: http://www.youtube.com/watch?v=c5lGwqi8L_g ) and want to give it a bit more meat.

One thing I did try was just adding debugger; as an extra line where users set them, but this isn't really an ideal solution.

回答1:

I'd say you can definitely do this for webkit browsers using the remote debugging protocol. This is based on a websocket connection and a json message protocol that goes back and forth.

You can read the announcement and the whole protocol schema.

Chrome also offers more information about this inside its remote developer-tools docs.

For the debugger domain, for instance, you can see how you can use Debugger.setBreakpoint, Debugger.setBreakpointByUrl and Debugger.setBreakpointsActive to work with breakpoints.

On the other hand, Mozilla also seems to be working on this as you can see in https://developer.mozilla.org/en-US/docs/Tools/Debugger-API and https://wiki.mozilla.org/Remote_Debugging_Protocol though I don't know the completion status of it.

In this case, you can work with breakpoints using the Debugger.Script APIs setBreakPoint, getBreakPoint, getBreakpoints, clearBreakpoints and clearAllBreakpoints

I hope this helps you move forward.



回答2:

There isn't such a thing, at least not using the public, scriptable side of JavaScript. It would be possible if you have a privileged browser extension that could do that for you. For example, Firebug has a debug method which you can call from its command line, but not from scripts inside a page.

So, you have two solutions:

  1. Implement your own JavaScript interpreter, which you can control as you wish. Might be a bit too ambitious, though...
  2. Rely on a browser extension that can set breakpoints anywhere in the code, expose some API to public code, and interact with it from your JavaScript. But that means that users will have to install some extra piece of software before they can use your "Web IDE".


回答3:

Use _defineSetter__ to watch variables, and combine it with a call to debugger when an assignment happens.

__defineSetter__("name", function() { debugger; });

or defineProperty:

function setter () { debugger; }

Object.defineProperty(Math, 'name', { set: setter });

References

  • MDN: Object.defineProperty

  • A List Apart: Advanced Debugging With JavaScript

  • JavaScript Getters and Setters