Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging UI and pause when a key is pressed

I'm really no debugging expert and lately I came across a problem which hopefully has an easy solution. When I test and debug the Mathematica plugin for IDEA, I write some code, build it and run it in a sandbox IDEA.

For those who are not familiar with writing plugins for IDEA: The main problem is, that all the UI code is already there because it comes with IDEA. My plugin only implements specific interfaces which are required to make IDEA understand the Mathematica language. Therefore, setting a breakpoint or throwing something in an onClickListener like suggested by @Jeroen is not possible, because I virtually haven't written any single line of UI code*.

Now I had the situation that everything works fine, but when I cancel a specific action, something weird happens. I have no idea which specific method (it is none of mine!) is called at the moment when I press Esc to cancel this action. The current point is very likely deep inside the sources of IDEA which I have and which I can navigate with the debugger!.

Question: What is the easiest way in Java to make the debugger break, wherever it might be when I press Esc in the UI-program I currently debug?

*This is not entirely true, but for the sake of a general solution I would like to think of it that way.

like image 802
halirutan Avatar asked Mar 10 '14 01:03

halirutan


People also ask

How do I pause browser debugging?

We can use the DevTools console to pause our code whenever it hits an exception — which is just a slightly fancier name for an error. Let's return to DevTools. This time, we'll click on the Sources tab. Click the button on the top right that looks like an octagon with a pause button.

Which debug feature is used to pause execution and examine variables?

Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint.

How do I pause debug in Visual Studio?

Pause (suspend or break) execution Set breakpoints in the code that you want to examine and wait until one of them is hit. Break program execution with Ctrl+Alt+Break . The debugger will finish the statement that is executing at the moment you pause, and then stop at the statement that should be executed next.


2 Answers

As discussed in this question, you can create your own subclass of EventQueue which inspects events, and create a place in the code where you can set your break point and only catch certain kinds of events. It might look something like this:

    EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
    eventQueue.push(new EventQueue(){
        public void postEvent(AWTEvent theEvent) {
            if(theEvent instanceof KeyEvent){
                KeyEvent kEvent = (KeyEvent)theEvent;
                if(kEvent.getKeyCode() == KeyEvent.VK_ESCAPE){
                    System.out.println("escape pressed, set breakpoint here");
                }
            }
            super.postEvent(theEvent);
        }
    });

Then when you hit that breakpoint, you can step into the super.postEvent() call and see where it goes, which component(s) receive it, and when they begin to interact with some part of your code. Be forewarned that it may be a long slog, though -- AWT/Swing event dispatching can get pretty complex.

like image 100
Matt McHenry Avatar answered Sep 30 '22 08:09

Matt McHenry


If you are working in (or can port to) Netbeans you can use their visual debugger which is specifically designed for these GUI-centric issues. It can direct you to specific source code when you interact with a GUI element, so it should be able to point you to the mystery code that is run when you click escape. I believe that other IDEs have similar features, but Netbeans is the only one I know of off the top of my head.

like image 27
David says Reinstate Monica Avatar answered Sep 30 '22 09:09

David says Reinstate Monica