2018-09-18
<article>
<div class="article_content clearfix csdn-tracking-statistics" id="article_content" data-mod="popu_307" data-dsm="post" data-pid="blog">
<div class="article-copyright">
版权声明:原创文章,欢迎分享,标明出处。 https://blog.csdn.net/tom_221x/article/details/46789415 </div>
<link href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-e2445db1a8.css" rel="stylesheet">
<div class="htmledit_views">
<p>使用NDK编写native code时候,真机常会出现crash的错误。然后logcat就是一片错误堆栈。</p>
<p></p>
<pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs"><ol class="hljs-ln" style="width:1468px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): signal <span class="hljs-number">11</span> (SIGSEGV), code <span class="hljs-number">2</span> (SEGV_ACCERR), fault addr <span class="hljs-number">40123</span>d5c</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): backtrace:</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): #<span class="hljs-number">00</span> pc <span class="hljs-number">00000</span>d5c <unknown></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): #<span class="hljs-number">01</span> pc <span class="hljs-number">0009f</span>8db /system/lib/libstagefright.so (android::SmoothStreamingExtractor::SmoothStreamingExtractor(android::sp<android::DataSource> <span class="hljs-keyword">const</span>&)+<span class="hljs-number">366</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): #<span class="hljs-number">02</span> pc <span class="hljs-number">00082f</span>89 /system/lib/libstagefright.so (android::MediaExtractor::Create(android::sp<android::DataSource> <span class="hljs-keyword">const</span>&, <span class="hljs-keyword">char</span> <span class="hljs-keyword">const</span>)+<span class="hljs-number">500</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): #<span class="hljs-number">03</span> pc <span class="hljs-number">00045</span>cdf /system/lib/libmediaplayerservice.so (android::AmlogicPlayerExtractorDemux::AmlogicPlayerExtractorDemux(AVFormatContext)+<span class="hljs-number">438</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): #<span class="hljs-number">04</span> pc <span class="hljs-number">0004602</span>d /system/lib/libmediaplayerservice.so (android::AmlogicPlayerExtractorDemux::extractor_read_header(AVFormatContext, AVFormatParameters)+<span class="hljs-number">20</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">I/DEBUG ( <span class="hljs-number">2562</span>): #<span class="hljs-number">05</span> pc <span class="hljs-number">00102210</span> /system/lib/libamplayer.so (avformat_open_input_header+<span class="hljs-number">604</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">....</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
曾经一直用log的方式查看揣测错误的代码行数。熟不知这些看似淫乱的错误日志其实就是错误堆栈的相关信息,只需要一个工具转换即可。
<p></p>
<p></p>
<pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs">android-ndk-r10\toolchains\arm-linux-androideabi<span class="hljs-number">-4.8</span>\prebuilt\windows-x86_64\bin\arm-linux-androideabi-addr2line</code><div class="hljs-button" data-title="复制"></div></pre>
<span style="font-family:Verdana, Arial, Helvetica, '宋体', sans-serif;color:#595959;"><span style="font-size:14px;line-height:22.3999996185303px;">使用方式很简单</span></span>
<p></p>
<p><span style="font-family:Verdana, Arial, Helvetica, '宋体', sans-serif;color:#595959;"><span style="font-size:14px;line-height:22.3999996185303px;"></span></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">// -f 输出函数名</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">// -e 输出错误代码行数和文件路径</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">// xxx.so 对应出错的so文件, 在android工程obj目录下</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">// addr 是具体的地址</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">arm-linux-androideabi-addr2line -f -e xxx.so addr</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p></p>
<pre></pre>
错误日志中backtracer就是堆栈信息,#00 #01 就是堆栈列表。 #00 就是堆栈顶层就是错误所在地址,pc后面的就是地址00000d5c
<p></p>
<p><span style="font-family:Verdana, Arial, Helvetica, '宋体', sans-serif;color:#595959;"><span style="font-size:14px;line-height:22.3999996185303px;"></span></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs">arm-linux-androideabi-addr2line -f -e /system/lib/libstagefright.so <span class="hljs-number">00000</span>d5c</code><div class="hljs-button" data-title="复制"></div></pre>
<p></p>
<pre></pre>
<p>这样瞬间就能定位错误了。。。</p>
<p>注意这个so文件是android工程obj目录里面的,而不是libs里面的。</p>
<p>obj目录是带有debug信息的库文件,libs的库文件是没有debug信息的,addr2line无法读取源代码信息。</p>
<p><span style="font-family:Verdana, Arial, Helvetica, '宋体', sans-serif;color:rgb(89,89,89);"><span style="font-size:14px;line-height:22.3999996185303px;">
</span></span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, '宋体', sans-serif;color:#595959;"><span style="font-size:14px;line-height:22.3999996185303px;">
</span></span></p>
<p>
</p>
</div>
</div>
</article>