Disassemble C, C++ or Fortran code under cursor
Disaster lets you press C-c d
to see the compiled assembly code for the
C, C++ or Fortran file you're currently editing. It even jumps to and
highlights the line of assembly corresponding to the line beneath your cursor.
It works by creating a .o
file using make
(if you have a Makefile), or
cmake
(if you have a compile_commands.json
file) or the default system
compiler. It then runs that file through objdump
to generate the
human-readable assembly.
Make sure to place disaster.el
somewhere in the load-path
, then you should
be able to run M-x disaster
. If you want, you add the following lines to
your .emacs
file to register the C-c d
shortcut for invoking disaster
:
(add-to-list 'load-path "/PATH/TO/DISASTER")
(require 'disaster)
(define-key c-mode-map (kbd "C-c d") 'disaster)
(define-key fortran-mode-map (kbd "C-c d") 'disaster)
For Doom Emacs users, you can add this snippet to your packages.el
.
(package! disaster
:recipe (:host github :repo "jart/disaster"))
And this to your config.el
:
(use-package! disaster
:commands (disaster)
:init
;; If you prefer viewing assembly code in `nasm-mode` instead of `asm-mode`
(setq disaster-assembly-mode 'nasm-mode)
(map! :localleader
:map (c++-mode-map c-mode-map fortran-mode-map)
:desc "Disaster" "d" #'disaster))
Command line options to pass to make if a Makefile is found.
Which mode to use to view assembly code.
The command for your C compiler.
The command for your C++ compiler.
The command for your Fortran compiler.
Command line options to use when compiling C.
Command line options to use when compiling C++.!
Command line options to use when compiling Fortran.
The command name and flags for running objdump.
Buffer name to use for assembler output.
Buffer name to use for objdump assembly output.
List of lists of files that may indicate software project root directory. Sublist are ordered from highest to lowest precedence.
Regexp for C source files.
Regexp for C++ source files.
Regexp for Fortran source files.
Create compile command for a Make-based project. MAKE-ROOT: path to build root, CWD: path to current source file, REL-OBJ: path to object file (relative to project root), OBJ-FILE: full path to object file (build root!) PROJ-ROOT: path to project root, REL-FILE FILE.
Create compile command for a CMake-based project. MAKE-ROOT: path to build root, CWD: path to current source file, REL-OBJ: path to object file (relative to project root), OBJ-FILE: full path to object file (build root!) PROJ-ROOT: path to project root, REL-FILE FILE.
Get the .o object file name from a full COMPILE-CMD.
Create the actual compile command. USE-CMAKE: non NIL to use CMake, NIL to use Make or default compiler options, MAKE-ROOT: path to build root, CWD: path to current source file, REL-OBJ: path to object file (relative to project root), OBJ-FILE: full path to object file (build root!) PROJ-ROOT: path to project root, REL-FILE FILE.
Show assembly code for current line of C/C++ file. Here's the logic path it follows:
- Is there a complile_commands.json in this directory? Get the object file name for the current file, and run it associated command.
- Is there a Makefile in this directory? Run
make bufname.o
. - Or is there a Makefile in a parent directory? Run
make -C .. bufname.o
. - Or is this a C file? Run
cc -g -c -o bufname.o bufname.c
- Or is this a C++ file? Run
c++ -g -c -o bufname.o bufname.c
- If build failed, display errors in compile-mode.
- Run objdump inside a new window while maintaining focus.
- Jump to line matching current line. If FILE and LINE are not specified, the current editing location is used.
General-purpose Heuristic to detect bottom directory of project.
First, this will try to use (vc-root-dir)
to guess the project
root directory, and falls back to manual check wich works by scanning
parent directories of FILE (using disaster--find-parent-dirs
) for certain
types of files like a .projectile
file or a Makefile
(which is less
preferred).
The canonical structure of LOOKS is a list of lists of files
to look for in each parent directory where sublists are ordered
from highest precedence to lowest. However you may specify
LOOKS as a single string or a list of strings for your
convenience. If LOOKS is not specified, it'll default to
disaster-project-root-files
.
Find the root of build directory. USE-CMAKE: non nil to use CMake's compile_commands.json, PROJECT-ROOT: root directory of the project.