DWARF表路径错误排查:armclang编译后Keil调试无法识别源文件
I’ve helped troubleshoot exactly this kind of problem with armclang and Keil before, so let’s break down the most likely causes and fixes based on your scenario:
1. Build System Working Directory & Path Passing Errors
This is the #1 culprit in most cases. If your build tool (Makefile, CMake, or custom script) switches to the build/obj directory to run the compile command, armclang will use that working directory as the base for resolving source file paths.
For example:
- If you run
armclang -g src/main.c -c -o build/obj/main.ofrom your project root (F:/MyProject/), the DWARF info will correctly recordF:/MyProject/src/main.c. - But if your build script cd’s into
build/objfirst and runsarmclang -g ../../src/main.c -c -o main.o, armclang will resolve the relative path against the current working directory (build/obj), leading to a mangled absolute path in DWARF (like theF:/MyProject/build/obj\main.cyou saw). Mixing Windows-style\and Unix-style/separators here can make the problem worse.
Fix: Update your build script to run compile commands from the project root, or pass absolute paths to your source files instead of relative ones. For Makefile, you can use $(abspath src/main.c) to ensure the full correct path is passed to armclang.
2. Accidental Use of Path Remapping Flags
armclang supports flags like -fdebug-prefix-map=old-path=new-path to rewrite source paths in DWARF info. If your build system incorrectly sets this flag (e.g., mapping ../src to ./), it will overwrite the actual source path with the build directory path.
Fix: Check your compile flags for any instances of -fdebug-prefix-map or -ffile-prefix-map. If they’re present, verify the mapping logic is correct—remove them entirely if you don’t need path remapping.
3. Windows Path Separator Compatibility Quirks
Windows uses \ as the path separator, but armclang (which has roots in Unix-based toolchains) sometimes handles mixed separators poorly. If your build script passes paths with a mix of / and \ (e.g., src\main.c instead of src/main.c), the compiler may resolve the path incorrectly when generating DWARF info.
Fix: Standardize on / for all paths in your build script. For Makefile, you can use $(subst \,/,$(SOURCE_FILE)) to convert Windows-style paths to Unix-style before passing them to armclang.
4. Missing Keil Debug Path Mapping (Backup Fix)
While you should fix the root cause in the compile step, if you need a temporary workaround, Keil lets you manually map source paths. Go to your project’s Debug Configuration → Source Code Paths, add F:/MyProject/src/ to the list, and Keil will search this directory for source files even if the DWARF path is wrong.
内容的提问来源于stack exchange,提问作者Michał Stroka

