Java 使用外部程序向GDB scanf发送输入

Java 使用外部程序向GDB scanf发送输入,java,c,debugging,process,gdb,Java,C,Debugging,Process,Gdb,当使用外部程序触发scanf时,如何向GDB发送输入 c文件: #include<stdio.h> void main() { int x; int y; printf("input x: "); scanf("%d",&x); printf("input y: "); scanf("%d",&y); } 外部程序的GDB输出: public class Debugger extends Thread{

当使用外部程序触发scanf时,如何向GDB发送输入

c文件:

#include<stdio.h>

void main()
{
    int x;
    int y;
    printf("input x: ");
    scanf("%d",&x);
    printf("input y: ");
    scanf("%d",&y);

}
外部程序的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("45");
        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_;
}
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 0x804843d: file file4.c, line 7.
(gdb) Starting program: /home/charmae/workspace/AVT/a.out 

Breakpoint 1, main () at file4.c:7
7       printf("input of x: ");
(gdb) 8     scanf("%d",&x);
(gdb) Undefined command: "45".  Try "help".
(gdb) 
GNU gdb(Ubuntu/Linaro 7.3-0ubuntu2)7.3-2011.08
版权所有(C)2011免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“i686 linux gnu”。
有关错误报告说明,请参阅:
...
从/home/charmae/workspace/AVT/a.out读取符号…完成。
(gdb)断点1位于0x804843d:文件file4.c,第7行。
(gdb)启动程序:/home/charmae/workspace/AVT/a.out
文件4.c:7处的断点1,main()
7 printf(“x的输入:”);
(gdb)8扫描帧(“%d”和“x”);
(gdb)未定义的命令:“45”。试试“帮助”。
(gdb)

您可以像在shell中一样重定向输入

(gdb) run < input.txt

最后,您可以使用直接调用来进行类似的攻击(但我认为您不需要这样做,它不会对用户友好)

yo您可以详细说明如何使用此方法:gdb--pid$(pgrep myprogram.name)以及如何使用文件描述符的复制吗?我如何可能在dup2上使用dup2/dup3?有关一般想法,请参阅;在
gdb上--pid
请参见
gdb --pid $(pgrep myprogram.name)