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


    目录

    本文介绍在Ubuntu20.04下, VSCode中如何设置对 HC32L110 进行 debug

    仓库地址: https://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"
                        }
                    ]
                }
            }
        ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    具体的配置项含义, 可以参考 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",
    }
    
    • 1
    • 2
    • 3
    • 4

    修改 rules.mk

    在rules.mk中开启debug, 涉及到两处, OPT要使用-O0, 表示不执行任何优化. 使用优化后代码中的部分变量在编译后会被丢弃无法跟踪

    OPT         ?= -O0
    
    • 1

    CFLAGS中增加gdb输出

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

    这样编译后, 尺寸会比原先大不少

    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
    
    • 1
    • 2
    • 3
    • 4

    这时候到命令行执行一下命令/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
    
    • 1
    • 2

    观察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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    libncursesw.so.5可以通过安装 libncursesw5 可以解决

    sudo apt install libncursesw5
    
    • 1

    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
    
    • 1
    • 2

    但是 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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    简单粗暴的解决方法就是直接用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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这时候再运行就可以启动了

    $ /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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用

    点击 Run And Debug 中的绿色运行图标启动Debug, F10 下一步, F11 进入, Shift + F11 跳出. 左侧能观察到变量值和寄存器值. 操作方法和其它IDE基本一致.

    参考

  • 相关阅读:
    Linux文件权限修改、用户设置命令
    Docker的数据管理
    75、SpringBoot 整合 MyBatis------使用 Mapper 作为 Dao 组件
    NPM 如何换源?
    二、Docker基本操作
    第七章 设计zrlog项目的测试用例(7.1章节)
    统计射击比赛成绩(JavaScript)
    docker 安装jenkins 跨域csrf
    Java多线程(三)
    简单线性规划问题
  • 原文地址:https://blog.csdn.net/michaelchain/article/details/126683380