免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2503 | 回复: 0
打印 上一主题 下一主题

Ruby1.9的隐藏参数"--dump" [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-04 10:33 |只看该作者 |倒序浏览
转:skandhas

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





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


1. --dump 参数

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

[version, copyright, usage, yydebug, syntax, parsetree , parsetree_with_comment, insns ]

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



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

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


2. 具体用法举例

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

ruby --dump parsetree c:\1.rb


3.测试结果

测试脚本:

Ruby代码
  1. class A   
  2.   def hello   
  3.     puts 'Hello'  
  4.   end  
  5. end  
  6.   
  7. A.new.hello  
复制代码
打印RubyVM指令:ruby --dump insns c:\1.rb

输出

Rubyvm指令代码
  1. == disasm: <RubyVM::InstructionSequence:<main>@c:/1.rb>=================   
  2. 0000 trace            1                                               (   1)   
  3. 0002 putspecialobject 3  
  4. 0004 putnil   
  5. 0005 defineclass      :A, <class:A>, 0  
  6. 0009 pop   
  7. 0010 trace            1                                               (   7)   
  8. 0012 getinlinecache   19, <ic:0>   
  9. 0015 getconstant      :A   
  10. 0017 setinlinecache   <ic:0>   
  11. 0019 send             :new, 0, nil, 0, <ic:1>   
  12. 0025 send             :hello, 0, nil, 0, <ic:2>   
  13. 0031 leave   
  14. == disasm: <RubyVM::InstructionSequence:<class:A>@c:/1.rb>==============   
  15. 0000 trace            2                                               (   1)   
  16. 0002 trace            1                                               (   2)   
  17. 0004 putspecialobject 1  
  18. 0006 putspecialobject 2  
  19. 0008 putobject        :hello   
  20. 0010 putiseq          hello   
  21. 0012 send             :"core#define_method", 3, nil, 0, <ic:0>   
  22. 0018 trace            4                                               (   5)   
  23. 0020 leave                                                            (   2)   
  24. == disasm: <RubyVM::InstructionSequence:hello@c:/1.rb>==================   
  25. 0000 trace            8                                               (   2)   
  26. 0002 trace            1                                               (   3)   
  27. 0004 putnil   
  28. 0005 putstring        "Hello"  
  29. 0007 send             :puts, 1, nil, 8, <ic:0>   
  30. 0013 trace            16                                              (   4)   
  31. 0015 leave                                                            (   3)  
复制代码
打印ParseTree:ruby --dump parsetree c:\1.rb

输出:

Parse tree代码
  1. ###########################################################   
  2. ## Do NOT use this node dump for any purpose other than  ##   
  3. ## debug and research.  Compatibility is not guaranteed. ##   
  4. ###########################################################   
  5.   
  6. # @ NODE_SCOPE (line: 7)   
  7. # +- nd_tbl: (empty)   
  8. # +- nd_args:   
  9. # |   (null node)   
  10. # +- nd_body:   
  11. #     @ NODE_BLOCK (line: 1)   
  12. #     +- nd_head:   
  13. #     |   @ NODE_CLASS (line: 1)   
  14. #     |   +- nd_cpath:   
  15. #     |   |   @ NODE_COLON2 (line: 1)   
  16. #     |   |   +- nd_mid: :A   
  17. #     |   |   +- nd_head:   
  18. #     |   |       (null node)   
  19. #     |   +- nd_super:   
  20. #     |   |   (null node)   
  21. #     |   +- nd_body:   
  22. #     |       @ NODE_SCOPE (line: 5)   
  23. #     |       +- nd_tbl: (empty)   
  24. #     |       +- nd_args:   
  25. #     |       |   (null node)   
  26. #     |       +- nd_body:   
  27. #     |           @ NODE_DEFN (line: 2)   
  28. #     |           +- nd_mid: :hello   
  29. #     |           +- nd_defn:   
  30. #     |               @ NODE_SCOPE (line: 4)   
  31. #     |               +- nd_tbl: (empty)   
  32. #     |               +- nd_args:   
  33. #     |               |   @ NODE_ARGS (line: 2)   
  34. #     |               |   +- nd_frml: 0  
  35. #     |               |   +- nd_next:   
  36. #     |               |   |   @ NODE_ARGS_AUX (line: 2)   
  37. #     |               |   |   +- nd_rest: (null)   
  38. #     |               |   |   +- nd_body: (null)   
  39. #     |               |   |   +- nd_next:   
  40. #     |               |   |       (null node)   
  41. #     |               |   +- nd_opt:   
  42. #     |               |       (null node)   
  43. #     |               +- nd_body:   
  44. #     |                   @ NODE_FCALL (line: 3)   
  45. #     |                   +- nd_mid: :puts   
  46. #     |                   +- nd_args:   
  47. #     |                       @ NODE_ARRAY (line: 3)   
  48. #     |                       +- nd_alen: 1  
  49. #     |                       +- nd_head:   
  50. #     |                       |   @ NODE_STR (line: 3)   
  51. #     |                       |   +- nd_lit: "Hello"  
  52. #     |                       +- nd_next:   
  53. #     |                           (null node)   
  54. #     +- nd_next:   
  55. #         @ NODE_BLOCK (line: 7)   
  56. #         +- nd_head:   
  57. #         |   @ NODE_CALL (line: 7)   
  58. #         |   +- nd_mid: :hello   
  59. #         |   +- nd_recv:   
  60. #         |   |   @ NODE_CALL (line: 7)   
  61. #         |   |   +- nd_mid: :new   
  62. #         |   |   +- nd_recv:   
  63. #         |   |   |   @ NODE_CONST (line: 7)   
  64. #         |   |   |   +- nd_vid: :A   
  65. #         |   |   +- nd_args:   
  66. #         |   |       (null node)   
  67. #         |   +- nd_args:   
  68. #         |       (null node)   
  69. #         +- nd_next:   
  70. #             (null node)  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP