Marus / cortex-debug

Visual Studio Code extension for enhancing debug capabilities for Cortex-M Microcontrollers
MIT License
1.02k stars 240 forks source link

Impossible to debug application relocated in Flash at offset 0x0802 0000 #976

Closed ANauzet closed 9 months ago

ANauzet commented 9 months ago

Describe the bug Debugging code that is relocated in Flash does not work

I have a Bootloader @0x0800 0000 and an Application relocated @0x0802 0000 by modifying linker script and VECT_TAB_OFF 0x0002 0000. In execution, the bootloader call the application and the application works fine. But when trying to debug the application, impossible to reach main() If the application is relocated to 0x0800 0000, debug is possible

To Reproduce Steps to reproduce the behavior:

  1. Start debug session
  2. Breakpoint on start of main() is never reach
  3. Cortex-debug session do not crash. I can click pause/play. In pause, no code line is highlight. Call stack display: ??@0x08001d86, @0xffffffed, ??@0x0800b2c8, ??@0x0800aba6
  4. Note that code is executed. I have trace displayed on UART that show application is running.

Expected behavior

Debug should be possible on application executed from Flash offset

Environment (please complete the following information):

[comment]: <> Whenever possible, please make sure you are using the latest versions of VSCode and our extension

Please include launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Cortex Debug",
            "cwd": "${workspaceFolder}",
            "executable": "./build/Debug/APP.bin",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "openocd",
            "device": "STM32F407",
            "configFiles": [
                "interface/stlink.cfg",
                "target/stm32f4x.cfg"
            ],
            "symbolFiles": [
                "${workspaceFolder}/build/Debug/APP.elf"                
            ],
            "loadFiles": [
                "${workspaceFolder}/build/Debug/APP.hex"
            ],
            "gdbPath": "arm-none-eabi-gdb.exe",
            "interface": "swd"
            "showDevDebugOutput": "raw"
        }
    ]
}

Attach text from Debug Console

Please enable debug output in your launch.json ("showDevDebugOutput": "raw"). It this is too large, please attach it as a file

Cortex-Debug: VSCode debugger extension version 1.12.1 git(652d042). Usage info: https://github.com/Marus/cortex-debug#usage
"configuration": {
    "name": "Cortex Debug",
    "cwd": "C:\\Work\\Projets\\PRJ001\\Application",
    "executable": "C:\\Work\\Projets\\PRJ001\\Application\\build\\Debug\\APP.elf",
    "request": "launch",
    "type": "cortex-debug",
    "servertype": "openocd",
    "device": "STM32F407",
    "configFiles": [
        "interface/stlink.cfg",
        "target/stm32f4x.cfg"
    ],
    "symbolFiles": [
        {
            "file": "C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf",
            "sections": [],
            "sectionMap": {}
        }
    ],
    "loadFiles": [
        "C:/Work/Projets/PRJ001/Application/build/Debug/APP.hex"
    ],
    "gdbPath": "arm-none-eabi-gdb.exe",
    "interface": "swd",
    "showDevDebugOutput": "raw",
    "__configurationTarget": 6,
    "gdbServerConsolePort": 55878,
    "pvtAvoidPorts": [],
    "chainedConfigurations": {
        "enabled": false
    },
    "debuggerArgs": [],
    "swoConfig": {
        "enabled": false,
        "decoders": [],
        "cpuFrequency": 0,
        "swoFrequency": 0,
        "source": "probe"
    },
    "rttConfig": {
        "enabled": false,
        "decoders": []
    },
    "graphConfig": [],
    "preLaunchCommands": [],
    "postLaunchCommands": [],
    "preAttachCommands": [],
    "postAttachCommands": [],
    "preRestartCommands": [],
    "postRestartCommands": [],
    "preResetCommands": [],
    "postResetCommands": [],
    "searchDir": [],
    "toolchainPrefix": "arm-none-eabi",
    "extensionPath": "c:/Users/T467/.vscode/extensions/marus25.cortex-debug-1.12.1",
    "registerUseNaturalFormat": true,
    "variableUseNaturalFormat": false,
    "pvtVersion": "1.12.1",
    "__sessionId": "d26c54e2-ec0f-4a9e-b0c4-a86f3cb0fd2e",
    "pvtShowDevDebugOutput": "raw"
}
Reading symbols from arm-none-eabi-objdump.exe --syms -C -h -w C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf
Reading symbols from arm-none-eabi-nm.exe --defined-only -S -l -C -p C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf
Launching GDB: arm-none-eabi-gdb.exe -q --interpreter=mi2
1-gdb-version
Launching gdb-server: openocd.exe -c "gdb_port 50000" -c "tcl_port 50001" -c "telnet_port 50002" -s "C:\\Work\\Projets\\PRJ001\\Application" -f "c:/Users/T467/.vscode/extensions/marus25.cortex-debug-1.12.1/support/openocd-helpers.tcl" -f interface/stlink.cfg -f target/stm32f4x.cfg
    Please check TERMINAL tab (gdb-server) for output from openocd.exe
Finished reading symbols from objdump: Time: 83 ms
Finished reading symbols from nm: Time: 92 ms
-> =thread-group-added,id="i1"
-> ~"GNU gdb (GNU Tools for STM32 10.3-2021.10.20211105-1100) 10.2.90.20210621-git\n"
-> ~"Copyright (C) 2021 Free Software Foundation, Inc.\n"
-> ~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law."
-> ~"\nType \"show copying\" and \"show warranty\" for details.\n"
-> ~"This GDB was configured as \"--host=x86_64-w64-mingw32 --target=arm-none-eabi\".\n"
-> ~"Type \"show configuration\" for configuration details.\n"
-> ~"For bug reporting instructions, please see:\n"
-> ~"<https://www.gnu.org/software/gdb/bugs/>.\n"
-> ~"Find the GDB manual and other documentation resources online at:\n    <http://www.gnu.org/software/gdb/documentation/>."
-> ~"\n\n"
-> ~"For help, type \"help\".\n"
-> ~"Type \"apropos word\" to search for commands related to \"word\".\n"
-> 1^done
2-gdb-set mi-async on
-> 2^done
3-interpreter-exec console "set print demangle on"
-> 3^done
4-interpreter-exec console "set print asm-demangle on"
-> =cmd-param-changed,param="print asm-demangle",value="on"
-> 4^done
5-enable-pretty-printing
-> 5^done
6-interpreter-exec console "source c:/Users/T467/.vscode/extensions/marus25.cortex-debug-1.12.1/support/gdbsupport.init"
-> 6^done
7-interpreter-exec console "source c:/Users/T467/.vscode/extensions/marus25.cortex-debug-1.12.1/support/gdb-swo.init"
-> =cmd-param-changed,param="language",value="c"
-> =cmd-param-changed,param="language",value="auto"
-> 7^done
8-interpreter-exec console "set output-radix 0x10"
-> ~"Output radix now set to decimal 16, hex 10, octal 20.\n"
Output radix now set to decimal 16, hex 10, octal 20.
-> =cmd-param-changed,param="output-radix",value="16"
-> 8^done
9-interpreter-exec console "set input-radix 0xa"
-> ~"Input radix now set to decimal 10, hex a, octal 12.\n"
Input radix now set to decimal 10, hex a, octal 12.
-> 9^done
10-interpreter-exec console "add-symbol-file \"C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf\""
-> ~"add symbol table from file \"C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf\"\n"
add symbol table from file "C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf"
-> ~"(y or n) [answered Y; input not from terminal]\n"
(y or n) [answered Y; input not from terminal]
-> ~"Reading symbols from C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf...\n"
Reading symbols from C:/Work/Projets/PRJ001/Application/build/Debug/APP.elf...
-> 10^done
11-target-select extended-remote localhost:50000
-> =thread-group-started,id="i1",pid="42000"
-> &"warning: No executable has been specified and target does not support\ndetermining executable automatically.  Try using the \"file\" command."
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
-> &"\n"

