




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
打造Nodejs的调试环境打造Nodejs的调试环境(上)自从NODEJS问世以来,我们注意到的一个现象是,开源生态围绕Nodejs的扩展(Extends)、插件(Plugins)或工具(Tools/Utilis)在不断地增多,可见当中之蓬勃。这一方面固然很好,但这里却不是重点讨论那些扩展或者插件,而是调试工具(debugger)本身。因为,没有一个好的调试工具,无法展开一丁点的开发工作,或者困难重重。这一点,自必然在在坐当中的心知肚明。然而,官方对调试器的态度也是不含糊的。原来,官方一早在nodejs内置基于Eclipse forV8调试器的接口,为调试提供安排一套解决方案;另外还有一文编写针对“调试”的教程本文恰恰是得益于出自于出自ry之手的这份教程提炼而来。 话说Eclipse是一个大平台,开源的,任何有构思的东东都可以自由地在Eclipse平台上施展,好不快活,呵呵!急不及待地先看看这个调试器的界面,大概是这样的, 图一 图乃经典的Eclipse的风格的界面但Eclipse,与nodejs的调试,本不互为一体,从何说起呢?Nodejs即就是GoogleV8的高层扩展,调试过程中还是必须经过V8下面打交道。Google V8引擎只是一个地道的JavaScript解释引擎,不包含调试模块在内,也就是说需要借助另外的调试模组参与之。所以V8/nodejs暴露开放了相关的调试接口,通过这些接口,整合到专门的调试模块中去,像Eclipse Debug View,如此就可以呈现一个专业的调试环境。本文所介绍的调试器是Google Chrome DeveloperTools,定位在面向Chrome开发者的工具包,而此刻我们就是借助它这个工具来调试nodejs的。Google ChromeDeveloperTools在原理上是基于TCP远程的调试环境的这么一个Eclipse插件。作者ry有考虑到,直接在C+/Java的最终环境中加入调试器而不远程调试的,原因是什么?作者的说法是为了避免插件在C+/Java运行时带来的不稳定性。好,休费多唇舌,咱们从基础部分进入调试nodejs的介绍。首先是安装Google Chrome DeveloperTools。假设用户已经了解Eclipse的基本使用(熟悉怎么安装的用户则可以跳过此步),进入菜单选择【Help】【SoftwareUpdates.】,如下图。 出现对话框后切换至【Available Software】标签页,如下图。 然后点击右方的【Add Site】按钮,有否出现一个输入url网址的对话框?这是Google Chrome Developer Tools的下载地址了,我们贴上: /svn/update/dev/然后确认OK,稍等待片刻,如果出现/svn/update/dev/的树菜单,在其前面打勾选择下载。接着点击右边的【install】。 稍等片刻计算依赖关系 在【Review Licenses】的窗体中确认许可,然后点击【Next】,正式开始安装: 安装进度100%之后重启EclipseIDE使插件生效。然后在菜单【Windows】【ShowView】选择【Debug】切换到Eclipse的调试视图,即图一的界面(还没链接Nodejs),如下图。到这一步,调试插件被安装完成,接着是设置调试器的部分。 如上图红色区域,点击【Debug】的图标旁边的倒三角,出现下拉菜单点击【Debug Configurations 】。然后从左边的选择项之中点击【Standalone V8 VM(独立V8虚拟机)】接着右键鼠标,选择【New】新建一个调试profile。 如下图,输入名称【name】和端口【Port】。可以设置端口为Node-5858、Node-5859、Node-5860等等,但必须与nodejs脚本中所指定的端口一致。 最后点击【Debug】按钮旋即计入调试界面。至此,我们安装调试插件和设置端口部分已经完成了。打算了解如何在js代码中进一步的调试吗?例如打点、步进、监视等的调试基本操作内容在下一篇中将为大家作介绍!打造Nodejs的调试环境(下)上一期我们为大家介绍了安装Eclipse调试插件的情况,这对于还不熟悉Eclipse开发平台的用户是至关重要的,希望可以通过一步步的图片加文字说明,把Nodejs困难的地方变简单和清晰、友好和轻松。现在正式进入要调试程序肯定要有调试代码。下面就是我们第一个测试的代码,很小的行数:view plaincopy to clipboardprint?1. var sys = require(sys);2. / 此乃计数器变量,Suspend(中断挂起)期间可以观察该变量的变化。3. var count = 0;4. sys.debug(开始进行调试);5. function timer_tick() 6. count+;7. sys.debug(计数器: + count);8. if (count = 10) 9. count += 1000;10. sys.debug(能在这里打点吗?错过10后不行啦);11. 12. setTimeout(timer_tick, 1000);13. 14. timer_tick();var sys = require(sys);/ 此乃计数器变量,Suspend(中断挂起)期间可以观察该变量的变化。var count = 0;sys.debug(开始进行调试);function timer_tick() count+; sys.debug(计数器: + count); if (count = 10) count += 1000; sys.debug(能在这里打点吗?错过10后不行啦); setTimeout(timer_tick, 1000);timer_tick();该段代码是最简单清晰不过的了,就是通过一个count的累加器不断地让nodejs运行作累加。怎么运行这段代码?呵呵,如果尚未清楚如何执行一个最简单的nodejs的话还是在这里交待一下:在CMD命令行键入(须当前nodejs目录下)nodejsdbgtest.js。nodejs.exe的参数是javascript的源码文件。但是因为现在我们是调试的情景,必须加上“-debug”的参数打开调试的接口,即node debug 。另外,nodejs还支持-debug-brk的参数,下文再讲。输入命令nodejs - debug dbgtest.js后运行的截图如下:如图则是成功运行nodejs的情况,正常情况下不断累加计数器(count+)。本身是console输出的地方,通过观察console的提示,我们可以看到nodejs内部已经在内容打开5858的端口等待远程调试器,此刻我们接着要做的就是运行Eclipse(进入DebugView),两者一起配合调试的工作,这就是“远程”而非本地调试的含义。怎么看是否在Debug的视图(View)下呢?留意一下是否被选中即可,在Eclipse的右上角。进入下一步的讲述之前有必要加入一个小插曲,就是提及一下nodejs所支持的调试命令(Thanks toNodejs sys对象),列举如下: sys.debug() 同步打印,调试的时候很有用 sys.log()带时间的输出信息 sys.error()输出信息 sys.inspect(object, showHidden, depth) 显示一个对象的所有描述,如果showHidden为false时,只显示名称,没有省略。Depth指定隔多长时间去递归对象,默认是两次 sys.puts() 类似与document.writeln(),在屏幕上打印,在末尾添加换行 sys.print() 类似与document.write(),在屏幕上打印,没有换行用户测试一下各个方法,很容易了解其用途。例如sys.puts(sys.inspect(sys,false,null); ,结果如下:view plaincopy to clipboardprint?1. /输出结果2. 3. print:Function,4. puts:Function,5. debug:Function,6. error:Function,7. inspect:Function,8. p:Function,9. log:Function,10. exec:Function,11. inherits:Function12. /输出结果print:Function, puts:Function, debug:Function, error:Function, inspect:Function, p:Function, log:Function, exec:Function, inherits:Function 再回到Eclipse调试视图中。如下图表明链接nodejs成功,可以进行相关的调试工作。切换至Project Explorer标签页,这里列出了所有的require()包文件和nodejs.exe 所指定的主程序文件,都是JavaScript(*.js)文件。我们双击打开程序文件dbgtest.js,即可打开源码:在行数上方双击那一行,就是一个打点的工作(breakpoint),程序随即被挂起,暂停工作,调试器中显示当地变量等的信息供用户观察与进一步应用。如图我们在第8行打了一个点:首次使用Eclipse的调试界面感觉可能有些别扭,这没有关系。如果不太习惯,使用多几次就好。调试的基本内容大抵是那几回事。例如,你可以步进跟踪代码(stepinto,快捷键F5,但注意一点,对于步进require可能会crash挂掉,这也是情有可原的,require()是加载包的特殊方法)。下图则是调出watch的功能。当然,通过在js源码中加入debugger的关键字来打点也是支持的(debugger非常有用的关键字!)。view plaincopy to clipboardprint?1. function timer_tick() 2. count+;3. debugger;4. sys.debug(计数器: + count);5. .6. function timer_tick() count+; debugger; sys.debug(计数器: + count); . 恢复nodejs程序的运行,就要按绿色的箭头来恢复,快捷键是F8;(现在处于暂停所以中间的按钮是灰色;)红色按钮就是断开链接。另外如果至于如何关闭nodejs进程?据作者惯用的方式,就是在dos/win的命令行下ctrl+c强行退出即可。前面不是说到nodejs -debug-brk另外一个的参数吗?究竟有何作用?-debug-brk也是调试的命令,只是在一开始时就是挂起nodejs程序不运行,等待调试器通知才运行。明显与-debug边运行代码边监听调试的不同。为了避免过多的钻“牛角尖”,还是让用户们自己来试试这个参数吧:)前面说到调试界面不以独立的C/C+程序提供,其实一方面可以很轻松地嫁接这个调试模块的到C/C+项目中。因为属于高级话题的部分,限水平和能力的缘故就不展开讨论了(可留意一下ry原文)。结语:有了V8和Eclispe远程调试的可靠保障,nodejs的发展会越来越体现成熟,让我们投入到美丽的SSJS世界中去吧!Using Eclipse as Node Applications DebuggerEclipse debugger plugin for V8can be easily used to debug node scripts. (OnOSX10.5 the plugin requires an Eclipse 64 bit Version, started with Java SE 6). The V8 engine provides debugging support by enabling attachment of the remote debugger on aTCPport (5858 is nodes default). There are 2 debug related node options: node -debug=port NodeApp.jsor node -debug-brk=port NodeApp.jsThe-debugoption will just enable remote debugger connection on given port and then start the application normally. Even when debugger is connected to the running node instance later on, the script execution will not be stopped until “Suspend” command is issued by Eclipse debugger. Another way to stop the execution is to browse the source code of the JavaScript modules comprising the application and double click on the line number at the desired position in script to break at (most likely a callback). Once execution stops you can set/clear more breakpoints, but also inspect call stack and view content of all program variables.The-debug-brkoption is needed when your script is short lived (no time to attach debugger) and/or you want to observe the NodeApp.js execution from the very start. This option will force execution to break at the first line of the main script and wait for debugger to connect. The behavior upon connection is now different the script is suspended and no breakpoints are set. Note that V8 engine debugger is not behaving very good when it steps over or steps into require() method (it will crash), so try to set up first breakpoint past the initial module loading. This will also enable you to set breakpoints in any of those modules as well.InstallationRather than installing the V8 Eclipse Debugging Plugin on an existing Eclipse Java or Eclipse C+ installation, you might want to install the plugin into its own Eclipse Platform. This prevents an instability of the plugin, Node.js or V8 from disrupting your Java or C+ projects. You can download a clean slate Eclipse Platform, then use the plugin update utility to install V8 Eclipse Debugging Plugin. Download a clean slateEclipse Platformfor your operating system. Unzip the Eclipse Platform to a directory on your hard drive. Run the Eclipse Platform. You will be asked to create a workspace. Create a workspace separate from any Java or C+ workspaces you may already have created. If this is your first time using Eclipse, the default is fine. Proceed to the workbench by clicking the icon with the arrow titledWorkbenchin the top right corner of the Eclipse window. Select the menu itemHelp Install New Software. A dialog box titledInstallwill appear. This contains a plugin installation wizard. The first step of the wizard is titledAvailable Software. At the top of the dialog you will see a combo box labeledWork with:. Click the button to the right of the combo box labeledAdd. A dialog box titledAdd Repositorywill appear. In the textbox labeledName:enterGoogle Chrome Developer Tools. In the textbox labeledLocation:enter/svn/update/dev/. ClickOK. Now selectGoogle Chrome Developer Tools /svn/update/dev/from theWork with:combo box.Google Chrome Developer Toolswill appear in the table below. Click the checkbox next toGoogle Chrome Developer Toolsin the table. Click the button labeledNext at the bottom of the dialog box. The next wizard step titledInstall Detailsis displayed. Click the button labeledNext . The next wizard step titledReview Licensesis displayed. Once you are satisfied with the licenses, you can select theI accept the terms of the license agreementradio button and click the button labeledFinish. A progress dialog box titledInstalling Softwarewill be displayed. After installation is complete click theRestart Nowbutton. Eclipse will restart with the V8 Eclipse Debugging Plugin installed.Sample Debugging SessionHere is a simple script that will be used to demonstrate debugging procedure:/ dbgtest.jsvar sys=require(sys);var count = 0;sys.debug(Starting .);function timer_tick() count = count+1; sys.debug(Tick count: + count); if (count = 10) count += 1000; sys.debug(Set break here); setTimeout(timer_tick, 1000);timer_tick();Start the above script:$ node -debug dbgtest.jsdebugger listening on port 5858DEBUG: Starting .DEBUG: Tick count: 1DEBUG: Tick count: 2DEBUG: Tick count: 3 / and so onAssuming that you have already installed Eclipse, together with thepluginmentioned above, now you need to make initial debug profile.In the Menu, choose Window Open Perspective Other, then select Debug from the list and click OK, to ensure you are viewing the debug perspective.Click the drop box button (little black triangle) next to the green bug one and select “Debug Configurations ” option.Now start making new debug configuration by clicking the “New” button:Note that this configuration is named Node-5858 and the port is set accordingly. While you there create one or more additional configurations such as Node-5859, Node-5860, with respective ports, so you can later debug more than one node executable at the same time. Note also that most recent version of V8 debugger plugin has option to specify remote host as well.If you decided to edit the source lookup path, be sure to leave the Default entry. Break points wont work if you remove it.Start the debugging session by clicking “Debug” button. If everything goes well, you will get something like this:You can suspend the execution by pressing “Pause” button, however since it will most likely stop at node.js:process.loop() which, being a native C+ function, is not very interesting it is better to expand Node-5858 in Project Explorer, open dbgtest.js and set a break somewhere in timer callback, such as line 11, by double click on the line number.The breakpoint will be reached within a second:Now you can examine stack frames, browse program variables and do all the other useful stuff. If the same script was started with:$ node -debug-brk dbgtest.jsdebugger listening on port 5858Waiting for remote debugger connection.then debugger screen after session is connected would look like this:As it was mentioned before, avoid stepping over or into require() calls, so the first safe place to stop is at line 4.If you set breakpoint there and resume execution by clicking “Play” button, the script will stop at line 6, most likely due toJITinteraction.You probably noticed that Eclipse V8 debugger can not connect to the remote host.This simple transparentTCPproxy script will enable debugging of the remote node application running on the deployment or test server:/ Transparent TCP proxy for single connection only/ Zoran Tomicic ztomicic (at) / var sys = require(sys);var tcp = require(net);var encoding = binaryvar localActive = false; / flag to make sure that only one connection / to remote is activevar verbose = false; if (process.argv.length 4) sys.puts (Usage: node tcpproxy.js -vn+ -v for verbose operationn); process.exit(1);if (process.argv5 = -v) verbose = true;function log(s) if (verbose) sys.puts(tcpproxy: + s);var localPort = process.argv2;var remoteHost = process.argv3;var remotePort = process.argv4;var remote;var local = tcp.createServer(function (socket) socket.addListener(connect, function () if (localActive = false) localActive = true; socket.setEncoding(encoding); socket.setTimeout(0); log(Local connect); else log(Local reject); socket.end(); return; remote = tcp.createConnection(remotePort, remoteHost); remote.setEncoding(encoding); remote.setTimeout(0); log(Connecting to remote); remote.addListener(connect, function() log(Remote connect); ); remote.addListener(data, function(data) socket.write(data, encoding); log(Remote: (+data.length+ )n+data); ); remote.addListener(end, function () log(Remote close); socket.end(); remote.end(); ); ); socket.addListener(data, function (data) remote.write(data, encoding); log(Local: (+data.length+ )n+d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 财务担保合同范本
- 维修粉刷宿舍合同范本
- 阀门投标合同范本
- 社区居民消防安全知识培训课件活
- 顶账房交易合同范本
- 库存钢筋销售合同范本
- 私立医院进修合同范本
- 江津商店转让合同范本
- 社区安全知识培训课件游戏
- 矿山建设安装合同范本
- 非煤矿职工职业卫生培训
- 社区居民高血压防治健康讲座
- 2025年湖北省中考化学试题深度解读及答案详解
- Unit 3 Same or DifferentSection A Grammar Focus (3a-3c) 课件-2025-2026学年人教版八年级英语上册
- 管线及设备开启作业安全管理制度与操作流程
- 2025年保密知识试题库附参考答案(完整版)
- 2025年医学基础知识考试试题库及答案
- 1.5 植物的变化 教案 教科版小学一年级科学上册
- 揭西招投标管理办法
- 社区与小课堂的合同协议
- 低空经济相关政策文件
评论
0/150
提交评论