java gdb 运行时

标签 java process gdb runtime runtime.exec

如何在 java 中使用进程对象执行 gdb 命令?

charmae@charmae-pc:~/Desktop$ gcc -g file.c
charmae@charmae-pc:~/Desktop$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/charmae/Desktop/a.out...done.
(gdb) run
Starting program: /home/charmae/Desktop/a.out 
address of x = 0xbffff2f8
address of y= 0xbffff2fc
address of x = 0xbffff2f8
value of x = 1
[Inferior 1 (process 4268) exited with code 017]
(gdb) 

在我的java代码中:

 Runtime rt = Runtime.getRuntime();
 Process proc = rt.exec("gdb ./a.out");
                rt.exec("run");
 BufferedReader std = new BufferedReader(new 
                     InputStreamReader(proc.getErrorStream()));
 while ((s = std.readLine()) != null) {
                 System.out.println(s);
               }

最佳答案

是啊!回答了我自己的问题。我使用了管道处理。

    public class GDBpipeWriter extends Thread{

        public void run(){
        Process p = null;
        try {
        p = Runtime.getRuntime().exec("gdb a.out --interpreter=console");
       new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
       new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());

        stdin.flush();
        stdin.println("break 4");
        stdin.flush();
        stdin.println("break 10");
        stdin.flush();
        stdin.println("run");
        stdin.flush();

        /// write any other commands you want here
       // stdin.close();


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


class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
  }
  public void run() {
      try
      {
          final byte[] buffer = new byte[1024];
          for (int length = 0; (length = istrm_.read(buffer)) != -1; )
          {
              ostrm_.write(buffer, 0, length);
          }

      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  private final OutputStream ostrm_;
  private final InputStream istrm_;
}

关于java gdb 运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118571/

相关文章:

java - 如何在 Java 中禁用 ButtonGroup 内的 JRadioButton

python - 运行 python 的 Unix 进程

python - 在 python 中打开具有特定 pid 的进程

c - 将 getopt 与 gdb 一起使用

c++ - 当 ddd 为 "waiting for it to get ready"时,gdb/dbx 在做什么?

java - 在 Java 中使用 C++ 函数

java - 在接口(interface)中提供不同的方法名称

java - 如何设置Dialog的大小以匹配JTable的大小?

c# - 即使在 ProcessWindowStyle.Hidden 之后,控制台窗口仍然弹出;

c++ - 如何在 gdb 中的内存位置添加断点