Git Product home page Git Product logo

disassemble's Introduction

Build Status

A disassembler for JITed functions in Racket.

To install:

% raco pkg install disassemble

To use it, try something like this:

[samth@punge:~/sw/disassemble (master) plt] racket
Welcome to Racket v6.0.1.10.
> (require disassemble)
> (define (f x) 1)
> (disassemble f)
       0: 488943f8                       (mov (mem64+ rbx #x-8) rax)
       4: 4883c3f8                       (add rbx #xfffffffffffffff8)
       8: b803000000                     (mov eax #x3)
       d: 4c8b75c8                       (mov r14 (mem64+ rbp #x-38))
      11: 4883c428                       (add rsp #x28)
      15: 415d                           (pop r13)
      17: 415c                           (pop r12)
      19: 5b                             (pop rbx)
      1a: 5d                             (pop rbp)
      1b: c3                             (ret)
>

If you have ndisasm installed (and in your PATH) you can also try:

> (disassemble f #:program 'nasm)
00000000  488943F8          mov [rbx-0x8],rax
00000004  4883C3F8          add rbx,byte -0x8
00000008  B803000000        mov eax,0x3
0000000D  4C8B75C8          mov r14,[rbp-0x38]
00000011  4883C428          add rsp,byte +0x28
00000015  415D              pop r13
00000017  415C              pop r12
00000019  5B                pop rbx
0000001A  5D                pop rbp
0000001B  C3                ret

This works only on x86 or x86-64.

Also, the dump function writes the bytes of the machine code to a file:

> (dump const "file.bin")

Patches, uses, complaints, and suggestions are all welcome.

The disassembly code (when not using NASM) is taken from Göran Weinholt's Machine Code library.

disassemble's People

Contributors

filonenko-mikhail avatar jbclements avatar licentious avatar lopezca avatar mflatt avatar rjnw avatar samth avatar sorawee avatar stamourv avatar takikawa 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

disassemble's Issues

Internal error on `map`

Example:

-> (require disassemble)
-> (disassemble map)
; error: contract violation
;   expected: (or/c string? symbol?)
;   given: '#(map
;     #<path:/home/asumu/plt/racket-git/racket/collects/racket/private/map.rkt>
;     21 13 588 924 #f)
;   argument position: 1st
; [,bt for context]

Can't disassemble

$ racket
Welcome to Racket v8.5.0.7 [cs].
> (require disassemble my-lib)
> (disassemble a-proc-of-my-lib)
inspector-object: invalid message 'code to object type 'record [,bt for context]
> ,bt
inspector-object: invalid message 'code to object type 'record
  context...:
   /usr/local/google/home/lorseau/.local/share/racket/snapshot/pkgs/disassemble/disassemble/main.rkt:142:5: body of top-level
   /usr/local/google/home/lorseau/.local/share/racket/snapshot/pkgs/disassemble/disassemble/main.rkt:193:0: disassemble
   /usr/share/racket-8.5.0.7/pkgs/xrepl-lib/xrepl/xrepl.rkt:1573:0
   /usr/share/racket-8.5.0.7/collects/racket/repl.rkt:11:26
> 

The same happens whether I remove zos or recompile beforehand.

The basic example of the docs does work though.

Typed Racket support

Hi! Thank you very much for this library, it is extremely interesting.

One thing I tried out is checking how the emitted assembly changes under gradual typing. But I could not find in the documentation how to work with Typed Racket; I also tried racket/contract.

My attempt with Typed Racket on the REPL:

$ racket -I typed/racket
Welcome to Racket v7.1.
> (: my-square (-> Number Number))
> (define (my-square x) (* x x))
> (require disassemble)
> (disassemble my-square)
; readline-input:7:0: Type Checker: missing type for identifier;
;  consider using `require/typed' to import it
;   identifier: disassemble
;   from module: disassemble
;   in: (disassemble my-square)
; [,bt for context]

> (require/typed disassemble [disassemble (-> Any Any)])
> (disassemble my-square)
; disassemble: contract violation
;   expected: non-primitive procedure
;   given: #<procedure:my-square>
; [,bt for context]
> ^D

And here is my attempt to use disassemble on an untyped Racket procedure that has a contract:

$ racket
Welcome to Racket v7.1.
> (require disassemble)
> (define (my-square x)
    (* x x))
> (disassemble my-square)
;; ... elided: it works ...

> (define/contract (real-square x)
    (-> real? real?)
    (* x x))
> (disassemble real-square)
; disassemble: contract violation
;   expected: non-primitive procedure
;   given: #<procedure:real-square>
; [,bt for context]
> ^D

Is this possible, but undocumented? Or is it just not doable?

Thanks again!

Strange results from disassemble for fib

With racket 7.0, disassemble for fib:

(define (fib n)
  (cond
    [(<= n 1) n]
    [else (fib (+ n 1) (+ n 2))]))

returns the following partial assembly code:

      2f: 48bed749bc2e567f0000           (mov rsi #x7f562ebc49d7)
      39: 488975d8                       (mov (mem64+ rbp #x-28) rsi)
      3d: 48bee049bc2e567f0000           (mov rsi #x7f562ebc49e0)

With nasm it shows up as:

0000002F  48BE574EBC2E567F  mov rsi,0x7f562ebc4e57
         -0000
00000039  488975D8          mov [rbp-0x28],rsi
0000003D  48BE604EBC2E567F  mov rsi,0x7f562ebc4e60
         -0000

I am confused about what those -0000 are and they look like some parsing bug of sorts.

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.