簡體   English   中英

使用外部程序將輸入發送到GDB scanf

[英]Sending input to GDB scanf using external program

每當使用外部程序觸發scanf時,如何向GDB發送輸入?

c文件:

#include<stdio.h>

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

}

Java外部程序:

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

外部程序的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 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) 

您可以像在shell中一樣重定向輸入

(gdb) run < input.txt

或者,您可以在另一個終端中運行該程序並通過PID連接到它:

gdb --pid $(pgrep myprogram.name)

最后,您可以使用直接調用dup2/dup3來執行類似的黑客攻擊(但我認為您不需要這樣做,它不會對用戶友好)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM