在当前目录, 运行多个含有main函数的源文件

  1. cmake_minimum_required(VERSION 3.21)
  2. # 获取目录名
  3. get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  4. # 目录名中的空格替换成下划线
  5. string(REPLACE " " "_" ProjectId ${ProjectId})
  6. # project(${ProjectId} CXX)
  7. project(${ProjectId} C)
  8. set(CMAKE_CXX_STANDARD 23)
  9. # 将目录下的.c存入files
  10. # file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
  11. file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
  12. foreach(file ${files})
  13. get_filename_component(name ${file} NAME)
  14. add_executable(${name} ${file})
  15. endforeach()

image.png

下断点观察变量的变化

image.png

  • GDB

image.png

  • LLDB

image.png

  • Watchs

image.png

  • 执行下一次断点

image.png

  • Evaluate Expression

image.png
image.png
image.png

反汇编/disassemble

  1. #include <stdio.h>
  2. int main() {
  3. for(int i = 0; i < 5; ++i)
  4. {
  5. printf("i=%d\n", i);
  6. }
  7. return 0;
  8. }

image.png

  1. (lldb) disassemble
  2. 02_DEBUG.c.exe`main(...):
  3. 0x1f14b0 <+0>: pushl %ebp
  4. 0x1f14b1 <+1>: movl %esp, %ebp
  5. 0x1f14b3 <+3>: pushl %ecx
  6. 0x1f14b4 <+4>: movl $0xcccccccc, -0x4(%ebp) ; imm = 0xCCCCCCCC
  7. 0x1f14bb <+11>: movl $0x0, -0x4(%ebp)
  8. 0x1f14c2 <+18>: jmp 0x1f14cd ; <+29> at 02_DEBUG.c:4
  9. 0x1f14c4 <+20>: movl -0x4(%ebp), %eax
  10. 0x1f14c7 <+23>: addl $0x1, %eax
  11. 0x1f14ca <+26>: movl %eax, -0x4(%ebp)
  12. 0x1f14cd <+29>: cmpl $0x5, -0x4(%ebp)
  13. 0x1f14d1 <+33>: jge 0x1f14e6 ; <+54> at 02_DEBUG.c:9
  14. -> 0x1f14d3 <+35>: movl -0x4(%ebp), %ecx
  15. 0x1f14d6 <+38>: pushl %ecx
  16. 0x1f14d7 <+39>: pushl $0x1fa000 ; imm = 0x1FA000
  17. 0x1f14dc <+44>: calll 0x1f107d ; _printf
  18. 0x1f14e1 <+49>: addl $0x8, %esp
  19. 0x1f14e4 <+52>: jmp 0x1f14c4 ; <+20> at 02_DEBUG.c:4
  20. 0x1f14e6 <+54>: xorl %eax, %eax
  21. 0x1f14e8 <+56>: addl $0x4, %esp
  22. 0x1f14eb <+59>: cmpl %esp, %ebp
  23. 0x1f14ed <+61>: calll 0x1f1159 ; __RTC_CheckEsp
  24. 0x1f14f2 <+66>: movl %ebp, %esp
  25. 0x1f14f4 <+68>: popl %ebp
  26. 0x1f14f5 <+69>: retl
  • 设置汇编指令格式: settings set target.x86-disassembly-flavor
    • 常见Intel, AT&T

image.png
image.png

  1. (lldb) settings set target.x86-disassembly-flavor intel
  2. (lldb) disassemble
  3. 02_DEBUG.c.exe`main(...):
  4. 0x1f14b0 <+0>: push ebp
  5. 0x1f14b1 <+1>: mov ebp, esp
  6. 0x1f14b3 <+3>: push ecx
  7. 0x1f14b4 <+4>: mov dword ptr [ebp - 0x4], 0xcccccccc
  8. 0x1f14bb <+11>: mov dword ptr [ebp - 0x4], 0x0
  9. 0x1f14c2 <+18>: jmp 0x1f14cd ; <+29> at 02_DEBUG.c:4
  10. 0x1f14c4 <+20>: mov eax, dword ptr [ebp - 0x4]
  11. 0x1f14c7 <+23>: add eax, 0x1
  12. 0x1f14ca <+26>: mov dword ptr [ebp - 0x4], eax
  13. 0x1f14cd <+29>: cmp dword ptr [ebp - 0x4], 0x5
  14. 0x1f14d1 <+33>: jge 0x1f14e6 ; <+54> at 02_DEBUG.c:9
  15. -> 0x1f14d3 <+35>: mov ecx, dword ptr [ebp - 0x4]
  16. 0x1f14d6 <+38>: push ecx
  17. 0x1f14d7 <+39>: push 0x1fa000
  18. 0x1f14dc <+44>: call 0x1f107d ; _printf
  19. 0x1f14e1 <+49>: add esp, 0x8
  20. 0x1f14e4 <+52>: jmp 0x1f14c4 ; <+20> at 02_DEBUG.c:4
  21. 0x1f14e6 <+54>: xor eax, eax
  22. 0x1f14e8 <+56>: add esp, 0x4
  23. 0x1f14eb <+59>: cmp ebp, esp
  24. 0x1f14ed <+61>: call 0x1f1159 ; __RTC_CheckEsp
  25. 0x1f14f2 <+66>: mov esp, ebp
  26. 0x1f14f4 <+68>: pop ebp
  27. 0x1f14f5 <+69>: ret
  1. (lldb) settings set target.x86-disassembly-flavor att
  2. (lldb) disassemble
  3. 02_DEBUG.c.exe`main(...):
  4. 0x1f14b0 <+0>: pushl %ebp
  5. 0x1f14b1 <+1>: movl %esp, %ebp
  6. 0x1f14b3 <+3>: pushl %ecx
  7. 0x1f14b4 <+4>: movl $0xcccccccc, -0x4(%ebp) ; imm = 0xCCCCCCCC
  8. 0x1f14bb <+11>: movl $0x0, -0x4(%ebp)
  9. 0x1f14c2 <+18>: jmp 0x1f14cd ; <+29> at 02_DEBUG.c:4
  10. 0x1f14c4 <+20>: movl -0x4(%ebp), %eax
  11. 0x1f14c7 <+23>: addl $0x1, %eax
  12. 0x1f14ca <+26>: movl %eax, -0x4(%ebp)
  13. 0x1f14cd <+29>: cmpl $0x5, -0x4(%ebp)
  14. 0x1f14d1 <+33>: jge 0x1f14e6 ; <+54> at 02_DEBUG.c:9
  15. -> 0x1f14d3 <+35>: movl -0x4(%ebp), %ecx
  16. 0x1f14d6 <+38>: pushl %ecx
  17. 0x1f14d7 <+39>: pushl $0x1fa000 ; imm = 0x1FA000
  18. 0x1f14dc <+44>: calll 0x1f107d ; _printf
  19. 0x1f14e1 <+49>: addl $0x8, %esp
  20. 0x1f14e4 <+52>: jmp 0x1f14c4 ; <+20> at 02_DEBUG.c:4
  21. 0x1f14e6 <+54>: xorl %eax, %eax
  22. 0x1f14e8 <+56>: addl $0x4, %esp
  23. 0x1f14eb <+59>: cmpl %esp, %ebp
  24. 0x1f14ed <+61>: calll 0x1f1159 ; __RTC_CheckEsp
  25. 0x1f14f2 <+66>: movl %ebp, %esp
  26. 0x1f14f4 <+68>: popl %ebp
  27. 0x1f14f5 <+69>: retl
  • 将语句复制到.lldbint ,设置汇编显示的默认格式

image.png

Memory View

image.png

  • &main

image.png

  • &i

image.png
image.png

小端序: 低地址位在最左侧