검색

이 콘텐츠는 선택한 언어로 제공되지 않습니다.

4.3.2. Running GDB

download PDF
This section will describe a basic execution of GDB, using the following simple program:
hello.c

#include <stdio.h>

char hello[] = { "Hello, World!" };

int
main()
{
  fprintf (stdout, "%s\n", hello);
  return (0);
}

The following procedure illustrates the debugging process in its most basic form.

Procedure 4.1. Debugging a 'Hello World' Program

  1. Compile hello.c into an executable with the debug flag set, as in:
    gcc -g -o hello hello.c
    Ensure that the resulting binary hello is in the same directory as hello.c.
  2. Run gdb on the hello binary, that is, gdb hello.
  3. After several introductory comments, gdb will display the default GDB prompt:
    (gdb)
  4. The variable hello is global, so it can be seen even before the main procedure starts:
    gdb) p hello
    $1 = "Hello, World!"
    (gdb) p hello[0]
    $2 = 72 'H'
    (gdb) p *hello
    $3 = 72 'H'
    (gdb)
    
    Note that the print targets hello[0] and *hello require the evaluation of an expression, as does, for example, *(hello + 1):
    (gdb) p *(hello + 1)
    $4 = 101 'e'
    
  5. Next, list the source:
    (gdb) l
    1       #include <stdio.h>
    2
    3       char hello[] = { "Hello, World!" };
    4
    5       int
    6       main()
    7       {
    8         fprintf (stdout, "%s\n", hello);
    9         return (0);
    10      }
    
    The list reveals that the fprintf call is on line 8. Apply a breakpoint on that line and resume the code:
    (gdb) br 8
    Breakpoint 1 at 0x80483ed: file hello.c, line 8.
    (gdb) r
    Starting program: /home/moller/tinkering/gdb-manual/hello
    
    Breakpoint 1, main () at hello.c:8
    8         fprintf (stdout, "%s\n", hello);
    
  6. Finally, use the next command to step past the fprintf call, executing it:
    (gdb) n
    Hello, World!
    9         return (0);
    
The following sections describe more complex applications of GDB.
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.