2011-11-30 80 views
2

我試圖退出調試會話,並要求用戶輸入驗證。 我試圖在我的代碼Java GDB輸入不是來自終端

stdin.println("quit"); 
    stdin.flush(); 
    stdin.println("y"); // this does not work.. 
    stdin.flush(); 

運行這段代碼,但它不工作..

如何使用這個Java外部程序我輸入發送給GDB?

public class Debugger 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 main"); 
     stdin.flush(); 
     stdin.println("run"); 
     stdin.flush(); 
     stdin.println("s"); 
     stdin.flush(); 
     stdin.println("quit"); 
     stdin.flush(); 
     stdin.println("y"); // this does not work.. 
     stdin.flush(); 
     // 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 
     { 
      int length; 
      byte[] buffer = new byte[1024]; 

      for (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_; 
} 

GDB輸出:

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/workspace/AVT/a.out...done. 
(gdb) Breakpoint 1 at 0x804853d: file fileg.c, line 7. 
(gdb) Starting program: /home/charmae/workspace/AVT/a.out 

Breakpoint 1, main() at fileg.c:7 
7  printf("input of x: "); 
(gdb) 8  scanf("%d",&x); 
(gdb) A debugging session is active. 

    Inferior 1 [process 6341] will be killed. 

Quit anyway? (y or n) [answered Y; input not from terminal] 

回答

0

運行的gdb設置set confirm off

例如:

gdb -ex run -ex "set confirm off" --args ./program [arg1] [arg2] [arg3]