Home  >  Article  >  Backend Development  >  Unexpected VS Code debugger value

Unexpected VS Code debugger value

WBOY
WBOYforward
2024-02-08 23:50:10446browse

意外的 VS Code 调试器值

php editor Strawberry brings you an unexpected tool - the VS Code debugger. As a powerful code editor, VS Code's debugger function provides convenient debugging tools to help developers locate and solve problems in the code more quickly. Whether it is single-step debugging, breakpoint debugging or viewing variable values, the VS Code debugger can give accurate feedback and prompts, greatly improving development efficiency. Let's explore this amazing debugger together!

Question content

Function

I have this function to detect if the volume of a tetrahedron is almost zero, i.e. if it is flat:

import (
    "math"

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

// 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 < 1, volume
}

enter

I use these four input points to step through the code:

{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 through the vs code debugger shows that the local variable has the following values:

question

The value displayed by the debugger does not make sense. How do denom and tolerance become 0? Total nonsense to me. Did I miss something?

Workaround

This is a simplified version of the demo showing the problem:

 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  }

This is the output from the delve debugging session:

(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

You will see that the instruction 0x4608de is selected as the breakpoint for line :7. At this point, the variables a and b have not yet gotten their values ​​(a will get their values ​​later at instruction 0x4608e2, while bGet the value at 0x4608e7).

That's why you don't get the correct value initially, it "fixes itself" later on.

This issue has been reported as cmd/compile: Wrong dwarf location for variable #58813一个>.

The above is the detailed content of Unexpected VS Code debugger value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete