vscode-tinygo icon indicating copy to clipboard operation
vscode-tinygo copied to clipboard

Add debug support

Open sago35 opened this issue 3 years ago • 69 comments

This PR adds debug support to vscode-tinygo. It works as follows.

  1. Add some definitions to .vscode/settings.json
  2. Add settings for debug launch to .vscode/launch.json
    • With this configuration, pressing F5 will launch the debugger with TinyGo configured.
  3. Add build tasks to .vscode/tasks.json to be used from .vscode/launch.json

The following are the parts that are not done so far.

  1. [x] ~~If tasks.json does not exist, adding a task will fail.~~ fixed
    • ~~If you create the file to be written later first, there will be no error.~~
  2. This is my first time using typescript, so I'm not sure if I'm writing the right code.

sago35 avatar Feb 14 '22 07:02 sago35

Use it this way for now. If there is a better way, I'd like your opinion.

https://www.youtube.com/watch?v=omT0agnFHA4

sago35 avatar Feb 14 '22 08:02 sago35

I forgot to write. In order to run it, you will need tinygo from the following branch

https://github.com/tinygo-org/tinygo/tree/buildinfo

sago35 avatar Feb 14 '22 09:02 sago35

It is now possible to easily launch a debugger using VSCode. If you run tinygo target on the screen where the main package to be debugged is open, the various settings will be updated. After that, press F5 to execute the build and start debugging.

image image image image image image

Right now, only openocd is supported. However, the openocdInterface can be changed

image

sago35 avatar Feb 16 '22 10:02 sago35

@sago35 HI, could you please write a quick HOWTO of getting VSCode debugging going for a RP Pico target? I have another RP Pico programmed as a USB->SWD that I can use for the hardware interface, I also have an openocd that supports that target. WHat scripts will I need for VSCode to connect gdb/openocd etc ..

Cherrs

bmentink avatar Mar 14 '22 02:03 bmentink

@bmentink First you must be able to run tinygo flash with picoprobe.

openocd must support picoprobe. You can use the official raspberrypi one or the following.

https://github.com/ciniml/debug-tools-builder/releases/

$ tinygo flash --target pico --size short --programmer picoprobe examples/blinky1
   code    data     bss |   flash     ram
   5704       4    2264 |    5708    2268
Open On-Chip Debugger 0.11.0-rc2+dev-gcafa0b5 (2021-02-20-22:09)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'swd'
Warn : Transport "swd" was already selected
adapter speed: 5000 kHz

Warn : Transport "swd" was already selected
swd
Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x10000001
Info : rp2040.core0: hardware has 4 breakpoints, 2 watchpoints
Info : rp2040.core1: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x000000ee msp: 0x20041f00
target halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x000000ee msp: 0x20041f00
** Programming Started **
Info : RP2040 B0 Flash Probe: 2097152 bytes @10000000, in 512 sectors

target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
Info : Writing 8192 bytes starting at 0x0
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
** Programming Finished **
** Resetting Target **
shutdown command invoked

If you can tinygo flash, next you need to check tinygo gdb. Make sure tinygo gdb is running correctly by using continue, etc.

$ tinygo gdb --target pico --size short --programmer picoprobe examples/blinky1

Next you need to build tinygo at buildinfo branch. It is OK if JSON is output by executing the following command.

https://github.com/tinygo-org/tinygo/tree/buildinfo

$ tinygo build -json --target pico --size short --programmer picoprobe examples/blinky1
{
  "Options": {
    ...
  },
  "Target": {
    ...
  },
  "GoMinorVersion": 17,
  "ClangHeaders": "C:\\tinygo\\tinygo\\llvm-project\\clang\\lib\\Headers",
  "TestConfig": {
    "CompileTestBinary": false
  }
}

sago35 avatar Mar 14 '22 10:03 sago35

Openocd Interface must be configured.

image

sago35 avatar Mar 14 '22 11:03 sago35

Many Thanks. Regarding the "building tinygo" step. I am using your custom branch that has the PWM max frequency fix, has that been applied to the buildinfo branch ?

@sago35 .. Also, what is the extension name that gives you the vscode picoprobe openocd interface? I only see one for MacOS and I am on Linux .... or does it work for Linux as well .. (My TinyGo extension does not have any options you can set)

bmentink avatar Mar 15 '22 00:03 bmentink

