中关村村草 发表于 2011-03-04 10:33

Ruby1.9的隐藏参数"--dump"

转:skandhas

Ruby1.9的隐藏参数"--dump"




前几天,我的同事老高在看Ruby1.9.2源码的时候,发现Ruby1.9有一个隐藏参数:"--dump"(注意是两个减号). 下面具体说说这个参数:


1. --dump 参数

--dump 可以打印出ruby及指定脚本的相关信息,具体如下:



其中有几项的内容我们已经熟悉,例如:version,copyright,usage. 不过这次我们感兴趣的是parsetree和insns的内容。



parsetree -- 打印指定脚本的Parse Tree

insns      -- 打印指定脚本的RubyVM(YARV)指令


2. 具体用法举例

假设有一个已经写好的脚本1.rb,存放在C盘。我们可以如下使用"--dump"参数:

ruby --dump parsetree c:\1.rb


3.测试结果

测试脚本:

Ruby代码class A   
def hello   
    puts 'Hello'
end
end

A.new.hello打印RubyVM指令:ruby --dump insns c:\1.rb

输出

Rubyvm指令代码== disasm: <RubyVM::InstructionSequence:<main>@c:/1.rb>=================   
0000 trace            1                                             (   1)   
0002 putspecialobject 3
0004 putnil   
0005 defineclass      :A, <class:A>, 0
0009 pop   
0010 trace            1                                             (   7)   
0012 getinlinecache   19, <ic:0>   
0015 getconstant      :A   
0017 setinlinecache   <ic:0>   
0019 send             :new, 0, nil, 0, <ic:1>   
0025 send             :hello, 0, nil, 0, <ic:2>   
0031 leave   
== disasm: <RubyVM::InstructionSequence:<class:A>@c:/1.rb>==============   
0000 trace            2                                             (   1)   
0002 trace            1                                             (   2)   
0004 putspecialobject 1
0006 putspecialobject 2
0008 putobject      :hello   
0010 putiseq          hello   
0012 send             :"core#define_method", 3, nil, 0, <ic:0>   
0018 trace            4                                             (   5)   
0020 leave                                                            (   2)   
== disasm: <RubyVM::InstructionSequence:hello@c:/1.rb>==================   
0000 trace            8                                             (   2)   
0002 trace            1                                             (   3)   
0004 putnil   
0005 putstring      "Hello"
0007 send             :puts, 1, nil, 8, <ic:0>   
0013 trace            16                                              (   4)   
0015 leave                                                            (   3)打印ParseTree:ruby --dump parsetree c:\1.rb

输出:

Parse tree代码###########################################################   
## Do NOT use this node dump for any purpose other than##   
## debug and research.Compatibility is not guaranteed. ##   
###########################################################   

# @ NODE_SCOPE (line: 7)   
# +- nd_tbl: (empty)   
# +- nd_args:   
# |   (null node)   
# +- nd_body:   
#   @ NODE_BLOCK (line: 1)   
#   +- nd_head:   
#   |   @ NODE_CLASS (line: 1)   
#   |   +- nd_cpath:   
#   |   |   @ NODE_COLON2 (line: 1)   
#   |   |   +- nd_mid: :A   
#   |   |   +- nd_head:   
#   |   |       (null node)   
#   |   +- nd_super:   
#   |   |   (null node)   
#   |   +- nd_body:   
#   |       @ NODE_SCOPE (line: 5)   
#   |       +- nd_tbl: (empty)   
#   |       +- nd_args:   
#   |       |   (null node)   
#   |       +- nd_body:   
#   |         @ NODE_DEFN (line: 2)   
#   |         +- nd_mid: :hello   
#   |         +- nd_defn:   
#   |               @ NODE_SCOPE (line: 4)   
#   |               +- nd_tbl: (empty)   
#   |               +- nd_args:   
#   |               |   @ NODE_ARGS (line: 2)   
#   |               |   +- nd_frml: 0
#   |               |   +- nd_next:   
#   |               |   |   @ NODE_ARGS_AUX (line: 2)   
#   |               |   |   +- nd_rest: (null)   
#   |               |   |   +- nd_body: (null)   
#   |               |   |   +- nd_next:   
#   |               |   |       (null node)   
#   |               |   +- nd_opt:   
#   |               |       (null node)   
#   |               +- nd_body:   
#   |                   @ NODE_FCALL (line: 3)   
#   |                   +- nd_mid: :puts   
#   |                   +- nd_args:   
#   |                     @ NODE_ARRAY (line: 3)   
#   |                     +- nd_alen: 1
#   |                     +- nd_head:   
#   |                     |   @ NODE_STR (line: 3)   
#   |                     |   +- nd_lit: "Hello"
#   |                     +- nd_next:   
#   |                           (null node)   
#   +- nd_next:   
#         @ NODE_BLOCK (line: 7)   
#         +- nd_head:   
#         |   @ NODE_CALL (line: 7)   
#         |   +- nd_mid: :hello   
#         |   +- nd_recv:   
#         |   |   @ NODE_CALL (line: 7)   
#         |   |   +- nd_mid: :new   
#         |   |   +- nd_recv:   
#         |   |   |   @ NODE_CONST (line: 7)   
#         |   |   |   +- nd_vid: :A   
#         |   |   +- nd_args:   
#         |   |       (null node)   
#         |   +- nd_args:   
#         |       (null node)   
#         +- nd_next:   
#             (null node)
页: [1]
查看完整版本: Ruby1.9的隐藏参数"--dump"