gdb set arguments プログラム引数の入力
# gdb testmain
# gdb break main
Breakpoint 1 at 0x4005d2: file ../src/testmain.c, line 12.
プログラムへの引数は以下のコマンドで入力できる。
(gdb) set args "abc" 100
(gdb) run
Starting program: /work/obj/testmain "abc" 100
Breakpoint 1, main (argc=3, argv=0x7fffffffe448) at ../src/testmain.c:12
12 char* rc = getConst();
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-225.el8_8.6.x8-8.5.0-10.1.el8_6.x86_64 libstdc++-8.5.0-10.1.el8_6.x86_64
(gdb) show args
Argument list to give program being debugged when it is started is ""abc" 100".
(gdb) info args
argc = 3
argv = 0x7fffffffe448
(gdb) p argv[0]
$2 = 0x7fffffffe6cb "/work/obj/testmain"
(gdb) p argv[1]
$3 = 0x7fffffffe6ed "abc"
(gdb) p argv[2]
$4 = 0x7fffffffe6f1 "100"
(gdb) c
Continuing.
rc=RC_0001
args[0]=/work/obj/testmain agrv[1]=abc argv[2]=100
[Inferior 1 (process 2458121) exited normally]
(gdb) Quit
ソースコードは以下。
# cat testmain.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CONST_RC "RC_0001"
char* getConst() {
return (char*)CONST_RC;
}
int main(int argc, char *argv[]) {
char* rc = getConst();
printf("rc=%s\n",rc);
if (argc > 2) {
printf("args[0]=%s agrv[1]=%s argv[2]=%s\n",argv[0],argv[1],argv[2]);
}
return 0;
}
makefileは以下。
# cat testmain.mk
SRCDIR = ../src
OBJDIR = ../obj
TARGETDIR = ../lib
TARGETSRC = $(SRCDIR)/testmain.c
TARGETOBJ = $(OBJDIR)/testmain.o
TARGETEXE = $(OBJDIR)/testmain
CC = g++
CCOPTIONS = -c -g3 -O0 -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small
LIBCC = $(CC)
LDOPTIONS = -m64
.SUFFIXES: .c .o
# -------------------------------
# rules
# -------------------------------
all: $(TARGETEXE)
$(TARGETEXE): $(TARGETOBJ)
$(LIBCC) $(LDOPTIONS) $(TARGETOBJ) -o $(TARGETEXE)
$(TARGETOBJ): $(TARGETSRC)
$(CC) $(CCOPTIONS) $(TARGETSRC) -o $(TARGETOBJ)
clean:
@rm -f $(TARGETOBJ) $(TARGETEXE)
install:
@cp -p $(TARGETEXE) $(TARGETDIR)
コメント
コメントを投稿