意外的VS Code调试器数值

huangapple go评论79阅读模式
英文:

Unexpected VS Code debugger values

问题

函数

我有这个函数来检测四面体是否几乎没有体积,即它是平面的:


import (
	"math";

	v3 "github.com/deadsy/sdfx/vec/v3"
)

// MATHEMATICA 脚本在这里可用:
// https://math.stackexchange.com/a/4709610/197913
func isZeroVolume(a, b, c, d v3.Vec) (bool, float64) {
	ab := b.Sub(a)
	ac := c.Sub(a)
	ad := d.Sub(a)

	// 注意,MATHEMATICA 的 `Norm` 函数等同于我们的 `Length()` 函数。
	nab := ab.Length()
	ncd := ac.Sub(ad).Length()
	nbd := ab.Sub(ad).Length()
	nbc := ab.Sub(ac).Length()
	nac := ac.Length()
	nad := ad.Length()

	// 检查边长是否为0
	if nab == 0 || ncd == 0 ||
		nbd == 0 || nbc == 0 ||
		nac == 0 || nad == 0 {
		return true, 0
	}

	volume := 1.0 / 6.0 * math.Abs(ab.Cross(ac).Dot(ad))
	denom := (nab + ncd) * (nac + nbd) * (nad + nbc)

	// 容差从这里推导出来:
	// https://math.stackexchange.com/a/4709610/197913
	tolerance := 480.0

	rho := tolerance * volume / denom

	return rho < 1, volume
}

输入

我使用以下四个输入点来逐步执行代码:

{X: -1.572793602943422, Y: -4.157202807477221, Z: 5.603983008116483}
{X: -2.45160644054413, Y: -3.4214927673339854, Z: 6.135950530673543}
{X: -2.45160644054413, Y: -3.7163730403986044, Z: 5.603983008116483}
{X: -1.572793602943422, Y: -3.5355907043553003, Z: 6.482795845717191}

调试器

通过 VS Code 调试器逐步执行代码时,局部变量具有以下值:

意外的VS Code调试器数值

问题

调试器显示的值毫无意义。denomtolerance 怎么会是 0?这对我来说毫无意义。我是否漏掉了什么?

英文:

Function

I have this function to detect if a tetrahedron has almost zero volume, i.e. it's flat:


import (
	&quot;math&quot;

	v3 &quot;github.com/deadsy/sdfx/vec/v3&quot;
)

// MATHEMATICA script is available here:
// https://math.stackexchange.com/a/4709610/197913
func isZeroVolume(a, b, c, d v3.Vec) (bool, float64) {
	ab := b.Sub(a)
	ac := c.Sub(a)
	ad := d.Sub(a)

	// Note that the `Norm` function of MATHEMATICA is equivalent to our `Length()` function.
	nab := ab.Length()
	ncd := ac.Sub(ad).Length()
	nbd := ab.Sub(ad).Length()
	nbc := ab.Sub(ac).Length()
	nac := ac.Length()
	nad := ad.Length()

	// Check for 0 edge lengths
	if nab == 0 || ncd == 0 ||
		nbd == 0 || nbc == 0 ||
		nac == 0 || nad == 0 {
		return true, 0
	}

	volume := 1.0 / 6.0 * math.Abs(ab.Cross(ac).Dot(ad))
	denom := (nab + ncd) * (nac + nbd) * (nad + nbc)

	// Tolerance derived from here:
	// https://math.stackexchange.com/a/4709610/197913
	tolerance := 480.0

	rho := tolerance * volume / denom

	return rho &lt; 1, volume
}

Input

I step through the code with these four input points:

{X: -1.572793602943422, Y: -4.157202807477221, Z: 5.603983008116483}
{X: -2.45160644054413, Y: -3.4214927673339854, Z: 6.135950530673543}
{X: -2.45160644054413, Y: -3.7163730403986044, Z: 5.603983008116483}
{X: -1.572793602943422, Y: -3.5355907043553003, Z: 6.482795845717191}

Debugger

Stepping through the code by VS Code debugger indicates that the local variable have these values:

意外的VS Code调试器数值

Question

The values shown by the debugger make no sense. How can denom and tolerance be 0? It makes no sense to me. Am I missing something?

答案1

得分: 1

这是一个简化版本的演示,用于展示问题:

 1	package main
 2	
 3	func main() {
 4		a := f(1)
 5		b := 1
 6	
 7		c := a < b
 8		_ = c
 9	}
10	
11	func f(i int) int {
12		if i > 0 {
13			return i
14		}
15		return -i
16	}

以下是 delve 调试会话的输出:

(dlv) b 7
Breakpoint 2 set at 0x4608de for main.main() ./main.go:7
(dlv) c
> main.main() ./main.go:7 (hits goroutine(1):1 total:1) (PC: 0x4608de)
2:	
3:	func main() {
4:		a := f(1)
5:		b := 1
6:	
=>   7:		c := a < b
8:		_ = c
9:	}
10:	
11:	func f(i int) int {
12:		if i > 0 {
(dlv) locals
a = 824633745824
b = 824633843808
(dlv) disass
TEXT main.main(SB) /home/zeke/src/temp/76380802/main.go
main.go:3	0x4608c0	493b6610			cmp rsp, qword ptr [r14+0x10]
main.go:3	0x4608c4	7639				jbe 0x4608ff
main.go:3	0x4608c6	4883ec28			sub rsp, 0x28
main.go:3	0x4608ca	48896c2420			mov qword ptr [rsp+0x20], rbp
main.go:3	0x4608cf	488d6c2420			lea rbp, ptr [rsp+0x20]
main.go:4	0x4608d4	b801000000			mov eax, 0x1
main.go:4	0x4608d9	e842000000			call $main.f
=>	main.go:7	0x4608de*	4883f801			cmp rax, 0x1
main.go:4	0x4608e2	4889442418			mov qword ptr [rsp+0x18], rax
main.go:5	0x4608e7	48c744241001000000	mov qword ptr [rsp+0x10], 0x1
main.go:7	0x4608f0	0f9c44240f			setl byte ptr [rsp+0xf]
main.go:9	0x4608f5	488b6c2420			mov rbp, qword ptr [rsp+0x20]
main.go:9	0x4608fa	4883c428			add rsp, 0x28
main.go:9	0x4608fe	c3					ret
main.go:3	0x4608ff	90					nop
main.go:3	0x460900	e83bccffff			call $runtime.morestack_noctxt
main.go:3	0x460905	ebb9				jmp $main.main

你可以看到指令 0x4608de 被选为第 7 行的断点。在这一点上,变量 ab 还没有获得它们的值(a 在指令 0x4608e2 后获得值,而 b0x4608e7 处获得值)。

这就是为什么一开始你没有得到正确的值,而后来它会自动“修复”自己的原因。

这个问题已经报告在 cmd/compile: bad DWARF location for variable #58813

英文:

Here is a simplified version of the demo to show the issue:

 1	package main
 2	
 3	func main() {
 4		a := f(1)
 5		b := 1
 6	
 7		c := a &lt; b
 8		_ = c
 9	}
10	
11	func f(i int) int {
12		if i &gt; 0 {
13			return i
14		}
15		return -i
16	}

And here is the output of a delve debug session:

(dlv) b 7
Breakpoint 2 set at 0x4608de for main.main() ./main.go:7
(dlv) c
&gt; main.main() ./main.go:7 (hits goroutine(1):1 total:1) (PC: 0x4608de)
2:	
3:	func main() {
4:		a := f(1)
5:		b := 1
6:	
=&gt;   7:		c := a &lt; b
8:		_ = c
9:	}
10:	
11:	func f(i int) int {
12:		if i &gt; 0 {
(dlv) locals
a = 824633745824
b = 824633843808
(dlv) disass
TEXT main.main(SB) /home/zeke/src/temp/76380802/main.go
main.go:3	0x4608c0	493b6610			cmp rsp, qword ptr [r14+0x10]
main.go:3	0x4608c4	7639				jbe 0x4608ff
main.go:3	0x4608c6	4883ec28			sub rsp, 0x28
main.go:3	0x4608ca	48896c2420			mov qword ptr [rsp+0x20], rbp
main.go:3	0x4608cf	488d6c2420			lea rbp, ptr [rsp+0x20]
main.go:4	0x4608d4	b801000000			mov eax, 0x1
main.go:4	0x4608d9	e842000000			call $main.f
=&gt;	main.go:7	0x4608de*	4883f801			cmp rax, 0x1
main.go:4	0x4608e2	4889442418			mov qword ptr [rsp+0x18], rax
main.go:5	0x4608e7	48c744241001000000	mov qword ptr [rsp+0x10], 0x1
main.go:7	0x4608f0	0f9c44240f			setl byte ptr [rsp+0xf]
main.go:9	0x4608f5	488b6c2420			mov rbp, qword ptr [rsp+0x20]
main.go:9	0x4608fa	4883c428			add rsp, 0x28
main.go:9	0x4608fe	c3					ret
main.go:3	0x4608ff	90					nop
main.go:3	0x460900	e83bccffff			call $runtime.morestack_noctxt
main.go:3	0x460905	ebb9				jmp $main.main

You see that the instruction 0x4608de is picked as the breakpoint at line :7. At this point, the variables a and b haven't got their values yet (a gets the value later at the instruction 0x4608e2, while b at 0x4608e7).

That's why you don't get the correct value at first and it "fixes itself" later.

This issue has been reported as cmd/compile: bad DWARF location for variable #58813.

答案2

得分: 0

好的,以下是翻译好的内容:

嗯,我刚刚通过调试器继续逐步执行代码,现在的值是符合预期的。比如tolerance的常量值:

意外的VS Code调试器数值

我不知道为什么只是继续逐步执行就能纠正它的问题...

英文:

Well, I just continued stepping through the code by the debugger and the values are now as expected. Like the constant value of tolerance:

意外的VS Code调试器数值

I don't know how and why it gets corrected by just continuing the stepping...

huangapple
  • 本文由 发表于 2023年6月1日 18:06:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定