學習iOS開發調試LLDB

聲明 是閱讀標哥技術博客的做的個人筆記

1、常用的調試命令 p、po、、print、call

對比一下四個命令的區別

p         -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
               the current program context, using user defined variables and
               variables currently in scope.
po        -- ('expression -O  -- ')  Evaluate an expression (ObjC++ or Swift)
               in the current program context, using user defined variables and
               variables currently in scope.
print     -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
               the current program context, using user defined variables and
               variables currently in scope.
call      -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
               the current program context, using user defined variables and
               variables currently in scope.

從官方描述來看 p、print、call是一樣 但是po就很不一樣了,輸入一樣但是輸出不一樣,po的輸出的是具體對象的內容。
按照特定的格式打印,如下:

(lldb) p/s blogName
(__NSCFConstantString *) $9 = @"me的技術博客"
(lldb) p/x blogName
(__NSCFConstantString *) $10 = 0x000000010921c0a8 @"me的技術博客"
(lldb) p/t blogName
(__NSCFConstantString *) $11 = 0b0000000000000000000000000000000100001001001000011100000010101000 @"me的技術博客"
(lldb) p/a blogName
(__NSCFConstantString *) $12 = 0x000000010921c0a8 @ @"me的技術博客"

2、lldb生命變量

在調試中,可以用e命令定義變量,方便在調試中使用
例子如下

(lldb) e NSString *$str = @"http://www.baidu.com"
(lldb) po $str
http://www.baidu.com
 
(lldb) e int $count = 10
(lldb) p $count
(int) $count = 10
(lldb) e NSArray *itemArray = @[@"Test", @"Demo", @"-------"]
(lldb) po $count
10

使用e聲明了$str變量,然后就可以使用了。再通過p命令打印出來的都是用$開頭的變量

3、調用變量API

在我們斷點某處時,打印上下環境中的某個變量調用函數的輸出結果

(lldb) po [blogName uppercaseString]
me的技術博客
 
(lldb) po [blogName substringFromIndex:2]
的技術博客

4、強轉返回值類型

和我們在coding的時候的使用強轉的方法是一樣的

5、斷點

我們可以在開始調試的時候 通過b命令

(lldb) b   33
Breakpoint 9: where = OCLLDBDebugDemo`-[ViewController onButtonClicked:] + 53 at ViewController.m:33, address = 0x000000010921a6d5

也可以這樣加斷點

(lldb) b  -[ViewController onButtonClicked:]
Breakpoint 4: where = OCLLDBDebugDemo`-[ViewController onButtonClicked:] + 53 at ViewController.m:33, address = 0x000000010921a6d5
 

不過自認為還是b加行號

6、設置觸發條件

先上圖

在NSLog打印的這一行,設置了條件,使其條件滿足的時候,斷點起作用,在這里是讓其發出聲音并且打印到控制臺

這種場景主要是用做替代單步跟蹤減少一步步的調試的問題,只是調試我們比較關心的條件下的調試。

7、打印試圖的層次結構

為了在調試的過程中,掌握試圖的層次結構以及位置等信息,我們可以使用調用recursiveDescription方法來打印出來,層次結構一目了然

(lldb) po [self.view recursiveDescription]
<UIView: 0x7fdd1052af10; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7fdd1052b290>>
   | <UIButton: 0x7fdd10529070; frame = (66 183; 188 40); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fdd1051bff0>>
   |    | <UIButtonLabel: 0x7fdd104162f0; frame = (41.5 11; 105 18); text = '標哥的技術博客'; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fdd10412590>>
   |    |    | <_UILabelContentLayer: 0x7fdd12804f30> (layer)
   | <_UILayoutGuide: 0x7fdd1052b300; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7fdd1052b710>>
   | <_UILayoutGuide: 0x7fdd1052c070; frame = (0 568; 0 0); hidden = YES; layer = <CALayer: 0x7fdd1052c200>>
 

8、臨時刷新界面UI

本例使用的是在controller中調試中改變view的背景色

(lldb) e self.view.backgroundColor = [UIColor yellowColor]**
(UICachedDeviceRGBColor *) $0 = 0x00007ff071f3dba0
(lldb) e (void)[CATransaction flush]**

執行完以上命令后 界面view變為黃色

9、 修改變量的值

在調試過程可以使用expr改變變量的值
使用規則

expr variable = newValue

10、其他調試的命令(通過help打印出來的可以起看看)



(lldb) help
Debugger commands:

  apropos           -- Find a list of debugger commands related to a particular
                       word/subject.
  breakpoint        -- A set of commands for operating on breakpoints. Also see
                       _regexp-break.
  command           -- A set of commands for managing or customizing the
                       debugger commands.
  disassemble       -- Disassemble bytes in the current function, or elsewhere
                       in the executable program as specified by the user.
  expression        -- Evaluate an expression (ObjC++ or Swift) in the current
                       program context, using user defined variables and
                       variables currently in scope.
  frame             -- A set of commands for operating on the current thread's
                       frames.
  gdb-remote        -- Connect to a remote GDB server.  If no hostname is
                       provided, localhost is assumed.
  gui               -- Switch into the curses based GUI mode.
  help              -- Show a list of all debugger commands, or give details
                       about specific commands.
  kdp-remote        -- Connect to a remote KDP server.  udp port 41139 is the
                       default port number.
  language          -- A set of commands for managing language-specific
                       functionality.'.
  log               -- A set of commands for operating on logs.
  memory            -- A set of commands for operating on memory.
  platform          -- A set of commands to manage and create platforms.
  plugin            -- A set of commands for managing or customizing plugin
                       commands.
  process           -- A set of commands for operating on a process.
  quit              -- Quit out of the LLDB debugger.
  register          -- A set of commands to access thread registers.
  script            -- Pass an expression to the script interpreter for
                       evaluation and return the results. Drop into the
                       interactive interpreter if no expression is given.
  settings          -- A set of commands for manipulating internal settable
                       debugger variables.
  source            -- A set of commands for accessing source file information
  target            -- A set of commands for operating on debugger targets.
  thread            -- A set of commands for operating on one or more threads
                       within a running process.
  type              -- A set of commands for operating on the type system
  version           -- Show version of LLDB debugger.
  watchpoint        -- A set of commands for operating on watchpoints.

