macOS开发命令集

一、说明

本文收集相关命令用于方便查询。仅供参考。

二、命令

clang

1
2
3
4
5
//编译代码
$ clang -framework Foundation Calculator.m main.m -o prog
//编译
$ clang -fobjc-arc –framework Foundation filename.m -o output_filename

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//clang -rewrite-objc 的作用是把oc代码转写成c/c++代码,我们常用它来窥探OC的底层实现。
$ clang -rewrite-objc main.m

//1. 指定真机
$ xcrun -sdk iphoneos clang -rewrite-objc main.m
//2. 指定模拟器
$ xcrun -sdk iphonesimulator clang -rewrite-objc main.m

//3. 指定指令集
$ xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m

//4. 指定版本
//>>> 显示版本
$ xcodebuild -showsdks
iOS SDKs:
iOS 11.4 -sdk iphoneos11.4
iOS Simulator SDKs:
Simulator - iOS 11.4 -sdk iphonesimulator11.4

$ xcrun -sdk iphonesimulator11.4 clang -rewrite-objc main.m

//5. 指定framework
$ xcrun -sdk iphonesimulator11.4 clang -rewrite-objc -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/Foundation.framework list5-7.mm

//6. 遇到weak编译错误
//annot create __weak reference because the current deployment target does not
support weak references
$ clang -rewrite-objc -fobjc-arc -stdlib=libc++ -mmacosx-version-min=10.13 -fobjc-runtime=macosx-10.13 -Wno-deprecated-declarations main.m

malloc_history

displays backtrace data showing exactly where your program made calls to the malloc and free functions

heap

displays a snapshot of the memory allocated by the malloc library and located in the address space of a specified process.

leaks

searches the virtual memory space of a process for buffers that were allocated by malloc but are no longer referenced.

codesign

签名

lipo

lipo:常用于多架构Mach-O文件的处理

1
2
3
4
5
6
7
8
9
10
//创建一个静态库
$ lipo -create ***.a ****.a -output *.a

//查看架构信息,文件为.a静态库或其他可执行文件等)
$ lipo -info 文件路径

//导出某种特定架构
$ lipo 文件路径 -thin 架构类型 -output 输出文件路径
//合并多种架构
$ lipo 文件路径1 文件路径2 -output 输出文件路径

vm_stat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 26350.
Pages active: 1385959.
Pages inactive: 1378292.
Pages speculative: 152.
Pages throttled: 0.
Pages wired down: 899878.
Pages purgeable: 90247.
"Translation faults": 567807817.
Pages copy-on-write: 85510141.
Pages zero filled: 234110938.
Pages reactivated: 11307835.
Pages purged: 10787844.
File-backed pages: 749698.
Anonymous pages: 2014705.
Pages stored in compressor: 4300907.
Pages occupied by compressor: 503328.
Decompressions: 15708837.
Compressions: 27061167.
Pageins: 19875545.
Pageouts: 90809.
Swapins: 4627714.
Swapouts: 5852629.

otool

otool查看Mach-O特定部分和段的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ otool -lV a
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
MH_MAGIC_64 X86_64 ALL LIB64 EXECUTE 15 1200 NOUNDEFS DYLDLINK
TWOLEVEL PIE

Load command 0 //加载命令0
cmd LC_SEGMENT_64 //命令是:LC_SEGMENT_64
cmdsize 72
segname __PAGEZERO
vmaddr 0x0000000000000000
vmsize 0x0000000100000000
........
flags (none)

其他指令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 //显示文件头信息   
$ otool -hV Mach-O文件

//强制otool显示32位文件头
$ otool -arch i386 -hV Mach-O文件


//查看一个quick look的插件,类型为 MH_BUNDLE
$ otool -h /System/Library/QuickLook/PDF.qlgenerator/Contents/MacOS/PDF
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
0xfeedfacf 16777223 3 0x00 8 21 2712 0x00000085

//通过V,verbose输出
$ otool -hV /System/Library/QuickLook/PDF.qlgenerator/Contents/MacOS/PDF


//打印动态库
$ otool -L Mach-O文件

##jtool

otool 工具善于分析加载命令和文本段,但是不适合分析数据段和其他区域。jtool则比otool更强。

下载之后,将jtool放置到执行环境的路径即可。

1
2
3
4
5
6
//环境配置
....将jtool放到环境变量配置的路径中。
$ source ~/.bash_profile
$ source ~/.profile
//jtool命令
$ jtool -l /bin/ls

jtool 查看Mach-O特定部分和段的内容

1
2
3
4
5
6
7
8
9
$ jtool -l a
LC 00: LC_SEGMENT_64 Mem: 0x000000000-0x100000000 __PAGEZERO
LC 01: LC_SEGMENT_64 Mem: 0x100000000-0x100001000 __TEXT
Mem: 0x100000f30-0x100000f6b __TEXT.__text (Normal)
Mem: 0x100000f6c-0x100000f78 __TEXT.__stubs (Symbol Stubs)
Mem: 0x100000f78-0x100000f9c __TEXT.__stub_helper (Normal)
Mem: 0x100000f9c-0x100000fb0 __TEXT.__cstring (C-String Literals)
Mem: 0x100000fb0-0x100000ff8 __TEXT.__unwind_info
......

xcrun

1
2
//重新编译
$ xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc OC源文件 -o 输出的CPP文件

参考

  1. Memory Usage Performance Guidelines-malloc_history、heap、leaks