HC32L110(五) Ubuntu20.04 VSCode的Debug環境配置

目錄

本文介紹在Ubuntu20.04下, VSCode中如何設置對 HC32L110 進行 debug

倉庫地址: //github.com/IOsetting/hc32l110-template

如果轉載, 請註明出處.

環境說明

本文使用的軟硬體環境已經在前面介紹

硬體

  • 基於 HC32L110 系列MCU的開發板
  • JLink OB

軟體

  • Ubuntu20.04
  • VSCode

配置步驟

安裝配置 Cortex-Debug

在VSCode的插件中, 搜索安裝Cortex-Debug

在VSCode中, 切換到Run And Debug, 點擊上方的 Add Configuration, 會在 .vscode 目錄下的 launch.json (如果沒有會自動創建)中添加配置, 需要增加對應的配置資訊

"configurations": [
        {
            "name": "Cortex Debug",
            "cwd": "${workspaceFolder}",
            "executable": "${workspaceFolder}/Build/app.elf",
            "request": "launch",        // 可以是launch或attach, 後者表示運行中接入, 前者會執行前置任務並重啟
            "type": "cortex-debug",
            "runToEntryPoint": "main",
            "servertype": "jlink",
            "device": "HC32L110X4",     // 如果是32K的版本, 需要修改為 HC32L110X6
            "interface": "swd",
            "runToMain": true,          // false則從 reset handler 開始停留
            "preLaunchTask": "build",   // 根據 tasks.json 中配置的任務填寫, 
            // "preLaunchCommands": ["Build all"], // 如果不使用 preLaunchTask, 可以用這個參數指定命令行命令
            "svdFile": "",              // svd 用於觀察外設
            "showDevDebugOutput": "vscode", // 輸出的日誌級別, parsed:解析後的內容, raw:直接輸出, vscode:包含scode調用和raw
            "swoConfig":
            {
                "enabled": true,
                "cpuFrequency": 24000000,
                "swoFrequency":  4000000,
                "source": "probe",
                "decoders":
                [
                    {
                        "label": "ITM port 0 output",
                        "type": "console",
                        "port": 0,
                        "showOnStartup": true,
                        "encoding": "ascii"
                    }
                ]
            }
        }
    ]

具體的配置項含義, 可以參考 Debug Attributes

同時在 .vscode/settings.json 中增加以下配置, 如果文件不存在則創建. 路徑根據自己的環境修改

{
    "cortex-debug.gdbPath": "/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb",
    "cortex-debug.JLinkGDBServerPath": "/opt/SEGGER/JLink/JLinkGDBServerCLExe",
}

修改 rules.mk

在rules.mk中開啟debug, 涉及到兩處, OPT要使用-O0, 表示不執行任何優化. 使用優化後程式碼中的部分變數在編譯後會被丟棄無法跟蹤

OPT         ?= -O0

CFLAGS中增加gdb輸出

CFLAGS      += -g -gdwarf-2 # original: -g

這樣編譯後, 尺寸會比原先大不少

Ubuntu20.04下遇到的問題

以上配置後, 點擊 Run And Debug 中的綠色運行圖標應該就能啟動Debug, 但是可能在看到以下輸出後就沒有反應了

Reading symbols from /opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-objdump --syms -C -h -w /home/milton/Stm32Projects/hc32_workspace/hc32l110-template/Build/app.elf
Reading symbols from /opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-nm --defined-only -S -l -C -p /home/milton/Stm32Projects/hc32_workspace/hc32l110-template/Build/app.elf
Launching GDB: /opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb -q --interpreter=mi2
1-gdb-version

這時候到命令行執行一下命令/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb -q --interpreter=mi2就能看到問題

1. libncursesw.so.5: cannot open shared object file

首先是會有這樣的錯誤輸出

/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb -q --interpreter=mi2
/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb: error while loading shared libraries: libncursesw.so.5: cannot open shared object file: No such file or directory

觀察ldd可以看到缺少兩個so: libncursesw.so.5, libpython3.6m.so.1.0

ldd /opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb
    linux-vdso.so.1 (0x00007ffc82998000)
    libncursesw.so.5 => not found
    libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f326ba1e000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f326ba18000)
    libpython3.6m.so.1.0 => not found
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f326b9f5000)
    libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f326b9f0000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f326b89f000)
    libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f326b871000)
    libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f326b68f000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f326b674000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f326b482000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f326ba6e000)

libncursesw.so.5可以通過安裝 libncursesw5 可以解決

sudo apt install libncursesw5

2. libpython3.6m.so.1.0: cannot open shared object file

這樣就剩下另一個動態連接庫無法找到

/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb -q --interpreter=mi2
/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

但是 Ubuntu20.04 自帶的 Python3.8, 無法安裝 Python3.6

~$ sudo apt-get install libpython3.6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'libpython3.6-stdlib' for regex 'libpython3.6'
0 upgraded, 0 newly installed, 0 to remove and 29 not upgraded.

簡單粗暴的解決方法就是直接用3.8的代替

cd /usr/lib/x86_64-linux-gnu/
$ ll libpython*
lrwxrwxrwx 1 root root      19 Jul  1 20:27 libpython2.7.so.1 -> libpython2.7.so.1.0
-rw-r--r-- 1 root root 3455600 Jul  1 20:27 libpython2.7.so.1.0
lrwxrwxrwx 1 root root      55 Jun 23 04:18 libpython3.8.a -> ../python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a
lrwxrwxrwx 1 root root      17 Jun 23 04:18 libpython3.8.so -> libpython3.8.so.1
lrwxrwxrwx 1 root root      19 Jun 23 04:18 libpython3.8.so.1 -> libpython3.8.so.1.0
-rw-r--r-- 1 root root 5449112 Jun 23 04:18 libpython3.8.so.1.0
$ sudo ln -s libpython3.8.so.1.0 libpython3.6m.so.1.0

這時候再運行就可以啟動了

$ /opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb -q --interpreter=mi2
/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb: Symbol `PyBool_Type' has different size in shared object, consider re-linking
/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb: Symbol `PySlice_Type' has different size in shared object, consider re-linking
/opt/gcc-arm/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin/arm-none-eabi-gdb: Symbol `PyFloat_Type' has different size in shared object, consider re-linking
=thread-group-added,id="i1"
(gdb)

使用

點擊 Run And Debug 中的綠色運行圖標啟動Debug, F10 下一步, F11 進入, Shift + F11 跳出. 左側能觀察到變數值和暫存器值. 操作方法和其它IDE基本一致.

參考