인수를 전달하고 파일에서 stdin을 gdb에서 실행되는 프로그램으로 리디렉션하는 방법은 무엇입니까?
나는 보통 다음과 같이 프로그램을 실행합니다.
./a.out arg1 arg2 <file
gdb를 사용하여 디버깅하고 싶습니다.
set args
기능을 알고 있지만 gdb 프롬프트에서만 작동합니다.
run
gdb 내 에서 명령에 인수를 전달하십시오 .
$ 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를 시작하십시오.
프로젝트 실행 파일을 이미 컴파일 한 프로젝트 디렉토리로 이동하십시오. 다음과 같이 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)
프로그램 실행을 시작하기 전에 중단 점을 설정하려고합니다. break 명령을 사용하면 그렇게 할 수 있습니다. main이라는 함수의 시작 부분에 중단 점을 설정하려면 다음을 수행하십시오.
(gdb) b 메인
(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 exitgdb
. But I call this a feature.You cannot easily pass
argv0
to the program like withexec -a arg0 command args
. Following should do this trick: Afterexec-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 read0<&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 typetty; sleep inf
and then use the printed TTY (i. E./dev/pts/60
) for debugging, as inDEBUGTTY=/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 controlgdb
./usr/bin/gdb -q -nx -nw
runsgdb
invokesgdb
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 theexec-wrapper
--args "$@"
passes the arguments as given
Wasn't that easy?
'Programing' 카테고리의 다른 글
Eclipse CDT에서 C ++ 11 / C ++ 0x 지원을 활성화하는 방법은 무엇입니까? (0) | 2020.05.01 |
---|---|
Mac OS X 터미널 : 맵 옵션 + 삭제를 "뒤로 삭제 단어"로 (0) | 2020.05.01 |
DIV에 클래스 "x"가없는 경우 jQuery (0) | 2020.05.01 |
jQuery에서 텍스트 영역의 변경 이벤트에 어떻게 바인딩 할 수 있습니까? (0) | 2020.05.01 |
자바 스크립트에서 NaN을 0으로 변환 (0) | 2020.05.01 |