使用java以编程方式与gdb交互

时间:2011-08-18 06:45:42

标签: java

我正在编写一个使用GDB通过java访问信息的应用程序。

使用Runtime.getRuntime.exec,我可以将GDB附加到任何进程。

问题是我无法在启动后向GDB发送输入。

**编辑(2011年8月19日):

在“out.println(gdbcommand)”行,启动gdb。如何获取新生成的gdb的stdout,向其输入输入然后读取stdin。到目前为止,我只能获得输出直到“out.println(gdbcommand)”。尝试以编程方式将输入发送到gdb的所有尝试到目前为止都没有工作。**

请参阅我的问题下方的trojanfoe评论。 以下是我的代码的编辑样本:

try {   
    String gdbcommand = "/data/bin/gdb " + pidS + " " + pidS;
    String line;

    Process process = Runtime.getRuntime().exec("su");

    if (process != null) {
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())),true);

        out.println(gdbcommand);

        //Note this line does not get sent to gdb's interface after starting gdb.
        //Is it possible to connect to the new stdout of gdb's interface?
        out.println("info registers");

        out.flush();
        out.close();

        while ((line = in.readLine()) != null) {
            System.out.println(line);
        } 

        process.destroy();
    }

} catch (Exception e) {
    e.printStackTrace();
}

请注意,“info registers”不会发送到GDB的接口,在调用out.close()之后,gdb的接口退出。 (如下所示)

08-18 14:40:36.595: INFO/System.out(4408): 0xafd0c51c in epoll_wait () from /system/lib/libc.so

08-18 14:40:36.595: INFO/System.out(4408): (gdb) Hangup detected on fd 0

08-18 14:40:36.595: INFO/System.out(4408): error detected on stdin

08-18 14:40:36.595: INFO/System.out(4408): The program is running.  Quit anyway (and detach it)? (y or n) [answered Y; input not from terminal]

5 个答案:

答案 0 :(得分:4)

您需要GDB/MI

  

GDB / MI是基于行的面向机器的GDB文本接口。它是   专门用于支持使用的系统的开发   调试器只是一个较大系统的一个小组件。

尝试使用--interpreter=mi

启动GDB

答案 1 :(得分:2)

好的,如果有人感兴趣,我这样做的方法是使用文件将命令发送到gdb。

           `try {

                String gdbcommand = "/data/bin/gdb  " + pidsII + " " + pidsII + " < somefile.txt";

                String line;

                ArrayList<String> lines = new ArrayList<String>();

                Process process = Runtime.getRuntime().exec("su");

                if (process != null){
                    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())),true);

                    out.println(gdbcommand);

                    out.flush();

                    out.flush();

                    out.close();

                    while ((line = in.readLine()) != null){

                        lines.add(line);

                    } 

                    String[] lineArray = new String[lines.size()];
                    lineArray  = lines.toArray(lineArray);

                    for (int i=0; i < lineArray.length; i++) {
                        System.out.println(lineArray[i].toString());
                    }
                    process.destroy();

                }

            } catch (Exception e) {
                e.printStackTrace();
            }`

其中somefile.txt包含文本信息寄存器。

出于某种原因,不调用out.flush()三次会使输出不显示,因此会出现冲刷的冲击。

答案 2 :(得分:1)

而不是

String[] command = {"/system/bin/sh", "-c", "su"};

也许做一个

String[] command = {"/system/bin/sh", "-c", "/data/bin/gdb " + pidS + " " + pidS};

或只是

String[] command = {"/data/bin/gdb " + pidS + " " + pidS};

答案 3 :(得分:1)

你不应该close()。关闭它会发送EOF

答案 4 :(得分:0)

gdb在启动后立即从stdin读取,看到没有输入(并且它不是终端),因此立即退出。为避免这种情况,您希望通过-batch以批处理模式运行它,然后将要执行的命令放在command file which you specify on the command line using the -x argument中。

您的代码将类似于:

out.println("/data/bin/gdb -batch -x " + commandFilenameS + " " + pidS + " " + pidS);
相关问题