Clang
Clang: a C language family frontend for LLVM
LLVM的C語(yǔ)言家族前端
編譯可執(zhí)行文件
clang HelloWorld.m -o HelloWorld -framework Foundation
將OC代碼轉(zhuǎn)換成C++代碼
將OC代碼轉(zhuǎn)換成C++代碼來(lái)剖析具體的底層實(shí)現(xiàn)直焙。
$ xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp
上面命令將main.m文件轉(zhuǎn)換成iOS系統(tǒng)下64位架構(gòu)(以下討論均在64位架構(gòu)基礎(chǔ)上進(jìn)行)的C++代碼沽损。
xcrun 命令
xcode安裝的時(shí)候順帶安裝了xcrun命令,xcrun命令在clang的基礎(chǔ)上進(jìn)行了一些封裝换淆,要更好用一些。
##### 在模擬器下編譯
xcrun -sdk iphonesimulator clang -rewrite-objc main.m
#在真機(jī)下編譯
xcrun -sdk iphoneos clang -rewrite-objc main.m
Debugger commands
在xcode的console中輸入help可查看所有自帶命令
常用命令:
1. po
po --Evaluate an expression on the current thread. Displays any returned value with formatting controlled by the type's author.
在當(dāng)前線程上求一個(gè)表達(dá)式。顯示任何返回值,其格式由類(lèi)型的創(chuàng)作者控制
2. p & print
p -- Evaluate an expression on the current thread. Displays any returned value with LLDB's default formatting.
print -- Evaluate an expression on the current thread. Displays any returned value with LLDB's default formatting.
在當(dāng)前線程上求一個(gè)表達(dá)式簇爆。以LLDB的默認(rèn)格式顯示任何返回的值
p的隱藏功能
//默認(rèn)打印為10進(jìn)制
(lldb) p 100
(int) $8 = 100
//轉(zhuǎn)16進(jìn)制
(lldb) p/x 100
(int) $9 = 0x00000064
//轉(zhuǎn)8進(jìn)制
(lldb) p/o 100
(int) $10 = 0144
//轉(zhuǎn)二進(jìn)制
(lldb) p/t 100
(int) $2 = 0b00000000000000000000000001100100
//字符轉(zhuǎn)10進(jìn)制數(shù)字
(lldb) p/d 'A'
(char) $7 = 65
//10進(jìn)制數(shù)字轉(zhuǎn)字符
(lldb) p/c 66
(int) $10 = B\0\0\0
3. x
x -- Read from the memory of the current target process.
從當(dāng)前目標(biāo)進(jìn)程的內(nèi)存中讀取。
GLPerson *p = [GLPerson alloc];
p.height = 180;
p.name = @"loong";
p.char1 = 'g';
p.char2 = 'n';
p.weight = 150;
--- 上面是代碼
(lldb) x p
0x600003c5ff60: 18 17 3b 06 01 00 00 00 67 6e 00 00 b4 00 00 00 ..;.....gn......
0x600003c5ff70: 96 00 00 00 00 00 00 00 40 f0 3a 06 01 00 00 00 ........@.:.....
幾組數(shù)據(jù)
- 數(shù)字4代表輸出4組數(shù)據(jù)
輸出格式
- x :代表16進(jìn)制
- f :代表浮點(diǎn)數(shù)
- d :代表10進(jìn)制
字節(jié)大小
- b :byte 代表1個(gè)字節(jié)
- h :half word 代表2個(gè)字節(jié)
- w :word 代表4個(gè)字節(jié)
- g :giant word 代表8個(gè)字節(jié)
4. bt
bt -- Show the current thread's call stack. Any numeric argument displays at most that many frames. The argument 'all' displays all threads.
顯示當(dāng)前線程的調(diào)用堆棧爽撒。任何數(shù)字參數(shù)最多顯示多少幀冕碟。參數(shù)“ all”顯示所有線程
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x0000000100000b81 KCObjc`main(argc=1, argv=0x00007ffeefbff578) at main.m:27:27 [opt]
frame #1: 0x00007fff6acf2cc9 libdyld.dylib`start + 1
frame #2: 0x00007fff6acf2cc9 libdyld.dylib`start + 1
命令一覽
Debugger commands:
apropos -- List debugger commands related to a word or subject.
breakpoint -- Commands for operating on breakpoints (see 'help b' for
shorthand.)
bugreport -- Commands for creating domain-specific bug reports.
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.
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.
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.
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
target -- Commands for operating on debugger targets.
thread -- Commands for operating on one or more threads in the
current process.
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 using the
options to specify a module to download symbols for.
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.
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 -- Evaluate an expression on the current thread. Displays any
returned value with LLDB's default formatting.
po -- Evaluate an expression on the current thread. Displays any
returned value with formatting controlled by the type's author.
poarray -- Evaluate an expression on the current thread. Displays any
returned value with LLDB's default formatting.
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.
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>'.