lldb调试c++

1.lldb

lldb是个非常好用的调试工具,在mac上的使用已经超过gdb,xcode和androidstudio上面已经成为了默认的调试器。

2.lldb help

(lldb) help
Debugger commands:
  apropos           -- List debugger commands related to a word or subject.
  breakpoint        -- Commands for operating on breakpoints (see 'help b' for
                       shorthand.)
  command           -- Commands for managing custom LLDB commands.
  disassemble       -- Disassemble specified instructions in the current
                       target.  Defaults to the current function for the
                       current thread and stack frame.
  expression        -- Evaluate an expression on the current thread.  Displays
                       any returned value with LLDB's default formatting.
  frame             -- Commands for selecting and examing the current thread's
                       stack frames.
  gdb-remote        -- Connect to a process via remote GDB server.
                       If no host is specifed, localhost is assumed.
                       gdb-remote is an abbreviation for 'process connect
                       --plugin gdb-remote connect://<hostname>:<port>'
  gui               -- Switch into the curses based GUI mode.
  help              -- Show a list of all debugger commands, or give details
                       about a specific command.
  kdp-remote        -- Connect to a process via remote KDP server.
                       If no UDP port is specified, port 41139 is
                       assumed.
                       kdp-remote is an abbreviation for 'process connect
                       --plugin kdp-remote udp://<hostname>:<port>'
  language          -- Commands specific to a source language.
  log               -- Commands controlling LLDB internal logging.
  memory            -- Commands for operating on memory in the current target
                       process.
  platform          -- Commands to manage and create platforms.
  plugin            -- Commands for managing LLDB plugins.
  process           -- Commands for interacting with processes on the current
                       platform.
  quit              -- Quit the LLDB debugger.
  register          -- Commands to access registers for the current thread and
                       stack frame.
  reproducer        -- Commands for manipulating reproducers. Reproducers make
                       it possible to capture full debug sessions with all its
                       dependencies. The resulting reproducer is used to replay
                       the debug session while debugging the debugger.
                       Because reproducers need the whole the debug session
                       from beginning to end, you need to launch the debugger
                       in capture or replay mode, commonly though the command
                       line driver.
                       Reproducers are unrelated record-replay debugging, as
                       you cannot interact with the debugger during replay.
  script            -- Invoke the script interpreter with provided code and
                       display any results.  Start the interactive interpreter
                       if no code is supplied.
  session           -- Commands controlling LLDB session.
  settings          -- Commands for managing LLDB settings.
  source            -- Commands for examining source code described by debug
                       information for the current target process.
  statistics        -- Print statistics about a debugging session
  swift-healthcheck -- Provides logging related to the Swift expression
                       evaluator, including Swift compiler diagnostics. This
                       makes it easier to identify project misconfigurations
                       that result in module import failures in the debugger.
                       The command is meant to be run after a expression
                       evaluator failure has occurred.
  target            -- Commands for operating on debugger targets.
  thread            -- Commands for operating on one or more threads in the
                       current process.
  trace             -- Commands for loading and using processor trace
                       information.
  type              -- Commands for operating on the type system.
  version           -- Show the LLDB debugger version.
  watchpoint        -- Commands for operating on watchpoints.
