用VS代码调试NASM [英] Debugging NASM in VS code
问题描述
我有一个简单的C ++程序,该程序调用了一些NASM代码:
main.cpp:
#include <iostream>
extern "C" int foo();
int main() {
std::cout << "The result is: " << foo() << std::endl;
return 0;
}
foo.asm:
bits 64
global foo
section .text
foo:
mov rax, 123
inc rax
ret
我可以使用CMake编译所有内容
cmake_minimum_required (VERSION 3.15)
project (assembly-x64 LANGUAGES CXX ASM_NASM)
# old school CMAKE to handle NASM formats
if(WIN32)
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F cv8")
set(CMAKE_ASM_NASM_OBJECT_FORMAT win64)
elseif(APPLE)
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F dwarf")
set(CMAKE_ASM_NASM_OBJECT_FORMAT macho64)
else()
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F dwarf")
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
endif()
add_executable(assembly-x64)
target_compile_features(assembly-x64 PUBLIC cxx_std_17)
target_sources(assembly-x64 PRIVATE main.cpp foo.asm)
我得到正确的结果.但是,我希望能够像C ++代码一样调试汇编代码.我可以在foo
函数上创建一个断点(虽然不使用GUI),但是在暂停时它不会显示相应的源位置.有没有办法解决这个问题?我希望能够监视寄存器等.不确定在VS代码中是否可行.
简短答案:否.在linux上,从vs-code启动gdb调试asm代码导致快速崩溃.它甚至不允许您在asm代码上放置断点.
阅读更长的答案,以备不时之需.
我假设您没有使用Linux,因为我无法使用您提供的cmake在Linux上构建项目.我必须添加以下内容:
...
else()
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F dwarf")
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
# had to add this to build on linux
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
<FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")
endif()
...
好,所以现在该项目已构建并正在运行.但是,坏消息是我无法设置调试"代理程序. VS-Code的环境. VS-Code最初并没有很好的汇编支持,因此我对此并不感到惊讶.我尝试多次配置launch.json
,但是每次启动GDB时,VS-Code崩溃.在Windows或Mac上可能会或可能不会发生这种情况,因此我无法针对这些平台进行回答,但我认为在该平台上将无法使用.
我个人只是直接在终端上使用gdb
,因为一旦您弄清了它,它就会更强大且更易于使用.我将为您提供两个工作流程,您可以使用它们代替Vs代码.
使用终端
- 构建可执行文件
- 接下来使用gdb如下启动它:
gdb ./assembly-64 --tui
这将打开gdb
并带您进入推针屏幕.
- 让我们假设对于这种类型,我们想在
foo
处设置一个断点:
b foo
- 现在我们的断点都已设置.我们准备开始调试会话.类型:
run
- 它将在
foo
处断裂.
但是请稍等,没有寄存器,我们应该如何查看它们?这是我最喜欢的关于gdb的部分.类型:
layout regs
您将在顶部获得一个漂亮的外观窗口,向您显示所有寄存器.甚至会在寄存器更改时突出显示它们,从而使您可以轻松监视更改.
- 除此之外,使用
n
下一步,使用si
进入.基本来说就足够了.如果要在某个存储器位置或寄存器中查看该值.类型:
print $rax
还有很多其他功能,但这将使您快速入门.
GUI
当我发现QtCreator
可以很好地调试asm + cpp文件时,我感到非常惊喜.只需加载您的cmake项目并放置断点即可.您可以从Menu->Window->Views->Registers
启用寄存器窗格.截图:
还有其他GUI(用于gdb),nemiver,ddd等.
I have a simple C++ program that calls some NASM code:
main.cpp:
#include <iostream>
extern "C" int foo();
int main() {
std::cout << "The result is: " << foo() << std::endl;
return 0;
}
foo.asm:
bits 64
global foo
section .text
foo:
mov rax, 123
inc rax
ret
I can compile everything with CMake
cmake_minimum_required (VERSION 3.15)
project (assembly-x64 LANGUAGES CXX ASM_NASM)
# old school CMAKE to handle NASM formats
if(WIN32)
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F cv8")
set(CMAKE_ASM_NASM_OBJECT_FORMAT win64)
elseif(APPLE)
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F dwarf")
set(CMAKE_ASM_NASM_OBJECT_FORMAT macho64)
else()
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F dwarf")
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
endif()
add_executable(assembly-x64)
target_compile_features(assembly-x64 PUBLIC cxx_std_17)
target_sources(assembly-x64 PRIVATE main.cpp foo.asm)
and I get the correct result. However, I'd like to be able to debug the assembly code just like I would the C++ code. I can create a breakpoint on the foo
function (not using the GUI though), but it doesn't show me the corresponding source location when it pauses. Is there a way around that issue? I'd like to be able to watch registers, etc. Not sure if it's possible in VS code.
Short Answer: No. On linux, launching gdb from vs-code to debug asm code resulted in a quick crash. It doesn't even let you put breakpoints on asm code.
Read the longer answer in case you want to know about the alternatives.
I assume you are not using Linux, because I was unable to build the project on Linux with the cmake you provided. I had to add the following:
...
else()
set(CMAKE_ASM_NASM_FLAGS_DEBUG "-g -F dwarf")
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
# had to add this to build on linux
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
<FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")
endif()
...
Ok, so now the project is built and running. However, the bad news is that I was unable to set up "debugging" environment with VS-Code. VS-Code doesn't really have good assembly support to begin with so I am not really surprised here. I tried to configure launch.json
multiple times, but everytime I launch GDB, VS-Code crashes. This may or may not happen on Windows or Mac, so I can not answer for those platforms but I assume it will not work there.
I personally just use gdb
from terminal directly because it is more powerful and easy to use once you figure it out. I will give you two work flows which you can use instead of Vs-code.
Use terminal
- Build your executable
- Next launch it using gdb like this:
gdb ./assembly-64 --tui
This will open gdb
and bring you to the tui screen.
- Lets assume we want to put a break point at
foo
, for this type:
b foo
- Now our breakpoints are all set. We are ready to start our debugging session. Type:
run
- It will go and break at
foo
.
But wait a minute, there are no registers, how should we see them? This is my favourite part about gdb. Type:
layout regs
And you will get a beautiful look window at top showing you all the registers. It will even highlight the registers as they change making it easy for you to monitor changes.
- Other than this, Use
n
to step next, usesi
to step into. That's pretty much it for the basics. If you want to see the value at some memory location, or a register. Type:
print $rax
There's a lot more to this, but this will give you a quick start.
GUI
I was pleasantly surprised when I discovered that QtCreator
can debug asm + cpp files very nicely. Just load up your cmake project and place your breakpoints. You can enable registers pane from Menu->Window->Views->Registers
. Screenshot:
There are other guis(for gdb) out there, nemiver, ddd etc.
这篇关于用VS代码调试NASM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!