安全编程原理-LAB2-GCC

Running a Hello World Program in C using GCC

一、打开终端

由于直接使用虚拟机的终端界面过小且不清晰,故本实验在windows下使用ssh协议连接到了虚拟机

如图

image-20200610120551601

二、创建debug_me.c

image-20200610121248605

保存后检查结果

image-20200610121317429

三、编译运行

image-20200610122004477

输入

1
2
3
gcc -g debug_me.c -o debug_me
gdb debug_me
run "hello, world" "goodbye, world"

出现运行结果:

1
2
String '1' - 'hello,world'
String '2' - 'goodbye world'

符合预期结果

调试尝试

  • 设置断点

    1
    break main

    image-20200610162324985

  • 输入run debug_me "hello, world" "goodbye, world"

    程序运行到main函数之中

  • 连续几次 n(next的缩写),运行到 print_string 函数时,输入n 程序直接运行出第一次输出的结果 : String ‘1’ - ‘debug_me’

  • 继续调试,程序再次运行到 print_string 函数时,输入 s (step),我们发现程序进入到了 print_string 函数内部。

  • step 和 next的区别

    step表示单步进入,当当前语句是一个函数时,程序将进入该函数内部

    next表示单步运行,当当前语句是一个函数时,程序不会进入到函数内部,而是直接运行出结果。

  • 继续调试,输入where,得到结果

    #0 print_string ......

    #1 .... in main

  •   (gdb) frame 0
      (gdb) print i
      ...
      (gdb) frame 1
      (gdb) print i
    

    得到结果

    前者 No symbol “i” in current context

    后者 $$1 = 2$