跳转至

Debugging and Errors

This page is inspired by CS161 Proj2 @ UC Berkeley

Full Tutorial of VSCode Debugging is listed here

Debug Toolbar

alt text

You can use mouse to grab this bar to anywhere you want

Commands

Continue / Pause

  • Continue: Resume normal program/script execution (up to the next breakpoint / end).
  • Pause: Inspect code executing at the current line and debug line-by-line.

Step Over

  • Execute the next method as a single command without inspecting or following its component steps.

Step Into

  • Enter the next method to follow its execution line-by-line.

Step Out

  • When inside a method or subroutine, return to the earlier execution context by completing remaining lines of the current method as though it were a single command.

Restart

  • Terminate the current program execution and start debugging again using the current run configuration.

Stop

  • Terminate the current program execution.

Example

Go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func (userdata *User) LoadFile(filename string) (content []byte, err error) {
    storageKey, err := uuid.FromBytes(userlib.Hash([]byte(filename + userdata.Username))[:16])
    if err != nil {
        return nil, err
    }
    dataJSON, ok := userlib.DatastoreGet(storageKey) // <- breakpoint here (1)
    if !ok {
        return nil, errors.New(strings.ToTitle("file not found"))
    }
    err = json.Unmarshal(dataJSON, &content) // <- breakpoint here (2)
    return content, err
}
Go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Helper Function
func datastoreGet(key UUID) (value []byte, ok bool) {
    datastoreShard := getDatastoreShard()
    value, ok = datastoreShard[key] // <- Arrive Point
    if ok && value != nil {
        // Update bandwidth tracker
        bandwidth := getDatastoreBandwidthShard()
        *bandwidth += len(value)

        foo := make([]byte, len(value))
        copy(foo, value)
        return foo, ok
    }
    return
}
  1. If I stand in breakpoint-1, and use continue, I will arrive at breakpoint-2.

  2. At breakpoint-1, if I use step over, I will arrive at if !ok {.

  3. At breakpoint-1, if I use step into, I will enter userlib.DatastoreGet(storageKey) and inspect its execution line-by-line.

  4. And in userlib.DatastoreGet(storageKey), if I get to Arrive Point, and use step out, I will return to breakpoint-1 dataJSON, ok := userlib.DatastoreGet(storageKey) like this function finished within a single command.

Breakpoints and Logpoints

BreakPoint

alt text

alt text

LogPoint

alt text

A Logpoint is a variant of a breakpoint that does not "break" into the debugger but instead logs a message to the debug console.

Logpoints enable you to inject logging while debugging without modifying the source code. They are especially useful when you're debugging production servers that cannot be paused or stopped.

Logpoints can also help you save time by not having to add or remove logging statements in your code.

Data Inspection

Look at the left side bar, and you can see the VARIABLES / WATCH section.

VARIABLES

alt text

WATCH

alt text

Summary

alt text