-> =thread-created,id="1",group-id="i1"
-> ~"0x08001d86 in ?? ()\n"
0x08001d86 in ?? ()
-> *stopped,frame={addr="0x08001d86",func="??",args=[],arch="arm"},thread-id="1",stopped-threads="all"
mi2.status = stopped
Program stopped, probably due to a reset and/or halt issued by debugger
-> 11^connected
12-interpreter-exec console "monitor reset halt"
-> @"target halted due to debug-request, current mode: Thread \n"
target halted due to debug-request, current mode: Thread
-> @"xPSR: 0x01000000 pc: 0x08002494 msp: 0x20020000\n"
xPSR: 0x01000000 pc: 0x08002494 msp: 0x20020000
-> 12^done
13-file-exec-file "C:/Work/Projets/PRJ001/Application/build/Debug/APP.hex"
-> 13^done
14-target-download
-> 14+download,{section=".sec1",section-size="392",total-size="94360"}
-> 14+download,{section=".sec1",section-sent="392",section-size="392",total-sent="392",total-size="94360"}
-> 14+download,{section=".sec2",section-size="65136",total-size="94360"}
-> 14+download,{section=".sec3",section-size="19236",total-size="94360"}
-> 14+download,{section=".sec4",section-size="9596",total-size="94360"}
-> 14^done,address="0x08022494",load-size="94360",transfer-rate="242880",write-rate="10484"
15-interpreter-exec console "monitor reset halt"
-> @"target halted due to debug-request, current mode: Thread \n"
target halted due to debug-request, current mode: Thread
-> @"xPSR: 0x01000000 pc: 0x08002494 msp: 0x20020000\n"
xPSR: 0x01000000 pc: 0x08002494 msp: 0x20020000
-> 15^done
16-break-insert "c:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c:106"
-> ~"Note: automatically using hardware breakpoints for read-only addresses.\n"
Note: automatically using hardware breakpoints for read-only addresses.
-> 16^done,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x08021054",func="AppOnlyFunction",file="C:/Work/Projets/PRJ001/Application/Core/Src/main.c",fullname="C:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c",line="106",thread-groups=["i1"],times="0",original-location="c:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c:106"}
17-break-insert "c:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c:156"
-> 17^done,bkpt={number="2",type="breakpoint",disp="keep",enabled="y",addr="0x08021066",func="main",file="C:/Work/Projets/PRJ001/Application/Core/Src/main.c",fullname="C:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c",line="156",thread-groups=["i1"],times="0",original-location="c:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c:156"}
18-break-insert "c:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c:223"
-> 18^done,bkpt={number="3",type="breakpoint",disp="keep",enabled="y",addr="0x08021134",func="main",file="C:/Work/Projets/PRJ001/Application/Core/Src/main.c",fullname="C:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c",line="223",thread-groups=["i1"],times="0",original-location="c:\\Work\\Projets\\PRJ001\\Application\\Core\\Src\\main.c:223"}
19-break-insert "c:\\Work\\Projets\\PRJ001\\Application\\Core\\BootLoader\\Src\\mainBoot.c:218"
-> 19^error,msg="No source file named c:\\Work\\Projets\\PRJ001\\Application\\Core\\BootLoader\\Src\\mainBoot.c."
20-break-insert "c:\\Work\\Projets\\PRJ001\\Application\\Core\\Startup\\startup_stm32f407zgtx.s:61"
-> 20^done,bkpt={number="4",type="breakpoint",disp="keep",enabled="y",addr="0x08022494",file="C:/Work/Projets/PRJ001/Application/Core/Startup\\startup_stm32f407zgtx.s",fullname="C:\\Work\\Projets\\PRJ001\\Application\\Core\\Startup\\startup_stm32f407zgtx.s",line="61",thread-groups=["i1"],times="0",original-location="c:\\Work\\Projets\\PRJ001\\Application\\Core\\Startup\\startup_stm32f407zgtx.s:61"}
21-break-insert "c:\\Work\\Projets\\PRJ001\\Application\\Drivers\\STM32F4xx_HAL_Driver\\Src\\stm32f4xx_hal.c:160"
-> 21^done,bkpt={number="5",type="breakpoint",disp="keep",enabled="y",addr="0x080258ea",func="HAL_Init",file="C:/Work/Projets/PRJ001/Application/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c",fullname="C:\\Work\\Projets\\PRJ001\\Application\\Drivers\\STM32F4xx_HAL_Driver\\Src\\stm32f4xx_hal.c",line="160",thread-groups=["i1"],times="0",original-location="c:\\Work\\Projets\\PRJ001\\Application\\Drivers\\STM32F4xx_HAL_Driver\\Src\\stm32f4xx_hal.c:160"}
Returning dummy thread-id to workaround VSCode issue with pause button not working
Returning dummy stack frame to workaround VSCode issue with pause button not working: {"threadId":1,"startFrame":0,"levels":20}
22-exec-continue --all
-> 22^running
-> *running,thread-id="all"
mi2.status = running

Additional context Add any other context about the problem here.

ANauzet commented 9 months ago

Maybe the solution is just to add a parameter to tell dbg that it should start at offset. But I could not found any information about it

haneefdm commented 9 months ago

Maybe the solution is just to add a parameter to tell dbg that it should start at offset. But I could not found any information about it

First of all, we do not do anything to set the start address. It is done by your processor and your reset vector,

Secondly, you have total control over what commands are executed by using pre/post/override options. Please read our Wiki and other documentation

https://github.com/Marus/cortex-debug/blob/master/debug_attributes.md

Finally, can you get all of this working using command line tools? If you can't, neither can we.

m4x1r commented 9 months ago

My issue is the same. After adding “overrideLaunchCommands” to the “configurations” in launch.json, I am able to debug successfully.

"configurations": [
    {
      "showDevDebugOutput": "parsed",
      "cwd": "${workspaceRoot}",
      "executable": "${workspaceRoot}/build/app.elf",
      "name": "Debug STM32",
      "request": "launch",
      "type": "cortex-debug",
      "servertype": "openocd",
      "preLaunchTask": "CMake: build",
      "device": "stm32f407",
      "configFiles": [
        "openocd.cfg"
      ],
      "overrideResetCommands":[],
      "overrideLaunchCommands":[
        "cd ${workspaceRoot}/build",
        "file app.elf",
        "target extended-remote localhost:50000",
        "monitor reset halt",
        "load",
        "set output-radix 16"
      ],
      "svdFile": "${workspaceRoot}/STM32F407.svd"
    }
]