NDK JNI so 库崩溃分析

JNI开发中,常遇到so库崩溃。我们可以采用以下几种方法定位问题。

通常崩溃log会打印出寄存器信息和堆栈信息。


 

目录下面有如下工具。

NDK JNI so 库崩溃分析_第1张图片

首先要根据不同的平台架构来找到对应的工具

arm、x86_64、或者aarch64、i686

先大概看看各个工具功能:as是汇编编译器、ar用于创建和修改档案文件(.a就是一种档案文件)、ld链接器、nm 列出程序文件的符号和符号在内存中的地址(符合包括函数名和变量名)

 

我们用来定位问题常用的是两个:

addr2line  用来找到地址对应的函数和源文件名和行号、objdump 显示程序文件相关内容和对程序文件进行反汇编。


addr2line  -C -f -e xxx.so 地址  地址

Convert addresses into line number/file name pairs.
 If no addresses are specified on the command line, they will be read from stdin
 The options are:
  @                Read options from
  -a --addresses         Show addresses
  -b --target=  Set the binary file format
  -e --exe=  Set the input file name (default is a.out)
  -i --inlines           Unwind inlined functions 解开内联函数
  -j --section=    Read section-relative offsets instead of addresses 读取相对于段的偏移而非地址
  -p --pretty-print      Make the output easier to read for humans 让输出对人类更可读
  -s --basenames         Strip directory names 去除目录名
  -f --functions         Show function names
  -C --demangle[=style]  Demangle function names 解码函数名
  -h --help              Display this information
  -v --version           Display the program's version


 

arm-linux-androideabi-objdump.exe -dx XX.so > xx.txt

反汇编

Display information from object .
 At least one of the following switches must be given:
  -a, --archive-headers    Display archive header information
  -f, --file-headers       Display the contents of the overall file header
  -p, --private-headers    Display object format specific file header contents
  -P, --private=OPT,OPT... Display object format specific contents
  -h, --[section-]headers  Display the contents of the section headers
  -x, --all-headers        Display the contents of all headers
  -d, --disassemble        Display assembler contents of executable sections
  -D, --disassemble-all    Display assembler contents of all sections
  -S, --source             Intermix source code with disassembly
  -s, --full-contents      Display the full contents of all sections requested
  -g, --debugging          Display debug information in object file
  -e, --debugging-tags     Display debug information using ctags style
  -G, --stabs              Display (in raw form) any STABS info in the file
  -W[lLiaprmfFsoRt] or
  --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,
          =frames-interp,=str,=loc,=Ranges,=pubtypes,
          =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,
          =addr,=cu_index]
                           Display DWARF info in the file
  -t, --syms               Display the contents of the symbol table(s)
  -T, --dynamic-syms       Display the contents of the dynamic symbol table
  -r, --reloc              Display the relocation entries in the file
  -R, --dynamic-reloc      Display the dynamic relocation entries in the file
  @                  Read options from
  -v, --version            Display this program's version number
  -i, --info               List object formats and architectures supported
  -H, --help               Display this information

 The following switches are optional:
  -b, --target=BFDNAME           Specify the target object format as BFDNAME
  -m, --architecture=MACHINE     Specify the target architecture as MACHINE
  -j, --section=NAME             Only display information for section NAME
  -M, --disassembler-options=OPT Pass text OPT on to the disassembler
  -EB --endian=big               Assume big endian format when disassembling
  -EL --endian=little            Assume little endian format when disassembling
      --file-start-context       Include context from start of file (with -S)
  -I, --include=DIR              Add DIR to search list for source files
  -l, --line-numbers             Include line numbers and filenames in output
  -F, --file-offsets             Include file offsets when displaying information
  -C, --demangle[=STYLE]         Decode mangled/processed symbol names
                                  The STYLE, if specified, can be `auto', `gnu',
                                  `lucid', `arm', `hp', `edg', `gnu-v3', `java'
                                  or `gnat'
  -w, --wide                     Format output for more than 80 columns
  -z, --disassemble-zeroes       Do not skip blocks of zeroes when disassembling
      --start-address=ADDR       Only process data whose address is >= ADDR
      --stop-address=ADDR        Only process data whose address is <= ADDR
      --prefix-addresses         Print complete address alongside disassembly
      --[no-]show-raw-insn       Display hex alongside symbolic disassembly
      --insn-width=WIDTH         Display WIDTH bytes on a single line for -d
      --adjust-vma=OFFSET        Add OFFSET to all displayed section addresses
      --special-syms             Include special symbols in symbol dumps
      --prefix=PREFIX            Add PREFIX to absolute paths for -S
      --prefix-strip=LEVEL       Strip initial directory names for -S
      --dwarf-depth=N        Do not display DIEs at depth N or greater
      --dwarf-start=N        Display DIEs starting with N, at the same depth
                             or deeper
      --dwarf-check          Make additional dwarf internal consistency checks.


ndk-stack -sym E:\dev_code\Sosomap-old\Sosomap-jni\obj\local\armeabi -dump D:\android-ndk-r9b-windows-x86\txmap_log.txt

-sym为带符号表的so路径, -dump为crash的堆栈信息,必须包含:********************


输出所有导出函数

arm-linux-androideabi-readelf.exe -a XX.so > xx.txt 


输出.a内所有导出函数。

arm-linux-androideabi-ar.exe -t xx.a > xx.txt

arm-linux-androideabi-nm.exe xx.a > xx.txt

你可能感兴趣的:(ndk)