Current command abbreviations (type 'help command alias' for more info):
  add-dsym  -- Add a debug symbol file to one of the target's current modules
               by specifying a path to a debug symbols file or by using the
               options to specify a module.
  attach    -- Attach to process by ID or name.
  b         -- Set a breakpoint using one of several shorthand formats.
  bt        -- Show the current thread's call stack.  Any numeric argument
               displays at most that many frames.  The argument 'all' displays
               all threads.
  c         -- Continue execution of all threads in the current process.
  call      -- Evaluate an expression on the current thread.  Displays any
               returned value with LLDB's default formatting.
  continue  -- Continue execution of all threads in the current process.
  detach    -- Detach from the current target process.
  di        -- Disassemble specified instructions in the current target.
               Defaults to the current function for the current thread and
               stack frame.
  dis       -- Disassemble specified instructions in the current target.
               Defaults to the current function for the current thread and
               stack frame.
  display   -- Evaluate an expression at every stop (see 'help target
               stop-hook'.)
  down      -- Select a newer stack frame.  Defaults to moving one frame, a
               numeric argument can specify an arbitrary number.
  env       -- Shorthand for viewing and setting environment variables.
  exit      -- Quit the LLDB debugger.
  f         -- Select the current stack frame by index from within the current
               thread (see 'thread backtrace'.)
  file      -- Create a target using the argument as the main executable.
  finish    -- Finish executing the current stack frame and stop after
               returning.  Defaults to current thread unless specified.
  history   -- Dump the history of commands in this session.
               Commands in the history list can be run again using "!<INDEX>".
               "!-<OFFSET>" will re-run the command that is <OFFSET> commands
               from the end of the list (counting the current command).
  image     -- Commands for accessing information for one or more target
               modules.
  j         -- Set the program counter to a new address.
  jump      -- Set the program counter to a new address.
  kill      -- Terminate the current target process.
  l         -- List relevant source code using one of several shorthand formats.
  list      -- List relevant source code using one of several shorthand formats.
  n         -- Source level single step, stepping over calls.  Defaults to
               current thread unless specified.
  next      -- Source level single step, stepping over calls.  Defaults to
               current thread unless specified.
  nexti     -- Instruction level single step, stepping over calls.  Defaults to
               current thread unless specified.
  ni        -- Instruction level single step, stepping over calls.  Defaults to
               current thread unless specified.
  p         -- Evaluate an expression on the current thread.  Displays any
               returned value with LLDB's default formatting.
  parray    -- parray <COUNT> <EXPRESSION> -- lldb will evaluate EXPRESSION to
               get a typed-pointer-to-an-array in memory, and will display
               COUNT elements of that type from the array.
  po        -- Evaluate an expression on the current thread.  Displays any
               returned value with formatting controlled by the type's author.
  poarray   -- poarray <COUNT> <EXPRESSION> -- lldb will evaluate EXPRESSION to
               get the address of an array of COUNT objects in memory, and will
               call po on them.
  print     -- Evaluate an expression on the current thread.  Displays any
               returned value with LLDB's default formatting.
  q         -- Quit the LLDB debugger.
  r         -- Launch the executable in the debugger.
  rbreak    -- Sets a breakpoint or set of breakpoints in the executable.
  re        -- Commands to access registers for the current thread and stack
               frame.
  repl      -- Evaluate an expression on the current thread.  Displays any
               returned value with LLDB's default formatting.
  run       -- Launch the executable in the debugger.
  s         -- Source level single step, stepping into calls.  Defaults to
               current thread unless specified.
  shell     -- Run a shell command on the host.
  si        -- Instruction level single step, stepping into calls.  Defaults to
               current thread unless specified.
  sif       -- Step through the current block, stopping if you step directly
               into a function whose name matches the TargetFunctionName.
  step      -- Source level single step, stepping into calls.  Defaults to
               current thread unless specified.
  stepi     -- Instruction level single step, stepping into calls.  Defaults to
               current thread unless specified.
  t         -- Change the currently selected thread.
  tbreak    -- Set a one-shot breakpoint using one of several shorthand formats.
  undisplay -- Stop displaying expression at every stop (specified by stop-hook
               index.)
  up        -- Select an older stack frame.  Defaults to moving one frame, a
               numeric argument can specify an arbitrary number.
  v         -- Show variables for the current stack frame. Defaults to all
               arguments and local variables in scope. Names of argument,
               local, file static and file global variables can be specified.
               Children of aggregate variables can be specified such as
               'var->child.x'.  The -> and [] operators in 'frame variable' do
               not invoke operator overloads if they exist, but directly access
               the specified element.  If you want to trigger operator
               overloads use the expression command to print the variable
               instead.
               It is worth noting that except for overloaded operators, when
               printing local variables 'expr local_var' and 'frame var
               local_var' produce the same results.  However, 'frame variable'
               is more efficient, since it uses debug information and memory
               reads directly, rather than parsing and evaluating an
               expression, which may even involve JITing and running code in
               the target program.
  var       -- Show variables for the current stack frame. Defaults to all
               arguments and local variables in scope. Names of argument,
               local, file static and file global variables can be specified.
               Children of aggregate variables can be specified such as
               'var->child.x'.  The -> and [] operators in 'frame variable' do
               not invoke operator overloads if they exist, but directly access
               the specified element.  If you want to trigger operator
               overloads use the expression command to print the variable
               instead.
               It is worth noting that except for overloaded operators, when
               printing local variables 'expr local_var' and 'frame var
               local_var' produce the same results.  However, 'frame variable'
               is more efficient, since it uses debug information and memory
               reads directly, rather than parsing and evaluating an
               expression, which may even involve JITing and running code in
               the target program.
  vo        -- Show variables for the current stack frame. Defaults to all
               arguments and local variables in scope. Names of argument,
               local, file static and file global variables can be specified.
               Children of aggregate variables can be specified such as
               'var->child.x'.  The -> and [] operators in 'frame variable' do
               not invoke operator overloads if they exist, but directly access
               the specified element.  If you want to trigger operator
               overloads use the expression command to print the variable
               instead.
               It is worth noting that except for overloaded operators, when
               printing local variables 'expr local_var' and 'frame var
               local_var' produce the same results.  However, 'frame variable'
               is more efficient, since it uses debug information and memory
               reads directly, rather than parsing and evaluating an
               expression, which may even involve JITing and running code in
               the target program.
  x         -- Read from the memory of the current target process.
For more information on any command, type 'help <command-name>'.

lldb一共有这些内容。

3.lldb的启动与退出

3.1启动

有两种方式

lldb clang

在这里插入图片描述
第二种是进去之后再传入文件

在这里插入图片描述

3.2退出

退出同样有两种,分别为exit;

在这里插入图片描述
另一个为quit
在这里插入图片描述

4.list查看源码

list 也可以用l,都可以达到目的

4.1 查看行数

list 10 ;查看当前文件代码的行数的代码

在这里插入图片描述

4.2查看文件代码

list filename

mei

4.3查看函数代码

在这里插入图片描述

5.breakpoint

5.1函数名设置断点

breakpoint set --name [函数名] 可设置断点。 如下图,设置了main函数断点。
在这里插入图片描述
在断点断下来后,我们进行run,就会断到断点这

5.2行号设置断点

breakpoint set --line [行号] s设置断点. 下面演示了在当前文件的第15行设置断点。且断点序号为2

在这里插入图片描述

5.3根据文件名设置断点

breakpoint set --file [文件名] --line [行号]。 下面演示 将断点设置在文件car.hpp中的第17行

在这里插入图片描述

5.3删除所有断点

breakpoint delete可删除所有断点,并提示确认 。

在这里插入图片描述

5.4删除某个断点

使用 breakpoint delete [断点序号] 可删除指定断点。 下面删除序号为2的断点和删除后的断点列表
Copy Highlighter-hljs

breakpoint delete 1

使用 breakpoint delete [n-m] 可删除从 序号n到序号m之间的断点。一个例子

breakpoint delete 2-3

breakpoint delete [序号1] [序号2] [序号3]…

breakpoint delete 5 7 8

5.5查看断点列表

使用breakpoint list 可以查看所有断点。
在这里插入图片描述

5.6启用和禁用断点

可设置多个断点,用这个序号作为唯一标识。

禁用断点:breakpoint disable [序号], 例如, breakpoint disable 1

启动断点:breakpoint enable [序号],例如: breakpoint enable 1

6.run 运行

run命令可以让程序运行起来。
在这里插入图片描述

7.next

使用 next or n 可以单步执行

在这里插入图片描述

8.step 步入

使用 step or s 步入 函数。

在这里插入图片描述
finish 跳出

使用 finish 跳出,返回步入的地方,

9.p和po输出变量

p#
使用p [变量名] 可以查看变量的值

po#
使用 po [指针变量名] 可以查看其值

在这里插入图片描述

10.frame查看变量值

使用frame variable 可以查看当前frame中的所有value
在这里插入图片描述

11.expression 修改变量值

使用expression [变量名]=[value]可以修改变量的值。(做ios tweak 分析时,这招就挺好使)。
例如: expression ret=10.

在这里插入图片描述

12.continue

前面提到,当设置断点后,使用step和next和finish,程序会停下来,需要程序继续运行,键入continue, 程序可自动继续向下执行.

13.memory

13.1 read

在LLDB中,read命令并不是一个内置的命令。可能你想要了解的是如何读取和检查内存,这可以通过memory read(或简写为m r)命令来实现。

memory read命令可以用来读取并显示内存中的数据。它的基本语法如下:

memory read/r [--size <byte-size>] [--format <format>] [--count <count>] <location>

其中:

–size :设置每个数据项的大小,单位是字节。默认是1字节。
–format :设置数据的显示格式。可以是以下之一:b(二进制)、d(十进制)、u(无符号十进制)、o(八进制)、x(十六进制)、f(浮点数)、c(字符)、s(C字符串)、i(指令)。
–count :设置要读取的数据项的数量。
:数据的内存地址。你可以使用表达式来指定地址。
例如,以下命令读取地址为0x1000的内存中的前4个32位整数,并以十六进制格式显示:

13.2 write

在LLDB中,用于写入内存的命令是memory write,简写为m w。

memory write命令的基本语法如下:

memory write <address> <value1> <value2> ...


其中:

:要写入的内存地址。你可以使用表达式来指定地址。 ...:一个或多个要写入的值。值的格式由内存地址处的数据类型确定。 例如,以下命令将值0x12345678写入地址为0x1000的内存:
memory write 0x1000 0x12345678

你也可以一次写入多个值。例如,以下命令将值0x12345678和0x9abcdef0写入地址为0x1000的内存:

memory write 0x1000 0x12345678 0x9abcdef0

你还可以使用表达式来指定地址。例如,以下命令将值’A’写入ptr指针指向的内存:

memory write ptr ‘A’

14.gui vscode lldb调试

首先先去vscode安装codelldb插件

在这里插入图片描述

CodeLLDB
c++/c

首先去文件目录,创建./vscode文件夹然后创建settings.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "lldb-debug",
        "type": "lldb",
        "request": "launch",
        "program": "/Users/besttoyou/CLionProjects/llvm-project的副本/llvm/cmake-build-debug/bin/clang",
        "args": ["/Users/besttoyou/CLionProjects/llvm-project的副本/llvm/cmake-build-debug/bin/main.c"],  // 修改为你的源文件路径
        "cwd": "${workspaceFolder}"
      }
    ]
  }
  