Current command abbreviations (type 'help command alias' for more info):

  add-dsym  -- ('target symbols add')  Add a debug symbol file to one of the
               target's current modules by specifying a path to a debug symbols
               file, or using the options to specify a module to download
               symbols for.
  attach    -- ('_regexp-attach')  Attach to a process id if in decimal,
               otherwise treat the argument as a process name to attach to.
  b         -- ('_regexp-break')  Set a breakpoint using a regular expression
               to specify the location, where <linenum> is in decimal and
               <address> is in hex.
  bt        -- ('_regexp-bt')  Show a backtrace.  An optional argument is
               accepted; if that argument is a number, it specifies the number
               of frames to display.  If that argument is 'all', full
               backtraces of all threads are displayed.
  c         -- ('process continue')  Continue execution of all threads in the
               current process.
  call      -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
               the current program context, using user defined variables and
               variables currently in scope.
  continue  -- ('process continue')  Continue execution of all threads in the
               current process.
  detach    -- ('process detach')  Detach from the current process being
               debugged.
  di        -- ('disassemble')  Disassemble bytes in the current function, or
               elsewhere in the executable program as specified by the user.
  dis       -- ('disassemble')  Disassemble bytes in the current function, or
               elsewhere in the executable program as specified by the user.
  display   -- ('_regexp-display')  Add an expression evaluation stop-hook.
  down      -- ('_regexp-down')  Go down "n" frames in the stack (1 frame by
               default).
  env       -- ('_regexp-env')  Implements a shortcut to viewing and setting
               environment variables.
  exit      -- ('quit')  Quit out of the LLDB debugger.
  f         -- ('frame select')  Select a frame by index from within the
               current thread and make it the current frame.
  file      -- ('target create')  Create a target using the argument as the
               main executable.
  finish    -- ('thread step-out')  Finish executing the function of the
               currently selected frame and return to its call site in
               specified thread (current thread, if none specified).
  image     -- ('target modules')  A set of commands for accessing information
               for one or more target modules.
  j         -- ('_regexp-jump')  Sets the program counter to a new address.
  jump      -- ('_regexp-jump')  Sets the program counter to a new address.
  kill      -- ('process kill')  Terminate the current process being debugged.
  l         -- ('_regexp-list')  Implements the GDB 'list' command in all of
               its forms except FILE:FUNCTION and maps them to the appropriate
               'source list' commands.
  list      -- ('_regexp-list')  Implements the GDB 'list' command in all of
               its forms except FILE:FUNCTION and maps them to the appropriate
               'source list' commands.
  n         -- ('thread step-over')  Source level single step in specified
               thread (current thread, if none specified), stepping over calls.
  next      -- ('thread step-over')  Source level single step in specified
               thread (current thread, if none specified), stepping over calls.
  nexti     -- ('thread step-inst-over')  Single step one instruction in
               specified thread (current thread, if none specified), stepping
               over calls.
  ni        -- ('thread step-inst-over')  Single step one instruction in
               specified thread (current thread, if none specified), stepping
               over calls.
  p         -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
               the current program context, using user defined variables and
               variables currently in scope.
  po        -- ('expression -O  -- ')  Evaluate an expression (ObjC++ or Swift)
               in the current program context, using user defined variables and
               variables currently in scope.
  print     -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
               the current program context, using user defined variables and
               variables currently in scope.
  q         -- ('quit')  Quit out of the LLDB debugger.
  r         -- ('process launch -X true --')  Launch the executable in the
               debugger.
  rbreak    -- ('breakpoint set -r %1')  Sets a breakpoint or set of
               breakpoints in the executable.
  repl      -- ('expression -r  -- ')  Evaluate an expression (ObjC++ or Swift)
               in the current program context, using user defined variables and
               variables currently in scope.
  run       -- ('process launch -X true --')  Launch the executable in the
               debugger.
  s         -- ('thread step-in')  Source level single step in specified thread
               (current thread, if none specified).
  si        -- ('thread step-inst')  Single step one instruction in specified
               thread (current thread, if none specified).
  step      -- ('thread step-in')  Source level single step in specified thread
               (current thread, if none specified).
  stepi     -- ('thread step-inst')  Single step one instruction in specified
               thread (current thread, if none specified).
  t         -- ('thread select')  Select a thread as the currently active
               thread.
  tbreak    -- ('_regexp-tbreak')  Set a one shot breakpoint using a regular
               expression to specify the location, where <linenum> is in
               decimal and <address> is in hex.
  undisplay -- ('_regexp-undisplay')  Remove an expression evaluation
               stop-hook.
  up        -- ('_regexp-up')  Go up "n" frames in the stack (1 frame by
               default).
  x         -- ('memory read')  Read from the memory of the process being
               debugged.

For more information on any command, type 'help <command-name>'.
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容