<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; debug</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/debug/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Build a Debug Enabled Erlang RunTime System</title>
		<link>http://blog.yufeng.info/archives/556</link>
		<comments>http://blog.yufeng.info/archives/556#comments</comments>
		<pubDate>Fri, 07 May 2010 07:03:59 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[Erts]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=556</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: How to Build a Debug Enabled Erlang RunTime System 很多朋友在问如何调试Erlang的驱动代码等等，其实otp源码下的INSTALL.md写的很清楚， 摘抄下： How to Build a Debug Enabled Erlang RunTime System &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; After completing all the normal building steps described above a debug enabled runtime system can be built. To do this you have to change directory to `$ERL_TOP/erts/emulator`. 注：一定要注意这句话， 假设你现在在otp源码目录下，正常编译好了， export ERL_TOP=`pwd` [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/556">How to Build a Debug Enabled Erlang RunTime System</a></p>
</div>
<p>很多朋友在问如何调试Erlang的驱动代码等等，其实otp源码下的INSTALL.md写的很清楚， 摘抄下：<br />
How to Build a Debug Enabled Erlang RunTime System<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>After completing all the normal building steps described above a debug<br />
enabled runtime system can be built. To do this you have to change<br />
directory to `$ERL_TOP/erts/emulator`.<br />
注：一定要注意这句话， 假设你现在在otp源码目录下，正常编译好了， export ERL_TOP=`pwd` 然后进入erts/emulator目录下</p>
<p>In this directory execute:</p>
<p>    $ make debug FLAVOR=$FLAVOR</p>
<p>where `$FLAVOR` is either `plain` or `smp`. The flavor options will<br />
produce a beam.debug and beam.smp.debug executable respectively. The<br />
files are installed along side with the normal (opt) versions `beam.smp`<br />
and `beam`.</p>
<p>To start the debug enabled runtime system execute:</p>
<p>    $ $ERL_TOP/bin/cerl -debug</p>
<p>The debug enabled runtime system features lock violation checking,<br />
assert checking and various sanity checks to help a developer ensure<br />
correctness. Some of these features can be enabled on a normal beam<br />
using appropriate configure options.</p>
<p>There are other types of runtime systems that can be built as well<br />
using the similar steps just described.</p>
<p>    $ make $TYPE FLAVOR=$FLAVOR</p>
<p>where `$TYPE` is `opt`, `gcov`, `gprof`, `debug`, `valgrind`, or `lcnt`.<br />
These different beam types are useful for debugging and profiling<br />
purposes.</p>
<p>祝玩的开心！
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/556/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>escript的高级特性</title>
		<link>http://blog.yufeng.info/archives/153</link>
		<comments>http://blog.yufeng.info/archives/153#comments</comments>
		<pubDate>Tue, 24 Nov 2009 21:45:55 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[escript]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=153</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: escript的高级特性 escript Erlang scripting support, 可以让erl模块转身变成unix script来使用，大大方便用户，具体的使用参看otp文档。我这里要演示的是些比较被忽视的高级特性: 首先crack erts/etc/common/escript.c:33 static int debug = 1; 让之显示调用参数。 CTRL+D [color=red]erl +B -boot start_clean -noshell -smp enable -sname factorial -mnesia debug verbose -run escript start -extra ./factorial 10[/color] factorial 10 = 3628800 特性1： 摘抄文档。。。 On the third line (or second line depending on the presence of [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/153">escript的高级特性</a></p>
</div>
<p>escript Erlang scripting support, 可以让erl模块转身变成unix script来使用，大大方便用户，具体的使用参看otp文档。我这里要演示的是些比较被忽视的高级特性:</p>
<p>首先crack erts/etc/common/escript.c:33  static int debug = 1; 让之显示调用参数。</p>
<pre class="brush: bash; title: ; notranslate">root@nd-desktop:~# cat &amp;gt;factorial</pre>
<pre class="brush: erlang; title: ; notranslate">
[color=red]#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose[/color]
main([String]) -&gt;
try
N = list_to_integer(String),
F = fac(N),
io:format(&quot;factorial ~w = ~w\n&quot;, [N,F])
catch
_:_ -&gt;
usage()
end;
main(_) -&gt;
usage().

usage() -&gt;
io:format(&quot;usage: factorial integer\n&quot;),
halt(1).

fac(0) -&gt; 1;
fac(N) -&gt; N * fac(N-1).
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~# chmod +x factorial
root@nd-desktop:~#　./factorial  10
</pre>
<p>[color=red]erl +B -boot start_clean -noshell -smp enable -sname factorial -mnesia debug verbose -run escript start -extra ./factorial 10[/color]<br />
factorial 10 = 3628800</p>
<p><strong>特性1：</strong></p>
<p>摘抄文档。。。<br />
On the third line (or second line depending on the presence of the Emacs directive), it is possible to give arguments to the emulator, such as</p>
<p>%%! -smp enable -sname factorial -mnesia debug verbose</p>
<p>Such an argument line must start with %%! and the rest of the line will interpreted as arguments to the emulator.</p>
<p>我们可以看到　这些选项被传递给了 erl</p>
<p><strong>特性2： </strong><br />
-mode(compile).</p>
<p>这个选项是在escript.erl这个模块处理的。默认情况下 escript是被解释执行的，如果你的脚本很复杂，那么效率估计会是瓶颈。这种情况下， 你可以通过这个选项来让escript来先编译你的模块成opcode, 在vm里面运行。</p>
<p><strong>特性3：</strong><br />
-d 选项 用来调试script的<br />
-c  编译执行<br />
-i  解释执行<br />
-s  只检查不执行<br />
root@nd-desktop:~#　escript -d ./factorial  10<br />
我们就可以看到 调试界面如下图<br />
<a href="http://blog.yufeng.info/wp-content/uploads/2009/11/escript_debug.PNG"><img class="alignnone size-medium wp-image-154" title="escript_debug" src="http://blog.yufeng.info/wp-content/uploads/2009/11/escript_debug-275x300.PNG" alt="escript_debug" width="275" height="300" /></a></p>
<p><strong>特性4：</strong><br />
可以把一个beam文件作为script</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(hello).
-export([start/0,main/1]).

main(_)-&gt;
start().

start()-&gt;
io:format(&quot;hello world~n&quot;,[]).
</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# erlc hello.erl
root@nd-desktop:/usr/src# cat &amp;gt;hello
</pre>
<pre class="brush: erlang; title: ; notranslate">
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.beam &amp;gt;&amp;gt;hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world
</pre>
<p><strong>特性5:</strong><br />
可以把一个zip文件作为script</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(hello).
-export([start/0,main/1]).

main(_)-&gt;
start().

start()-&gt;
io:format(&quot;hello world, fac(10)=~w ~n&quot;,[fac:fac(10)]).</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat fac.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(fac).
-export([fac/1]).

fac(0) -&gt;
1;
fac(N) -&gt; N * fac(N-1).</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# erlc *.erl
root@nd-desktop:/usr/src# erl
Erlang R13B03 (erts-5.7.4)
[smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1&amp;gt; zip:zip(&quot;hello.zip&quot;, [&quot;hello.beam&quot;, &quot;fac.beam&quot;]).
{ok,&quot;hello.zip&quot;}
2&amp;gt;
</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat &amp;gt;hello
</pre>
<pre class="brush: erlang; title: ; notranslate">
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose -escript main hello
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.zip &amp;gt;&amp;gt;hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world, fac(10)=3628800
</pre>
<p><strong>特性6：</strong><br />
在独立的包里面 把escript伪装成我们的应用程序</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat &amp;gt;hello.escript
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(hello).
-export([start/0,main/1]).

main(_)-&gt;
start().

start()-&gt;
io:format(&quot;hello world~n&quot;,[]).
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src#  cp `which escript` hello
root@nd-desktop:/usr/src# ./hello
hello world
</pre>
<p>规则是 escript 改名成xxxx 执行xxxx的时候 实际上要读取的脚本是 xxxx.escript</p>
<p><strong>综述： escript是很强大的 未来的erlang standalone全靠它。</strong></p>
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/153/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>如何用gdb调试erlang运行期（高级）</title>
		<link>http://blog.yufeng.info/archives/48</link>
		<comments>http://blog.yufeng.info/archives/48#comments</comments>
		<pubDate>Sun, 11 Oct 2009 06:00:38 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[cerl]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[etp]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[term]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=48</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: 如何用gdb调试erlang运行期（高级） 前些天在erlang的源码里面闲逛的时候发现了 bin目录下的cerl，一看原来是个调试的高级货。 我之前写过一篇文章http://mryufeng.javaeye.com/blog/113852 如何调试erlang 但是这是土八路的方法， cerl才是现代化工业。 # This is a script to start Erlang/OTP for debugging. PATH is set to # include this script so if slave nodes are started they will use this # script as well. # # usage: cerl [ OPTIONS ] [ ARGS ] # # The [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/48">如何用gdb调试erlang运行期（高级）</a></p>
</div>
<p>前些天在erlang的源码里面闲逛的时候发现了 bin目录下的cerl，一看原来是个调试的高级货。</p>
<p>我之前写过一篇文章http://mryufeng.javaeye.com/blog/113852 如何调试erlang 但是这是土八路的方法， cerl才是现代化工业。</p>
<p># This is a script to start Erlang/OTP for debugging. PATH is set to<br />
# include this script so if slave nodes are started they will use this<br />
# script as well.<br />
#<br />
#  usage:  cerl [ OPTIONS ] [ ARGS ]<br />
#<br />
#  The OPTIONS are<br />
#<br />
#   -rootdir $MYROOTDIR<br />
#               Run an installed emulator built from this source<br />
#   -debug      Run debug compiled emulator<br />
#   -gdb        Run the debug compiled emulator in emacs and gdb.<br />
#               You have to start beam in gdb using &#8220;run&#8221;.<br />
#   -break F    Run the debug compiled emulator in emacs and gdb and set break.<br />
#               The session is started, i.e. &#8220;run&#8221; is already don for you.<br />
#   -xxgdb      FIXME currently disabled<br />
#   -purify     Run emulator compiled for purify<br />
#   -quantify   Run emulator compiled for quantify<br />
#   -purecov    Run emulator compiled for purecov<br />
#   -gcov       Run emulator compiled for gcov<br />
#   -valgrind   Run emulator compiled for valgrind<br />
#   -lcnt Run emulator compiled for lock counting<br />
#   -nox        Unset the DISPLAY variable to disable us of X Windows<br />
#</p>
<p>要使用cerl 我们最好准备个调试版本的erlang。R13B 修正了些编译错误,可以编译出debug版本的系统：./configure &#038;&#038; make TYPE＝debug &#038;&#038; make</p>
<p>这样就生成了个beam.debug的运行期。</p>
<p>我们要调试的时候 就可以在otp的binx目录下运行  cerl -debug -gdb -break main</p>
<p>这时候cerl自动会加载 emacs 启动 gud,  整个过程都是自动的。但是这个脚本有点小问题， gud模型下没有把源码和当前对应的调试对应起来。可以通过以下方式修正：</p>
<pre class="brush: bash; title: ; notranslate">
yu-fengdemacbook-2:bin yufeng$ diff cerl cerl2
284c284
&lt;     exec $EMACS --eval &quot;(progn (gdb \&quot;gdb $EMU\&quot;) $gdbcmd)&quot;
---
&gt;     exec $EMACS --eval &quot;(progn (gdb \&quot;gdb --annotate=3  $EMU\&quot;) $gdbcmd)&quot;
</pre>
<p>具体的操作和界面可以参照这篇文章：</p>
<p>http://www.nabble.com/printing-of-Eterm%27s-from-gdb-td19240493.html</p>
<p>在调试的时候 我们会希望查看 eterm的值，但是由于eterm的格式非常复杂， gdb的print什么的无法满足我们的需求。 otp开发团队于是开发出了一套方法来减轻我们的负担：</p>
<p>1.  erts/etc/unix/etp-commands 这是gdb的脚本 包含了几十个etp方法，而且文档非常详细。</p>
<p>2. 源码里面的 pp， ps等函数， 这些函数是专门为gdb调试开发的。 可以用gdb的 call pp(xxx)来调用。</p>
<p>有了这些工具 调试和研究erts变成了一件很快乐的事情！ </p>
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/48/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

