学习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>'.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容