sbz's blog

Technical writings and recipes on UNIX, software and systems

Python bindings for udis86

Posted at — Aug 21, 2010

I’ve just released an udis86 python binding on pypi named pyudis86.

For now, it just wrap udis86 public APIs (the functions in libudis86/udis86.c file) but I project to also add some operand stuff and input hook part.

The result is available in an example file here:

import udis86
import sys

def main():
   ud_obj = udis86.init()
   ud_obj.set_input_file(sys.stdin)
   ud_obj.set_mode(udis86.MOD_64)
   ud_obj.set_pc(0)
   ud_obj.set_syntax(udis86.UD_SYN_INTEL)

   while True:
       ud_obj.disassemble()
       print "%016d %-16s %-24s" % (
           ud_obj.insn_off(), ud_obj.insn_hex(), ud_obj.insn_asm()
       )

if __name__ == '__main__':
   try:
       main()
   except KeyboardInterrupt, e:
       sys.exit(-1)

For the equivalent code in C (available on the website) which is:

#include <stdio.h>
#include <udis86.h>

int main(void) {
   ud_t ud_obj;

   ud_init(&ud_obj);
   ud_set_input_file(&ud_obj, stdin);
   ud_set_mode(&ud_obj, 64);
   ud_set_syntax(&ud_obj, UD_SYN_INTEL);

   while (ud_disassemble(&ud_obj)) {
       printf("\t%s\n", ud_insn_asm(&ud_obj));
   }

   return 0;

}

The trunk repository is on my github in project pyudis86. Do not hesitate to look it up!