Programing

인수를 전달하고 파일에서 stdin을 gdb에서 실행되는 프로그램으로 리디렉션하는 방법은 무엇입니까?

lottogame 2020. 5. 1. 08:00
반응형

인수를 전달하고 파일에서 stdin을 gdb에서 실행되는 프로그램으로 리디렉션하는 방법은 무엇입니까?


나는 보통 다음과 같이 프로그램을 실행합니다.

./a.out arg1 arg2 <file

gdb를 사용하여 디버깅하고 싶습니다.

set args기능을 알고 있지만 gdb 프롬프트에서만 작동합니다.


rungdb 내 에서 명령에 인수를 전달하십시오 .

$ gdb ./a.out
(gdb) r < t
Starting program: /dir/a.out < t

당신은 이것을 할 수 있습니다 :

gdb --args path/to/executable -every -arg you can=think < of

마법의 비트 --args.

그냥 입력 run디버깅을 시작하기 위해 GDB 명령 콘솔에서.


경로 재 지정 및 인수로 프로그램을 실행하기위한 run명령 을 원한다면 gdb다음을 사용할 수 있습니다 set args.

% gdb ./a.out
(gdb) set args arg1 arg2 <file
(gdb) run

--args매개 변수를 사용 하여 동일한 동작을 수행 할 수 없었 gdb으며 리디렉션을 격렬하게 탈출했습니다.

% gdb --args echo 1 2 "<file"
(gdb) show args
Argument list to give program being debugged when it is started is "1 2 \<file".
(gdb) run
...
1 2 <file
...

이것은 실제로 우리가 실제로 원하는 것이 아니라 gdb 자체의 입력을 리디렉션합니다.

% gdb --args echo 1 2 <file
zsh: no such file or directory: file

프로젝트에서 GDB를 시작하십시오.

  1. 프로젝트 실행 파일을 이미 컴파일 한 프로젝트 디렉토리로 이동하십시오. 다음과 같이 gdb 명령과 실행 파일 이름을 발행하십시오.

    gdb projectExecutablename

이것은 gdb를 시작하고 다음을 인쇄합니다 : GNU gdb (Ubuntu 7.11.1-0ubuntu1 ~ 16.04) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. ............... .................................. "apropos word"를 입력하여 "word"와 관련된 명령을 검색하십시오. projectExecutablename ...에서 기호 읽기 ... 완료. (gdb)

  1. 프로그램 실행을 시작하기 전에 중단 점을 설정하려고합니다. break 명령을 사용하면 그렇게 할 수 있습니다. main이라는 함수의 시작 부분에 중단 점을 설정하려면 다음을 수행하십시오.

    (gdb) b 메인

  2. (gdb) 프롬프트가 표시되면 실행 명령이 실행 파일 실행을 시작합니다. 디버깅하는 프로그램에 명령 줄 인수가 필요한 경우 run 명령에 지정합니다. "xfiles"파일 (프로젝트 디렉토리의 "mulder"폴더에 있음)에서 프로그램을 실행하려면 다음을 수행하십시오.

    (gdb) r mulder / xfiles

도움이 되었기를 바랍니다.

면책 조항 :이 솔루션은 그것에서 적응, 광산하지 https://web.stanford.edu/class/cs107/guide_gdb.html GDB이 짧은 가이드는, 아마도, 스탠포드 대학에서 개발되었다.


쉘 레벨에서 debug디버깅 할 수 있도록 명령 앞에 입력하는 것이 좋지 gdb않습니까?

그 아래에이 기능이 있습니다. 심지어 다음과 같이 작동합니다.

"$program" "$@" < <(in) 1> >(out) 2> >(two) 3> >(three)

This is a call where you cannot control anything, everything is variable, can contain spaces, linefeeds and shell metacharacters. In this example, in, out, two, and three are arbitrary other commands which consume or produce data which must not be harmed.

Following bash function invokes gdb nearly cleanly in such an environment [Gist]:

debug()
{
  1000<&0 1001>&1 1002>&2 \
  0</dev/tty 1>/dev/tty 2>&0 \
  /usr/bin/gdb -q -nx -nw \
  -ex 'set exec-wrapper /bin/bash -c "exec 0<&1000 1>&1001 2>&1002 \"\$@\"" exec' \
  -ex r \
  --args "$@";
}

Example on how to apply this: Just type debug in front:

Before:

p=($'\n' $'I\'am\'evil' "  yay  ")
"b u g" "${p[@]}" < <(in) 1> >(out) 2> >(two) 3> >(three)

After:

p=($'\n' $'I\'am\'evil' "  yay  ")
debug "b u g" "${p[@]}" < <(in) 1> >(out) 2> >(two) 3> >(three)

That's it. Now it's an absolute no-brainer to debug with gdb. Except for a few details or more:

  • gdb does not quit automatically and hence keeps the IO redirection open until you exit gdb. But I call this a feature.

  • You cannot easily pass argv0 to the program like with exec -a arg0 command args. Following should do this trick: After exec-wrapper change "exec to "exec -a \"\${DEBUG_ARG0:-\$1}\".

  • There are FDs above 1000 open, which are normally closed. If this is a problem, change 0<&1000 1>&1001 2>&1002 to read 0<&1000 1>&1001 2>&1002 1000<&- 1001>&- 1002>&-

  • You cannot run two debuggers in parallel. There also might be issues, if some other command consumes /dev/tty (or STDIN). To fix that, replace /dev/tty with "${DEBUGTTY:-/dev/tty}". In some other TTY type tty; sleep inf and then use the printed TTY (i. E. /dev/pts/60) for debugging, as in DEBUGTTY=/dev/pts/60 debug command arg... That's the Power of Shell, get used to it!

Function explained:

  • 1000<&0 1001>&1 1002>&2 moves away the first 3 FDs
    • This assumes, that FDs 1000, 1001 and 1002 are free
  • 0</dev/tty 1>/dev/tty 2>&0 restores the first 3 FDs to point to your current TTY. So you can control gdb.
  • /usr/bin/gdb -q -nx -nw runs gdb invokes gdb on shell
  • -ex 'set exec-wrapper /bin/bash -c "exec 0<&1000 1>&1001 2>&1002 \"\$@\"" creates a startup wrapper, which restores the first 3 FDs which were saved to 1000 and above
  • -ex r starts the program using the exec-wrapper
  • --args "$@" passes the arguments as given

Wasn't that easy?

참고URL : https://stackoverflow.com/questions/4521015/how-to-pass-arguments-and-redirect-stdin-from-a-file-to-program-run-in-gdb

반응형