programing

GDB: 일반 포인터의 참조 해제 시도

easyjava 2023. 9. 26. 22:37
반응형

GDB: 일반 포인터의 참조 해제 시도

에서 GDB 와 같은 가 추가적인 하도록 하려면 합니까?x/s?

할 때x/"일반 포인터를 참조 해제 시도"라는 오류가 나타납니다.으로.x/각 사용에는 암시적인 참조 해제가 포함되므로 여러 번 작동하지만, 각 중간 결과를 복사하여 붙여넣어야 하므로 귀찮습니다.

C한 C 를 해 보십시오.example.c:

#include <stdio.h>
int main(int argc, char **argv) {
  printf("argv[0] = %s\n", argv[0]);
}

해서 GDB 가 .argv됩니다에 됩니다.0xc(%ebp)에 대한 두 입니다.printf 즉,에)에서)0x4(%esp) 26 :

$ gcc -o example example.c
$ gdb example

(gdb) disass main
Dump of assembler code for function main:
   0x080483e4 <+0>:   push   %ebp
   0x080483e5 <+1>:   mov    %esp,%ebp
   0x080483e7 <+3>:   and    $0xfffffff0,%esp
   0x080483ea <+6>:   sub    $0x10,%esp
   0x080483ed <+9>:   mov    0xc(%ebp),%eax
   0x080483f0 <+12>:  mov    (%eax),%edx
   0x080483f2 <+14>:  mov    $0x80484e0,%eax
   0x080483f7 <+19>:  mov    %edx,0x4(%esp)
   0x080483fb <+23>:  mov    %eax,(%esp)
   0x080483fe <+26>:  call   0x8048300 <printf@plt>
   0x08048403 <+31>:  leave  
   0x08048404 <+32>:  ret    
End of assembler dump.

나는 깨집니다.printf합니다를 을 실행합니다.first그리고.second:

(gdb) break *main + 26
Breakpoint 1 at 0x80483fe

(gdb) run first second
Starting program: /var/tmp/SO-attempt-to-dereference-generic-pointer/example first second

합니다 합니다.argv[0]GDB에서 "일반 포인터" 오류가 발생합니다.

Breakpoint 1, 0x080483e5 in main ()
(gdb) x/s **(0xc + $ebp)
Attempt to dereference a generic pointer.

으로 몇 를 해제함으로써 'x/xw' 를를 수 되었습니다.argv[0](그리고argv[1]):

(gdb) x/xw 0xc + $ebp
0xbfffeba4: 0xbfffec34
(gdb) x/xw 0xbfffec34
0xbfffec34: 0xbfffedc8
(gdb) x/s 0xbfffedc8
0xbfffedc8:  "/var/tmp/SO-attempt-to-dereference-generic-pointer/example"

(gdb) x/xw 0xbfffec34 + 4
0xbfffec38: 0xbfffee03
(gdb) x/s 0xbfffee03
0xbfffee03:  "first"
(gdb) 

하지만 이것은 성가시고 간접적인 것입니다. (포인터 프로그래밍은 그렇게 되지 않을 것이기 때문에?)

해결책은 포인터를 재참조하기 전에 캐스팅하는 것입니다.

예를 들어, 위에서 중단한 부분을 선택하는 것입니다.

(gdb) x/s **((char ***) (0xc + $ebp))
0xbfffedc8:  "/var/tmp/SO-attempt-to-dereference-generic-pointer/example"
(gdb) x/s *(*((char ***) (0xc + $ebp)) + 1)
0xbfffee03:  "first"
(gdb) x/s *(*((char ***) (0xc + $ebp)) + 2)
0xbfffee09:  "second"

는 다음과 .0xc + $ebp가 그 포인터이기 가 필요합니다.char *** 안 돼요.char **.

언급URL : https://stackoverflow.com/questions/20414699/gdb-attempt-to-dereference-generic-pointer

반응형