Skip to content

Instantly share code, notes, and snippets.

@joyhuang9473
Last active September 18, 2018 03:43
Show Gist options
  • Save joyhuang9473/29c8c30207c092318d1a5c2b1d062b38 to your computer and use it in GitHub Desktop.
Save joyhuang9473/29c8c30207c092318d1a5c2b1d062b38 to your computer and use it in GitHub Desktop.
user@pc:~$ gcc -Wall -g hello.c -o hello
user@pc:~$ gdb hello
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
(gdb) break main
Breakpoint 1 at 0x40055a: file hello.c, line 9.
(gdb) run 123 abc
Starting program: /home/user/hello 123 abc
Breakpoint 1, main (argc=3, argv=0x7fffffffe558) at hello.c:9
9 printf("\n%s\n\n", szHello);
(gdb) list
4 }
5
6 const char *szHello = "Hello World";
7 int main(int argc, char *argv[])
8 {
9 printf("\n%s\n\n", szHello);
10
11 int i;
12 for (i=0; i<argc; i++) {
13 printf("argv[%d]\n", i);
(gdb) break 14
Breakpoint 2 at 0x400590: file hello.c, line 14.
(gdb) continue
Continuing.
Hello World
argv[0]
Breakpoint 2, main (argc=3, argv=0x7fffffffe558) at hello.c:14
14 printf("- main: %s\n", argv[i]);
(gdb) next
- main: /home/user/hello
15 func(argv[i]);
(gdb) step
func (pMem=0x7fffffffe790 "/home/user/hello") at hello.c:3
3 printf("- func: %p\n\n", pMem);
(gdb) backtrace
#0 func (pMem=0x7fffffffe790 "/home/user/hello") at hello.c:3
#1 0x00000000004005d8 in main (argc=3, argv=0x7fffffffe558) at hello.c:15
(gdb) list
1 #include <stdio.h>
2 void func(char *pMem) {
3 printf("- func: %p\n\n", pMem);
4 }
5
6 const char *szHello = "Hello World";
7 int main(int argc, char *argv[])
8 {
9 printf("\n%s\n\n", szHello);
10
(gdb) print pMem
$1 = 0x7fffffffe790 "/home/user/hello"
(gdb) continue
Continuing.
- func: 0x7fffffffe790
argv[1]
Breakpoint 2, main (argc=3, argv=0x7fffffffe558) at hello.c:14
14 printf("- main: %s\n", argv[i]);
(gdb) next
- main: 123
15 func(argv[i]);
(gdb) step
func (pMem=0x7fffffffe7a7 "123") at hello.c:3
3 printf("- func: %p\n\n", pMem);
(gdb) print pMem
$2 = 0x7fffffffe7a7 "123"
(gdb) pinrt *pMem
Undefined command: "pinrt". Try "help".
(gdb) print *pMem
$3 = 49 '1'
(gdb) continue
Continuing.
- func: 0x7fffffffe7a7
argv[2]
Breakpoint 2, main (argc=3, argv=0x7fffffffe558) at hello.c:14
14 printf("- main: %s\n", argv[i]);
(gdb) next
- main: abc
15 func(argv[i]);
(gdb) next
- func: 0x7fffffffe7ab
12 for (i=0; i<argc; i++) {
(gdb) continue
Continuing.
[Inferior 1 (process 15246) exited normally]
#include <stdio.h>
void func(char *pMem) {
printf("- func: %p\n\n", pMem);
}
const char *szHello = "Hello World";
int main(int argc, char *argv[])
{
printf("\n%s\n\n", szHello);
int i;
for (i=0; i<argc; i++) {
printf("argv[%d]\n", i);
printf("- main: %s\n", argv[i]);
func(argv[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment