Attach jdb on uncaught exception

隐身守侯 提交于 2019-11-30 10:01:41

To attach a debugger just in time does indeed use the options you suggest (launch and onuncaught). But the launch option needs more on linux:

Note that the launched process is not started in its own window. In most cases the launched process should be a small application which in turns launches the debugger application in its own window.

In your case, jdb is failing to open the terminal TTY it needs because of the context it is being started in. Depending on the environment where this is running, you will need to construct a script that launches jdb in a new window or attaches it to a pseduo-tty so that it can run properly.

I tested this by creating a script that uses screen to start the terminal

#!/bin/bash
# according to launch option doc, the first argument is the transport and
# the second argument will be the address
#
screen -dm -L -S autojdb jdb -attach $2

This script will start jdb in a detached screen and name the session for the screen autojdb. You can see the list of your screens using screen -ls. When you want to access the debugger that has been started use screen -r autojdb. Make sure to put the script in your path or give the full path (/home/me/screenjdb below) in your launch option:

java -agentlib:jdwp=transport=dt_socket,address=8000,server=y,onuncaught=y,suspend=n,launch=/home/me/screenjdb MyClass

In my script I also passed -L to screen which logs the session. This will record the session but also let you see any errors that occurred if the attach fails for some reason.

Instead of using a debugger and attaching one remotely, why don't you create an uncaught exception handler using Thread.UncaughtExceptionHandler ?

public class ThreadCatch
{
    public static void main(String... args)
    {
        new ThreadCatch().go();
    }

    public void go()
    {
        try {
            Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    System.out.println("Uncaught exception");
                    e.printStackTrace();
                }
            });

            final Thread thread = new Thread(new A());
            thread.start();
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class A implements Runnable
    {
        int x = 10;

        @Override
        public void run()
        {
            x++;
            ObjectTrackingException obj = new ObjectTrackingException();
            obj.setThrownFrom(this);
            throw obj;
        }
    }
}


class ObjectTrackingException extends RuntimeException
{
    private Object thrownFrom;

    public Object getThrownFrom() {
        return thrownFrom;
    }

    public void setThrownFrom(Object thrownFrom) {
        this.thrownFrom = thrownFrom;
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!