【工程项目经验】mac电脑lldb调试工具

重点:

https://lldb.llvm.org/use/tutorial.html

https://developer.apple.com/library/archive/documentation/IDEs/Conceptual/gdb_to_lldb_transition_guide/document/lldb-command-examples.html#//apple_ref/doc/uid/TP40012917-CH3-SW5

https://www.nesono.com/sites/default/files/lldb%20cheat%20sheet.pdf

1. LLDB 与 GDB

在 Xcode4.2 以前 Xcode 编译器还是 GCC(前端) + LLVM(后端) 架构,此时调试器还是 GDB 的时代。在 Xcode4.2 Clang(前端) + LLVM3.0(后端) 成为了默认编译器,之后从 Xcode4.3 开始 LLDB 成为了 Xcode 的默认编译器。LLDB 官网有LLDB 与 GDB命令对照表

摘抄网址:https://www.jianshu.com/p/00f4c93054af

1.1 LLDB 支持平台
macOS desktop user space debugging for i386 and x86_64
iOS simulator debugging on i386 and x86_64
iOS device debugging on ARM and AArch64
Linux local user-space debugging for i386, x86_64 and PPC64le
FreeBSD local user-space debugging for i386 and x86_64
Windows local user-space debugging for i386 (*)

mac 环境以外使用 LLDB 需要配置环境,mac 环境下安装 Xcode 即可使用。

1.2 语法

基本格式

<noun> <verb> [-options [option-value]] [argument [argument...]]

即:<关键字> <动作> [-可选项 [可选项的值]] [参数1 [参数2···]]
特别说明

  • 关键字、动作、可选项、参数 都是通过 空格 进行分隔。参数 如果带有空格需要使用 “” 包裹参数
  • 如果参数中包含有 \ 或 “” ,那么使用 \ 对它们进行转义
  • 如果参数以 - 开始,需要在可选项结束时使用 – 加以分割
    可选项顺序可以不是固定的
    示例
(lldb) process launch --stop-at-entry -- -program_arg value
关键字:process
动作:launch
可选项:-stop-at-entry
参数:-program_arg value
作用:启动程序并传入 `-program_arg value` 作为参数
(lldb) breakpoint set --file foo.c --line 12
关键字:breakpoint
动作:set
可选项:-file、-line
可选项的值:foo.c、12
作用:在文件 `foo.c` 的 `12` 行设置断点
(lldb) breakpoint set --name foo
关键字:breakpoint
动作:set
可选项:-name
可选项的值:foo
作用:为函数名为 `foo` 的函数设置断点
1.3 进入调试状态
  • 调试可执行文件
lldb DebugDemo.run
#DebugDemo.run是编译出来的可执行文件
  • 调试运行时带参数的可执行文件
    如果运行这个程序时是要带参数的,那么久这样:
lldb -- DebugDemo.run 1 2 3
#等价于你在终端运行DebugDemo.run 1 2 3
1.3.1 使用LLDB来启动程序

一旦指定了调试哪个程序,并为其设置了一些断点后,就可以开始运行程序了。我们可以使用以下命令来启动程序:

(lldb) process launch
(lldb) run
(lldb) r

参考 http://southpeak.github.io/2015/01/25/tool-lldb/

1.3.2 查看汇编
x/5i $pc-6
   0x804837f <main+11>: mov    %esp,%ebp
   0x8048381 <main+13>: push   %ecx
   0x8048382 <main+14>: sub    $0x4,%esp
=> 0x8048385 <main+17>: movl   $0x8048460,(%esp)

参考:https://sourceware.org/gdb/current/onlinedocs/gdb/Memory.html
参考:https://liangmc.com/archives/03lldb%E6%B1%87%E7%BC%96%E8%B0%83%E8%AF%95md

1.3.3 source mode和disassembly mode
stop-disassembly-count   -- The number of disassembly lines to show when displaying a stopped context.
  stop-disassembly-display -- Control when to display disassembly when displaying a stopped context.
  stop-line-count-after    -- The number of sources lines to display that come after the current source line when displaying a stopped context.
  stop-line-count-before   -- The number of sources lines to display that come before the current source line when displaying a stopped context.

https://stackoverflow.com/questions/33333165/how-to-switch-between-source-code-debug-mode-and-disassembly-debug-mode-in-lldb

1.3.4 反汇编
otool -p _main -tV a.o/// _main函数开始进行反汇编
otool -tV a.o

https://wangwangok.gitbooks.io/mac-terminal-tool/content/otool.html

1.4 看代码
  • 进入到调试状态之后, lldb和gdb一样,也给了你看代码的命令: list或l, 但只有在编译时候带-g才能看
l
  • 看某个函数的代码
list main
1.5 变量打印
大部分 iOS Coder 在实际开发中用得最多的 LLDB 命令可能就是 po。po的全称是print object,它的作用是调用 NSObject 对象的 debugDescription 方法。而另一个常用命令 p 全称是 print,作用是打印 NSObject 对象的地址或基本数据类型的值,并生成一个临时变量。下面来说说 LLDB 中的关键字 expression
  • print 简写p 是 expression – 的缩写
  • po 是 expression -o – 的缩写

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值