Home > Erlang探索 > 未公开的erlang ports trace

未公开的erlang ports trace

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 未公开的erlang ports trace

erlang的trace机制非常强大, 在dbg, ttb模块的配合下, 可以非常清楚的了解系统的运作, 排错, 和调优. 但是有个对于做网络程序重要的功能被忽视了, 没有写到文档. 那就是trace ports消息.
我们的gen_tcp,file都是port实现的, 那么了解这么模块的运作其实就是要跟踪系统对ports的打开, 关闭, 读写操作.
好吧,上代码的时间了.
由于是未公开的功能, 所以dbg模块默认也是没启用这个功能的.我们patch下:
lib/runtime_tools/src/dbg.erl

1128all() ->
1129    [send,'receive',call,procs,garbage_collection,running,
1130     set_on_spawn,set_on_first_spawn,set_on_link,set_on_first_link,
1131     timestamp,arity,return_to, ports].  %%添加ports

重新编译, 安装.

root@ubuntu:~/otp# erl
Erlang R14A (erts-5.8)  [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.8  (abort with ^G)
1> dbg:tracer().
{ok,<0.33.0>}
2> dbg:p(all, [ports]).
{ok,[{matched,nonode@nohost,0}]}
3> ls().    
(<0.3.0>) open #Port<0.522> efile
(#Port<0.522>) closed normal
(<0.3.0>) open #Port<0.523> efile
(#Port<0.523>) closed normal
(<0.3.0>) open #Port<0.524> efile
(#Port<0.524>) closed normal
(<0.3.0>) open #Port<0.525> efile
(#Port<0.525>) closed normal
(<0.3.0>) open #Port<0.526> efile
(#Port<0.526>) closed normal
.git                       .gitignore                 
.mailmap                   AUTHORS                    

ok
4> os:cmd("ls").
(<0.40.0>) open #Port<0.527> '/bin/sh -s unix:cmd 2>&1'
(#Port<0.527>) closed {}
"aclocal.m4\nAUTHORS\n"
(<0.3.0>) open #Port<0.522> efile
(#Port<0.522>) closed normal
(<0.3.0>) open #Port<0.523> efile
(#Port<0.523>) closed normal
(<0.3.0>) open #Port<0.524> efile
(#Port<0.524>) closed normal
(<0.3.0>) open #Port<0.525> efile
(#Port<0.525>) closed normal
(<0.3.0>) open #Port<0.526> efile
(#Port<0.526>) closed normal
5> {ok, F} = file:open("AUTHORS", [read]).
(<0.39.0>) open #Port<0.527> efile
{ok,<0.39.0>}
6> file:read(F, 1024).
{ok,"AUTHORS\n\n  Contributions - improvements, fixes, new features - from developers\n  make the Erlang 'Open Source' project a success. To give credit\n  where it's due, we've added a file called AUTHORS to each\n  application sub-directory with a list of everyone who has contributed\n  It might also contain the names of the original authors at Ericsson.\n\n  Speaking of original authors, we don't want to forget all the people\n  working full time with Erlang and OTP. So, thanks to everyone\n  working with Erlang at the OTP group, the Computer Science\n  Laboratory and the Software Architecture Laboratory.\n\n"} 
7> file:close(F).
ok
8> (#Port<0.527>) closed normal

这么简单的我们透过这个功能可以了解到ports的运作(打开, 关闭)了,多谢otp开发组.

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Erlang探索 Tags: ,
  1. litaocheng
    April 12th, 2010 at 13:34 | #1

    呃,好方便呀..
    ports trace…咱

    [Reply]

  1. No trackbacks yet.