program为你的路径,args,是传递的参数,这样就可以了。

在这里插入图片描述

你可以通过以下步骤在VSCode中下载LLDB调试器: 1. 首先,你需要下载并安装VSCode。你可以在官网上下载Visual Studio Code。 2. 安装完成后,打开VSCode并进入扩展商店。你可以通过在VSCode左侧的侧边栏中点击扩展图标来找到扩展商店。 3. 在扩展商店的搜索栏中输入"CodeLLDB"并点击搜索。CodeLLDB是一个支持LLDB调试器的扩展。 4. 在搜索结果中找到"CodeLLDB"扩展并点击安装。如果因为网络问题无法安装,你可以尝试手动下载该扩展。 5. 安装完成后,你就可以在VSCode中使用LLDB调试器进行C语言调试了。 请注意,LLDB调试器可以同时支持Windows、Linux和MacOS平台的C语言调试。在安装和配置LLDB调试器时,你需要根据你的操作系统和编译器版本进行相应的设置。 参考文献: https://zhuanlan.zhihu.com/p/486021900 https://code.visualstudio.com/ https://zhuanlan.zhihu.com/p/486021900<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vscode使用lldb调试C++程序](https://blog.csdn.net/itas109/article/details/122677006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [vscode调试](https://blog.csdn.net/qq_21237549/article/details/127273684)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二进制科学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值