gdb print value in hex
# gdb testhex
(gdb) break main
Breakpoint 1 at 0x4006b2: file ../src/testhex.c, line 15.
(gdb) run
Starting program: /work/mizuho/routine/obj/testhex
Breakpoint 1, main () at ../src/testhex.c:15
15 unsigned char input[] = {0xE3,0x81,0x82,0x00};
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-225.el8_8.6.x86_64 libgcc-8.5.0-10.1.el8_6.x86_64 libstdc++-8.5.0-10.1.el8_6.x86_64
list source code from 1 line
(gdb) l 1
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 void printhex(unsigned char* in) {
6 printf("input = %s in hex =",in);
7 unsigned char* p = in;
8 while(*p != 0x00) {
9 printf(" %2x", *p);
10 p++;
(gdb) l
11 }
12 printf("\n");
13 }
14 int main() {
15 unsigned char input[] = {0xE3,0x81,0x82,0x00};
16
17 printhex(input);
18 unsigned char* ptr = input;
19 while (*ptr != 0x00) {
20 if (*ptr == 0xe3) {
(gdb) l
21 printf("found 0xe3\n");
22 } else if (*ptr == 0x81) {
23 printf("found 0x81\n");
24 } else if (*ptr == 0x82) {
25 printf("found 0x82\n");
26 }
27 ptr++;
28 }
29 return 0;
30 }
変数inputを表示する。
(gdb) p input
$1 = "あ"
hexで表示する。
(gdb) p/x input
$2 = {0xe3, 0x81, 0x82, 0x0}
continueで最後まで実行する。
(gdb) c
Continuing.
input = あ in hex = e3 81 82
found 0xe3
found 0x81
found 0x82
[Inferior 1 (process 2457279) exited normally]
quitでgdbの終了。
(gdb) q
#
ソースコードは以下。
# cat testhex.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printhex(unsigned char* in) {
printf("input = %s in hex =",in);
unsigned char* p = in;
while(*p != 0x00) {
printf(" %2x", *p);
p++;
}
printf("\n");
}
int main() {
unsigned char input[] = {0xE3,0x81,0x82,0x00};
printhex(input);
unsigned char* ptr = input;
while (*ptr != 0x00) {
if (*ptr == 0xe3) {
printf("found 0xe3\n");
} else if (*ptr == 0x81) {
printf("found 0x81\n");
} else if (*ptr == 0x82) {
printf("found 0x82\n");
}
ptr++;
}
return 0;
}
makefileは以下。-g3 -O0がデバッグ用のoption。
# cat testhex.mk
SRCDIR = ../src
OBJDIR = ../obj
TARGETDIR = ../lib
TARGETSRC = $(SRCDIR)/testhex.c
TARGETOBJ = $(OBJDIR)/testhex.o
TARGETEXE = $(OBJDIR)/testhex
CC = g++
CCOPTIONS = -c -g3 -O0 -m64 -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)
コメント
コメントを投稿