@sago35 I have picoprobe flashing and gdb talking to the board. I just need further help with setting up visualStudio. Thanks. Maybe you can be more specific with the steps, what extension to load, what JSON file to use .. etc i.e launch.json

bmentink avatar Mar 18 '22 02:03 bmentink

Now you can use the vsce package command to create an extension for this branch version.

Once created, install the extension. You only need to configure openocd interface for vscode.

Subsequent steps are as shown in the video below. https://twitter.com/sago35tk/status/1503330468707860481

note: It is not necessary to create launch.json, etc., as they are generated by the extension.

sago35 avatar Mar 18 '22 03:03 sago35

@sago35 I can't use the buildinfo branch unless you have applied the patch for the PWM max freqency fix ... I ask again, have you? If not, how can I patch the buildinf branch with the PWM fix ..?

Is the vsce package command only available in the buildinfo branch as I don't see it anywhere ..

bmentink avatar Mar 21 '22 01:03 bmentink

@bmentink In most cases, it should be a simple matter to do the following

$ git checkout buildinfo
$ git rebase  YOUR_BRANCH_NAME
$ make llvm-build && make
$ ./build/tinygo version

Is the vsce package command only available in the buildinfo branch as I don't see it anywhere ..

vsce is for VSCode. In other words, it is for tinygo-org/vscode-tinygo.

https://code.visualstudio.com/api/working-with-extensions/publishing-extension

sago35 avatar Mar 21 '22 01:03 sago35

I don't have vsce installed, do I have to install that with npm? You say vsce is for VSCode but I see from the link above, that vsce is Video Studio code EXTENSION to manage extensions. I have vscode but not vsce ... I am confused.

Please give clear steps on what to do with visual to get the extension .. i.e treat me like a baby :)

bmentink avatar Mar 21 '22 01:03 bmentink

@bmentink

Please wait a moment while I upload the vsix file. If you are in a hurry, do the following


First, Node.js must be installed. After that, vsce must be installed with the following command.

https://code.visualstudio.com/api/working-with-extensions/publishing-extension Installation Make sure you have Node.js installed. Then run:

$ npm install -g vsce

Now all that remains is to run the following to create vscode-tinygo-<version>.vsix.

https://github.com/tinygo-org/vscode-tinygo#development

To create an extension as a package, run the following command: vsce package This creates a new vscode-tinygo-.vsix file that you can install directly in VS Code.

$ git clone https://github.com/tinygo-org/vscode-tinygo
$ cd vscode-tinygo
$ git checkout gdb
$ vsce package

sago35 avatar Mar 21 '22 01:03 sago35

Perfect .. Thanks :+1:

bmentink avatar Mar 21 '22 01:03 bmentink

@bmentink Unzip the following zip file and load it into vscode.

image

vscode-tinygo-0.3.0-dev1.vsix.zip

sago35 avatar Mar 21 '22 02:03 sago35

I have the extension installed, and I have selected the openocd interface. However the settings.json file and the tasks.json files look wrong, as this is what I get when trying to run a debug session.

The terminal process "/usr/bin/bash '-c', 'tinygo build -o /out.elf -target pico -size short -opt 1 ./bldc'" terminated with exit code: 2.

Am I not mean't to edit those files with the correct paths etc ..?

bmentink avatar Mar 21 '22 03:03 bmentink

@bmentink Please rename or delete the .vscode directory and try again.

sago35 avatar Mar 21 '22 03:03 sago35

@sago35 I tried what you suggested, I removed .vscode, and reinstalled go and the .vsix file as you suggested. the paths still took wrong.

tasks.json looks like:

{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "tinygo build task",
			"type": "shell",
			"command": "tinygo build -o ${env:TMP}/out.elf -target ${config:go.toolsEnvVars.TARGET} -size short -opt 1 ${config:go.toolsEnvVars.TargetPkg}",
			"problemMatcher": [],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}

The error was:

panic: expected main package to have name 'main'

goroutine 1 [running]:
github.com/tinygo-org/tinygo/loader.(*Package).Check(0xc0003b7dc0)
        /home/runner/work/tinygo/tinygo/loader/loader.go:373 +0x318
github.com/tinygo-org/tinygo/loader.(*Program).Parse(0xc00026c0c0)
        /home/runner/work/tinygo/tinygo/loader/loader.go:299 +0xb9
github.com/tinygo-org/tinygo/builder.Build({0x7fff4f07669d, 0x6}, {0x7fff4f076674, 0x60}, 0xc00023e510, 0xc004d0b7e0)
        /home/runner/work/tinygo/tinygo/builder/build.go:177 +0xfa9
