Git Product home page Git Product logo

disaster's Introduction

Emacs Logo

disaster.el

Disassemble C, C++ or Fortran code under cursor


License GPLv2 MELPA

Screenshot of a C example

Screenshot of a Fortran example

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.

Installation

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)

Doom Emacs

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))

Customization Documentation

disaster-make-flags

Command line options to pass to make if a Makefile is found.

disaster-assembly-mode

Which mode to use to view assembly code.

disaster-cc

The command for your C compiler.

disaster-cxx

The command for your C++ compiler.

disaster-fortran

The command for your Fortran compiler.

disaster-cflags

Command line options to use when compiling C.

disaster-cxxflags

Command line options to use when compiling C++.!

disaster-fortranflags

Command line options to use when compiling Fortran.

disaster-objdump

The command name and flags for running objdump.

disaster-buffer-compiler

Buffer name to use for assembler output.

disaster-buffer-assembly

Buffer name to use for objdump assembly output.

disaster-project-root-files

List of lists of files that may indicate software project root directory. Sublist are ordered from highest to lowest precedence.

disaster-c-regexp

Regexp for C source files.

disaster-cpp-regexp

Regexp for C++ source files.

disaster-fortran-regexp

Regexp for Fortran source files.

Function and Macro Documentation

(disaster-create-compile-command-make MAKE-ROOT CWD REL-OBJ OBJ-FILEPROJ-ROOT REL-FILE FILE)

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.

