Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

javascript - Devtools在事后分析函数调用和变量(Devtools analyse function calls and variables post-factum)

I'm debugging a userscript, which is hard to debug, because the bug happens rarely and unpredictably (yet), and the function stack gets executed repeatedly (and the bug happens approximately once every 10-20 times).

(我正在调试一个很难调试的用户脚本,因为该错误很少且无法预测地发生(尚未),并且函数堆栈被重复执行(该错误大约每10-20次发生一次)。)

I cannot pause the devtools, otherwise the page will reload and the bug won't appear (so I cannot use breakpoints).

(我无法暂停devtools,否则页面将重新加载并且错误不会出现(因此我无法使用断点)。)

Edit: even if I stop the page from reloading I still have to make user-input repeatedly and fast to cause the bug, so not possible to use breakpoints, because that would stop the bug from appearing.

(编辑:即使我停止重新加载页面,我仍然必须反复快速地进行用户输入以引起错误,因此无法使用断点,因为那样会阻止错误的出现。)

One of the ways which I can think of is putting console.log in every function and logging variables and then searching through them to find where it went wrong.

(我能想到的方法之一是将console.log放入每个函数中并记录变量,然后在它们中进行搜索以查找错误之处。)

But it seems very tedious.

(但这似乎很乏味。)

In the performance section of devtools it's possible to record the process, and see the function calls, but it's impossible to see the variables.

(在devtools的“性能”部分中,可以记录过程并查看函数调用,但是看不到变量。)

Is there any way to go through the function call stack post-factum (maybe a tool other than devtools), seeing what the variables were equal to in every cycle?

(是否有任何方法可以遍历函数调用后的堆栈(可能是devtools以外的工具),看看每个周期中的变量等于什么?)

(And not only the current call stack, because it gets updated on every user input in my script).

((并且不仅是当前的调用堆栈,因为在我的脚本中,每个用户输入都会更新它)。)

Or is console.logging my only option in this case?

(还是在这种情况下console.logging是我唯一的选择?)

I thought that "step out of the current function" option would work, but it seems to be unpredictable (it seemed to happen at least when there is setTimeout).

(我以为“跳出当前功能”选项会起作用,但这似乎是不可预测的(至少在有setTimeout的情况下似乎会发生)。)

And the function stack gets updated on every user input (and to capture this bug the user input sequence should be fast), so there is no way to go through the call stack either.

(而且功能堆栈会在每个用户输入上进行更新(并且要捕获此错误,用户输入序列应该很快),因此也无法通过调用堆栈。)

It would be perfect to have something like a call stack tree, which would show when a certain function was last called (and how many times during a certain period of time or since the page load), what was the position of this function in the tree of all functions, and the ability to go through this tree and search for all instances of calls to a specific function, and then the ability to see what the variables were equal to in every call.

(拥有调用堆栈树之类的东西将是完美的,它可以显示某个函数的最后调用时间(以及在特定时间段内或自页面加载以来多少次),该函数在目录中的位置是什么。所有功能的树,并具有遍历该树并搜索对特定函数的所有调用实例的能力,然后具有查看每个调用中的变量相等的能力。)

There is a lot of code, but since it was requested, here is a link to the code inside a GitHub repository (it's a Chrome Extension, and the code where the bug happens is inside the objGA object [at the 6490's line of code]).

(有很多代码,但是自从被请求以来,这里有一个指向GitHub存储库中的代码的链接(它是Chrome扩展程序,而发生错误的代码在objGA对象内部[在6490的代码行中] )。)

And the bug happens somewhere between the 6600's line and the 6890's (the bug is that sometimes it doesn't move chess pieces on premoves).

(该错误发生在6600线与6890线之间的某个地方(该错误是有时它不会在棋子上移动棋子)。)

It's a Chrome Extension to move chess pieces with a keyboard and a mouse.

(这是一个Chrome扩展程序,可通过键盘和鼠标移动棋子。)

https://github.com/Sentero-esp12/Lichess-Keyboard-WebRequest-/blob/master/Extension/code.js

(https://github.com/Sentero-esp12/Lichess-Keyboard-WebRequest-/blob/master/Extension/code.js)

Edit: Luckily I was able to reproduce it in a slow pace with breakpoints (was able to find the exact moment when the bug appeared).

(编辑:幸运的是,我能够用断点缓慢地重现它(能够找到错误出现的确切时间)。)

So it turned out I was using Math.round instead of Math.floor trying to extract the first digit from a double digit number.

(因此,事实证明我是在使用Math.round而不是Math.floor尝试从两位数字中提取第一位数字。)

Since only some numbers were >= *5, the bug appeared only occasionally and I couldn't notice the cause.

(由于只有一些数字> = * 5,因此该错误仅偶尔出现,我无法注意到原因。)

  ask by Sentero-esp12 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...