main.Build({0x7fff4f07669d, 0x6}, {0x7fff4f076674, 0x8}, 0x1)
        /home/runner/work/tinygo/tinygo/main.go:151 +0x8f
main.main()
        /home/runner/work/tinygo/tinygo/main.go:1337 +0x3aed
The terminal process "/usr/bin/bash '-c', 'tinygo build -o /out.elf -target pico -size short -opt 1 ./bldc'" terminated with exit code: 2.

Terminal will be reused by tasks, press any key to close it.2.

It seems to have ignored me setting the tinygo path to my custom build tinygo ... The tinygo path in my case is: ~/tinygo/build/tinygo

For reference, my settings.json looks like:

{
    "go.toolsEnvVars": {
        "GOROOT": "/home/bmentink/.cache/tinygo/goroot-69d4dd4e42b0c20ced8e32cfa58b0593ee5a356c354f3328202260d070e3e317",
        "GOFLAGS": "-tags=cortexm,baremetal,linux,arm,rp2040,rp,pico,tinygo,math_big_pure_go,gc.conservative,scheduler.tasks,serial.uart",
        "TARGET": "pico",
        "TINYGOROOT": "/home/bmentink/tinygo/build/tinygo",
        "GdbPath": "",
        "OpenocdInterface": "picoprobe",
        "OpenocdTarget": "",
        "X01": "/home/bmentink/Code/tinygo_BLDC_M3_180deg_soft",
        "TargetPkg": "./bldc"
    }
}

bmentink avatar Mar 21 '22 20:03 bmentink

Ahhh, I missed setting the target when the window containing main() is open ... very important step !!!! ... Now I have a permission error as I usually have to run tinygo with sudo ...

tinygo:ld.lld: error: cannot open /usr/local/lib/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0plus/compiler-rt/lib.a: Permission denied
tinygo:ld.lld: error: cannot open /usr/local/lib/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0plus/picolibc/lib.a: Permission denied

bmentink avatar Mar 21 '22 22:03 bmentink

@bmentink

panic: expected main package to have name 'main'

When starting debug, run it while the main package is displayed in VSCode.

Also, please let me know the results of the following command so I can look into it. If the package name (./bldc) is incorrect, please provide the correct value.

$ tinygo build -json -o /out.elf -target pico -size short -opt 1 ./bldc

sago35 avatar Mar 21 '22 22:03 sago35

What do you mean by package name?, In my source directory (where main.go is) there is also a sub-directory called bldc, maybe it is getting confused?

The out put of the above command is:

{
  "Options": {
    "GOOS": "linux",
    "GOARCH": "amd64",
    "GOARM": "6",
    "Target": "pico",
    "Opt": "1",
    "GC": "",
    "PanicStrategy": "print",
    "Scheduler": "",
    "Serial": "",
    "PrintIR": false,
    "DumpSSA": false,
    "VerifyIR": false,
    "Debug": true,
    "PrintSizes": "short",
    "PrintAllocs": null,
    "PrintStacks": false,
    "Tags": "",
    "WasmAbi": "",
    "GlobalValues": {},
    "TestConfig": {
      "CompileTestBinary": false
    },
    "Programmer": "",
    "OpenOCDCommands": null,
    "LLVMFeatures": "",
    "Directory": "",
    "PrintJSON": true
  },
  "Target": {
    "inherits": [
      "cortex-m",
      "cortex-m0plus",
      "rp2040"
    ],
    "llvm-target": "thumbv6m-unknown-unknown-eabi",
    "cpu": "cortex-m0plus",
    "features": "+armv6-m,+soft-float,+strict-align,+thumb-mode,-aes,-bf16,-cdecp0,-cdecp1,-cdecp2,-cdecp3,-cdecp4,-cdecp5,-cdecp6,-cdecp7,-crc,-crypto,-d32,-dotprod,-dsp,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-hwdiv,-hwdiv-arm,-i8mm,-lob,-mve,-mve.fp,-neon,-ras,-sb,-sha2,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp",
    "goos": "linux",
    "goarch": "arm",
    "build-tags": [
      "cortexm",
      "baremetal",
      "linux",
      "arm",
      "rp2040",
      "rp",
      "pico"
    ],
    "gc": "conservative",
    "scheduler": "tasks",
    "serial": "uart",
    "linker": "ld.lld",
    "rtlib": "compiler-rt",
    "libc": "picolibc",
    "automatic-stack-size": true,
    "default-stack-size": 2048,
    "cflags": [
      "-Werror",
      "-fshort-enums",
      "-fomit-frame-pointer",
      "-mfloat-abi=soft",
      "-fno-exceptions",
      "-fno-unwind-tables",
      "-ffunction-sections",
      "-fdata-sections"
    ],
    "ldflags": [
      "--emit-relocs",
      "--gc-sections"
    ],
    "linkerscript": "targets/pico.ld",
    "extra-files": [
      "src/device/arm/cortexm.s",
      "src/internal/task/task_stack_cortexm.S",
      "src/runtime/gc_arm.S",
      "src/device/rp/rp2040.s",
      "targets/pico-boot-stage2.S"
    ],
    "rp2040-boot-patch": true,
    "emulator": null,
    "flash-command": "",
    "gdb": [
      "gdb-multiarch",
      "arm-none-eabi-gdb"
    ],
    "flash-1200-bps-reset": "",
    "serial-port": null,
    "flash-method": "msd",
    "msd-volume-name": "RPI-RP2",
    "msd-firmware-name": "firmware.uf2",
    "uf2-family-id": "0xe48bff56",
    "binary-format": "uf2",
    "openocd-interface": "",
    "openocd-target": "rp2040",
    "openocd-transport": "swd",
    "openocd-commands": null,
    "jlink-device": "",
    "code-model": "",
    "relocation-model": "",
    "wasm-abi": ""
  },
  "GoMinorVersion": 17,
  "ClangHeaders": "/home/bmentink/tinygo/llvm-project/clang/lib/Headers",
  "TestConfig": {
    "CompileTestBinary": false
  }
}

bmentink avatar Mar 21 '22 22:03 bmentink

If by package, you mean main.go, then the above command should be .. tinygo build -json -o /out.elf -target pico -size short -opt 1 main.go ?

bmentink avatar Mar 21 '22 22:03 bmentink

At the moment, I am getting a permission error where it is compiling the .elf file:

error: open /out.elf: permission denied
The terminal process "/usr/bin/bash '-c', 'tinygo build -o /out.elf -target pico -size short -opt 1 ./.'" terminated with exit code: 1.

why is there a "/" in front of the out.elf?

bmentink avatar Mar 21 '22 22:03 bmentink

I think the ~~tinygo build~~ tinygo build -json results are fine.

update: change tinygo build to tinygo build -json

sago35 avatar Mar 21 '22 22:03 sago35

Please see post.. https://github.com/tinygo-org/vscode-tinygo/pull/4#issuecomment-1074499084

bmentink avatar Mar 21 '22 22:03 bmentink

What is the result of the following command? I would like to verify that the version of tinygo I am running in VScode is correct. So, please execute it in the terminal in VSCode as shown in the image.

$ which tinygo

$ tinygo version

image

sago35 avatar Mar 21 '22 22:03 sago35

why is there a "/" in front of the out.elf?

Now ${env:TMP}/out.elf is specified. Perhaps you do not have $TMP in your environment variable. Could you set $TMP once before launching vscode?

$ TMP=$TEMP code .

Perhaps the source code for this PR needs to be corrected.

sago35 avatar Mar 21 '22 23:03 sago35

@bmentink Oops, I didn't convey something important. When you try something new, I want you to delete the .vscode directory and restart vscode.

Files in the .vscode folder will not be overwritten if they already exist. Originally, deleting the .vscode folder was unnecessary, but now it is necessary for debugging purposes.

sago35 avatar Mar 21 '22 23:03 sago35

tinygo version 0.23.0-dev-f42ebe98 linux/amd64 (using go version go1.17.6 and LLVM version 13.0.0)

bmentink avatar Mar 21 '22 23:03 bmentink

Seems I have two tinygo's lying around. I have a early version (not with the PWM fix and buildinfo ) in /usr/local/bin and /usr/local/lib ... I have now removed that old version and am wanting to use the current compiled one in my home directory. Is there a way to install that to /usr/local..?

At the moment vscode can not see the Tinggo:pico stuff any more ... I get the following error when I try to set it:

Could not list TinyGo targets: Error: spawn tinygo ENOENT

I have removed .vscode and restarted vscode/reinstalled extensions

bmentink avatar Mar 21 '22 23:03 bmentink