(disaster-create-compile-command-cmake MAKE-ROOT CWD REL-OBJ OBJ-FILEPROJ-ROOT REL-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.

(disaster-get-object-file-path-cmake COMPILE-CMD)

Get the .o object file name from a full COMPILE-CMD.

(disaster-create-compile-command USE-CMAKE MAKE-ROOT CWD REL-OBJOBJ-FILE PROJ-ROOT REL-FILE FILE)

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.

(disaster &optional FILE LINE)

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.

(disaster-find-project-root &optional LOOKS FILE)

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.

(disaster-find-build-root USE-CMAKE PROJECT-ROOT)

Find the root of build directory. USE-CMAKE: non nil to use CMake's compile_commands.json, PROJECT-ROOT: root directory of the project.


Markdown README file generated by make-readme-markdown.el

disaster's People

Contributors

abougouffa avatar cestdiego avatar evmar avatar hying-caritas avatar jart avatar macpapo avatar maskray avatar mgalgs avatar veigaribo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

disaster's Issues

Disaster is not currently working on all M Mac family

Hi @abougouffa I seen that you are the new maintainer.
I'm trying to run disaster on a M1 Mac and there is no way that I can compile with the current flags.
GCC doesn't support yet the native comp for M chips family, I created a simple patch for this and I would like to create a Pull Request.

Mixed source/assembly on Mac OS X?

Hi there,

somehow I can't get mixed source/assembly on Mac OS X to work, at least not using gobjdump or otool. Is there a 'canonical' way to do this? If not, the following kludge more-or-less works if gdb has been compiled with Python support (v3 in this case, also using use-package to load disaster):

== .gdbinit part

python
import re
class DisassembleLine(gdb.Command):

    """Disassemble around given line specification."""

    def __init__(self):
        super(DisassembleLine, self).__init__(
            "disassemble-line", gdb.COMMAND_USER)

    def address_for_line(self, linespec):
        lineinfo = gdb.execute('info line ' + linespec, to_string=True)
        address_match = re.match('.* starts at address (.*?) ', lineinfo)
        if address_match is None:
            return None
        else:
            return address_match.group(1)

    def invoke(self, arg, from_tty):
        address = self.address_for_line(arg)
        if address is None:
            print("Error: location {0} could not be found".format(arg))
        else:
            print(gdb.execute('disassemble /m ' + address, to_string=True))

DisassembleLine()
end

== elisp part

(use-package disaster
  :config (progn
            (add-c-hook (lambda () (local-set-key (kbd "C-c C-d") 'disaster)))
            (defadvice disaster (before disaster-point-gdb-to-function)
              (setq disaster-objdump
                    (format "gdb --batch -ex \"disassemble-line %s:%s\""
                            (file-name-nondirectory buffer-file-truename)
                            (line-number-at-pos (point))))
            (ad-activate 'disaster)))

If there is a better way, please tell me; if not, maybe this workaround could find its way into the disaster repo for the convenience of my fellow Mac users?
best,
Sebastian

Support compile_commands.json

Disaster looks like a nice tool and the only one of its kind for emacs. However, it seems to only support Make and default compilation. If you are working with a large project that does not use Make which is increasingly common, it seems like there is no way to use it.

compile_commands.json is probably at present the closest thing to a standard way in C++ community to describe the compilation flags for a project. In addition to being used by all of the clang static analyzer tools, it can be generated automatically by both CMake and ninja, as well as from Make using Bear. Furthermore it's the format of choice for other very popular emacs C++ tools like rtags and emacs-ycmd.

Any interest in adding support?

question wrt doom emacs installation instructions

Hi,

I tried adding disaster.el to my doom emacs.

I did the following

In a first try I added only this:

(package! disaster
  :recipe (:host github :repo "jart/disaster"))

to package.el

An ran "doom sync"

I then read the rest of the readme and added

(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))

to config.el and ran "doom sync" again.

C-c d did not work afterwards, but M-x disaster showed some activity. However my cmake based project did not compile.

I then found a disaster.el installed in the doom config:

$:~/.emacs.d$ fdfind -uuu disaster.el ~/.emacs.d/
/home/frank/.emacs.d/.local/straight/build-28.2/disaster/disaster.el
/home/frank/.emacs.d/.local/straight/build-28.2/disaster/disaster.elc
/home/frank/.emacs.d/.local/straight/repos/disaster/disaster.el

Which did not have the date of today. Instead it was from a week (2023-02-07 ) ago. At this date I probably re-installed/re-compiled doom, but was not aware that disaster.el existed. I do not know why it is in my doom setup. I did not enable it knowingly before today (2023-02-12).

Anyway the problem now is that the code installed is an old release of disaster.el:

$ diff -Naur .emacs.d/.local/straight/repos/disaster/disaster.el projects/disaster/disaster.el  | head
--- .emacs.d/.local/straight/repos/disaster/disaster.el	2023-02-07 10:34:37.567169426 +0100
+++ projects/disaster/disaster.el	2023-02-12 10:58:04.009570458 +0100
@@ -1,11 +1,14 @@
-;;; disaster.el --- Disassemble C/C++ code under cursor in Emacs
+;;; disaster.el --- Disassemble C, C++ or Fortran code under cursor -*- lexical-binding: t; -*-
 
-;; Copyright (C) 2013 Justine Tunney.
+;; Copyright (C) 2013-2022 Justine Tunney.
 
 ;; Author: Justine Tunney <[email protected]>
$ git -C ~/.emacs.d/.local/straight/repos/disaster log
commit 10a785facc60d89d78e0d5177985ab1af1741bb4 (grafted, HEAD -> master)
Author: Huang Ying <[email protected]>
Date:   Tue Oct 17 05:52:38 2017 +0800

    Support separate build root directory (#18)
    
    Many build systems support to use a build root directory that is
    different from the project source code root directory.  This patch is
    to support that.

Could you please hint howto fix the situation ? Is disaster.el now a default component of doom emacs?
My doom-emacs n https://github.com/doomemacs/doomemacs is at commit e96624926d724aff98e862221422cd7124a99c19

~/.emacs.d$ rg disaster
bin/doom:68:;;   disasterous side-effects.
modules/lang/cc/packages.el:9:(package! disaster :pin "10a785facc60d89d78e0d5177985ab1af1741bb4")
modules/lang/cc/README.org:36:- [[doom-package:disaster]]
profiles/README.org:43:rollback/snapshot traversal for disaster recovery purposes.
docs/faq.org:92:- No disaster recovery ::

However the old version does not seem to work with cmake well...

Supported file extensions

It checks the file's extension in disaster, but the range of supported extensions is narrow.
According to the regular expression in the code, it contains .c .cpp .cc,
but also contains false-positives like .cp .ccp. I checked these cases with the latest source.
Also it would be great to include extensions .cxx .c++ because cc-mode supports these.

Can disaster be customized?

I want to read flags from a .clang_complete file instead of running make on a Makefile. Can that be done?.

Alternatively, is there a way to define flags for disaster on a project basis?

I want to run default CC of C++ with custom flags like: -std=c++1y O3

Great Package, BTW!

Doesn't fully integrate with autotools build rules

Let's say autotools creates a makefile with this rule:

smash_unittest-lol_test.o: src/lol_test.cc
    # blah blah

Because lol_test.cc is listed in smash_unittest_SOURCES. The problem is that disaster tries to make src/lol_test.o which makes with a generic rule that excludes the per program flags from things like smash_unittest_CPPFLAGS.

This must be fixed.

Symbol's value as variable is void: c-mode-base-map

Warning (initialization): An error occurred while loading `/home/agauniyal/.emacs.d/init.el':

Symbol's value as variable is void: c-mode-base-map

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the `--debug-init' option to view a complete error backtrace.

Support under Mac OS

It looks like objdump is not supported or now replaced as binutils/gobjdump. How to make objdump running under Mac OS like the demo does? thx

integration with cmake-ide

Hello,

I'm using cmake-ide plugin to build my project.
Is there any way to configure disaster to use compilation commands from cmake-ide and ask cmake-ide to compile files?

Couldn’t find corresponding assembly line

Running M-x disaster keeps printing the same in all C source files (haven't tried others).

It's using the default objdump command:

objdump -d -M att -Sl --no-show-raw-insn

I tried changing mode to nasm, tried changing the objdump command a bit, but nothing.

Those gray comments that appear on the screenshots are not generated for me, only headers for functions:

image

This is the message log:

Running: make -k -C <PATH>/file.o
make: Entering directory '<PATH>'
cc    -c -o file.o file.c
make: Leaving directory '<PATH>'
Running: objdump -d -M att -Sl --no-show-raw-insn <PATH>/file.o
Couldn’t find corresponding assembly line.

Support different architecture

It would be great to add support for others architecture, such as arm for instance.
One could imagine a setting for the prefix, (and an easy way to toggle from one to another) to call the appropriate version of objdump.

Support C++11

By passing -std=c++11 to c++. I doubt this will break much, and many people are working with C++11 now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.