Skip to content

Conversation

@mikesmitty
Copy link
Contributor

@mikesmitty mikesmitty commented Sep 5, 2025

I recently saw/fell in love with the idea of the https://github.com/piersfinlayson/airfrog debugger tool (wireless SWD+RTT) and got a bit jealous of the probe-rs debugger features/tight integration with VSCode. Turns out it is largely powered by the https://github.com/Marus/cortex-debug extension, which was not originally written with rust in mind so I've been working on some things to make it easier to use (like an example launch.json for VSCode, etc.).

This PR in particular expands RTT debugger compatibility by adding a _SEGGER_RTT symbol alias to machine.rttSerialInstance for RTT debuggers (such as Marus/cortex-debug) that are hard-coded to look for the _SEGGER_RTT symbol specifically and struggle with memory scanning. probe-rs does something similar-ish, but since monitor.go specifically looks for machine.rttSerialInstance we need both to exist to avoid breaking compatibility.

Example output from an nm dump:

$ arm-none-eabi-nm -S -l -p my-program.elf |egrep 'rttSerialInst|SEGGER'
20001068 00000048 b machine.rttSerialInstance   /Users/michaelsmith/git/tinygo/src/machine/serial-rtt.go:19
20001068 00000048 b _SEGGER_RTT

@mikesmitty
Copy link
Contributor Author

Man, having a debugger to give you stack traces and live readouts of memory and registers is so nice, not having stack traces felt like living in the stone age. This is the minimal launch.json I'm working with right now (goes in ${workspaceFolder}/.vscode/). I'm using a nightly build of openocd since v0.12.0 doesn't support rp2350 so some of the flags might be different with older versions. Also, it would be neat if we could add some of this into the TinyGo VSCode plugin like probe-rs does

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "TinyGo Debug",
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "openocd",
            "executable": "./tinygo-build.elf",
            "configFiles": [
                "interface/cmsis-dap.cfg",
                "target/rp2350.cfg"
            ],
            "svdFile": "RaspberryPi::RP2xxx_DFP@0.9.5",
            "preLaunchTask": "${defaultBuildTask}",
            "openOCDLaunchCommands": [
                "adapter speed 10000"
            ],
            "rttConfig": {
                "enabled": true,
                "clearSearch": false,
                "polling_interval": 10,
                "decoders": [
                    {
                        "label": "RTT channel 0",
                        "port": 0,
                        "timestamp": true,
                        "type": "console"
                    }
                ]
            }
        }
    ]
}

And tasks.json I'm using to automatically build it before running the debugger:

{
    "version": "2.0.0",
    "tasks": [
       {
            "label": "TinyGo Build",
            "type": "shell",
            "command": "${userHome}/git/tinygo/build/tinygo build -target=metro-rp2350 -serial=rtt -opt=1 -o ${workspaceFolder}/tinygo-build.elf",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "close": true
            }
        }
    ]
}

@aykevl
Copy link
Member

aykevl commented Sep 9, 2025

Personally I don't care about backwards compatibility in this case. You'd only really use this while debugging, and would rebuild the binary anyway.

What about just renaming the global? That seems like a simpler solution to me than adding an alias. (If going this route, please also add a comment to machine.rttSerialInstance explaining that it is renamed in the compiler).

--- a/compiler/symbol.go
+++ b/compiler/symbol.go
@@ -712,6 +712,9 @@ func (c *compilerContext) getGlobalInfo(g *ssa.Global) globalInfo {
                // Pick the default linkName.
                linkName: g.RelString(nil),
        }
+       if info.linkName == "machine.rttSerialInstance" {
+               info.linkName = "_SEGGER_RTT"
+       }
        // Check for //go: pragmas, which may change the link name (among others).
        doc := c.astComments[info.linkName]
        if doc != nil {

(Ideally this would be done using //go:linkname but that hasn't been implemented for globals yet, though it could be).

@mikesmitty
Copy link
Contributor Author

Oh, sure, that works for me too. I'll look into the globals issue while I'm at it

@mikesmitty
Copy link
Contributor Author

I ended up chasing my tail for a bit thinking the debug info wasn't getting properly attached since it wasn't showing in the nm output, but it's definitely in the DWARF info. I guess it just looks for the name field and not the linkage name

0x0000f262:   DW_TAG_variable
                DW_AT_name	("machine.rttSerialInstance")
                DW_AT_type	(0x0000f18f "machine.rttSerial")
                DW_AT_external	(true)
                DW_AT_decl_file	("/Users/michaelsmith/git/tinygo/src/machine/serial-rtt.go")
                DW_AT_decl_line	(21)
                DW_AT_alignment	(4)
                DW_AT_location	(DW_OP_addr 0x20001068)
                DW_AT_linkage_name	("_SEGGER_RTT")

@deadprogram deadprogram changed the base branch from release to dev September 12, 2025 12:22
@deadprogram
Copy link
Member

@mikesmitty I switched this PR to dev. Can you please rebase it against the latest dev branch? Thanks!

@mikesmitty
Copy link
Contributor Author

Oh whoops, yeah one sec

@deadprogram
Copy link
Member

deadprogram commented Sep 12, 2025

@mikesmitty you might also want to squash this PR into 2 commits, one for the RTT debugger, additions, the other for the go:linkname change. What do you think?

@mikesmitty
Copy link
Contributor Author

Yep, done

@deadprogram
Copy link
Member

Thank you @mikesmitty and to @aykevl for review. Now merging.

@deadprogram deadprogram merged commit 663a94f into tinygo-org:dev Sep 12, 2025
25 of 26 checks passed
@mikesmitty mikesmitty deleted the ms/add-rtt-symbol branch September 12, 2025 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants