简体   繁体   English

使用WebInspector的Javascript调试器,如何摆脱无限循环?

[英]Using WebInspector's Javascript Debugger, how do I break out of an infinite loop?

I wrote an infinite loop in my javascript code. 我在JavaScript代码中写了一个无限循环。 Using WebKit's Web Inspector, how do I terminate execution? 使用WebKit的Web检查器,如何终止执行? I have to quit Safari and reopen it again (after changing my code of course). 我必须退出Safari并重新打开它(当然要更改代码后)。

EDIT: To be more specific, I'm looking for a way to enter the looping executing process/thread to see why the loop isn't terminating. 编辑:更具体地说,我正在寻找一种进入循环执行进程/线程的方法,以了解循环为什么没有终止。 An analogy would be in GDB where I could do a ^C and break into the process. 在GDB中可以做一个比喻,我可以做一个^ C并进入流程。 I'm not looking for a way to kill my web browser. 我不是在寻找杀死我的网络浏览器的方法。 I'm pretty good at that already. 我已经很好了。

I'm not familiar with WebKit, but this sounds like a common problem that I usually debug as follows: Declare an integer outside of the scope of the loop, and increment it for each iteration, then throw an exception when the iteration count exceeds the maximum expected possible amount of iterations. 我对WebKit不熟悉,但这听起来像是一个常见问题,我通常按以下步骤进行调试:在循环范围之外声明一个整数,并为每次迭代递增该整数,然后在迭代计数超过整数时抛出异常。最大预期可能的迭代次数。 So, in pseudo-code, something like the following could be used to debug this problem: 因此,在伪代码中,可以使用类似以下的内容来调试此问题:

var iterations = 0;
var greatestPossibleNumberOfValidIterations = 500;
while(shouldLoopBooleanTest){
  iterations++;
  if(iterations>greatestPossibleNumberOfValidIterations){
     //do debugging/error handling
  }
} 

I don't expect that this is specific enough to warrant an accepted answer, but I hope it helps you solve your problem. 我不希望这足够具体,不能保证可以接受答案,但是我希望它可以帮助您解决问题。

Have you tried top to look at the process tree? 您是否尝试过top一下进程树? Find the PID of the program and then type kill -9 [PID] . 找到程序的PID,然后键入kill -9 [PID]

Have you tried using F11? 您是否尝试过使用F11? This is the key for step into: https://trac.webkit.org/wiki/WebInspector 这是进入以下步骤的关键: https : //trac.webkit.org/wiki/WebInspector

In using Firefox (I know the question doesn't specify Firefox, but I'm just throwing this in, in case it helps somebody), Safari, and Chrome, I found that there isn't a consistent way to break into an infinite loop, but there are ways to setup execution to break into a loop if you need to. 在使用Firefox(我知道问题并没有指定Firefox,但是我只是将它扔进去,以防他人使用),Safari和Chrome,我发现没有一种统一的方法可以分解成无限循环,但是有一些方法可以将执行设置为在需要时进入循环。 See my test code below. 请参阅下面的测试代码。

Firefox 火狐

Utter trash. 完全垃圾。 It just stood there spinning it's rainbow. 它只是站在那儿,旋转着彩虹。 I had to kill it. 我不得不杀死它。

Safari 苹果浏览器

The nice thing about Safari is that it will eventually throw up a dialog asking if you want to stop. 关于Safari的好处是,它最终将引发一个对话框,询问您是否要停止。 You should say yes. 你应该说是的。 Then hit command-option i to bring up the Web Inspector. 然后按command-option i出Web检查器。 The source should pop-up saying that the Javascript exceeded the timeout. 消息源应弹出,说明Javascript已超过超时时间。 Hit the pause button in the right hand side towards the top, then refresh. 点击右侧的顶部的暂停按钮,然后刷新。 The code will reload, now step through: I like using command-; 代码将重新加载,现在执行以下步骤:我喜欢使用command-; because it steps through every call. 因为它会逐步处理每个呼叫。 If you have complex code you might never get to the end. 如果您有复杂的代码,您可能永远也无法解决。 Don't hit continue though ( command-/ ) or you'll be back at square one. 不过请不要continuecommand-/ ),否则您将回到第一格。

Chrome

The most useful of the three. 这三个中最有用的。 If you naively load the code, it will keep going forever. 如果您天真地加载代码,它将永远持续下去。 But, before loading the page, open the Web Inspector and select 'Load resources all the time'. 但是,在加载页面之前,请打开Web检查器,然后选择“始终加载资源”。 Then reload the page. 然后重新加载页面。 While the page is trying to load, you can click over to scripts and pause the Javascript while it is running. 在尝试加载页面时,您可以单击脚本并在Javascript运行时暂停它。 This is what I was looking for. 这就是我想要的。

Code

<html>
  <head></head>
  <body onload="TEST.forever.loop()">
    Nothing
  </body>
  <script type="text/javascript">
    TEST = {};
    TEST.forever = {
      loop : function() {
        var i = 0;
        while (i >= 0) {
          i++;
        }
      }
    };
  </script>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM