-1

I want to run objdump -d [sample] command in my program and then I want to shows the output of it in a formated version. For example I want to show its output with one tab in each line of its output. How can I do that?

 cmd1 = "objdump -d "
 name = "sample"
 output = os.system(str(cmd1) + str(name))
 print(output)
  • `os.system` doesn't return the "output". It returns the integer return code. So, for starters, bad choice of call. Check the documentation on it. – MariusSiuram Jul 18 '17 at 08:43

1 Answers1

1

You can use subprocess.check_output:

In [864]: output = subprocess.check_output(['objdump -d a.out'], shell=True)

This returns a byte string of your output. You may then use str.decode to display your data. If you want to indent with a tab, you can just split on newline and then print line by line, like this:

In [870]: for line in output.decode().split('\n'):
     ...:     print('\t', line)
     ...:     

     a.out: file format Mach-O 64-bit x86-64

     Disassembly of section __TEXT,__text:
     __text:
     100000fa0: 55  pushq   %rbp
     100000fa1: 48 89 e5    movq    %rsp, %rbp
     100000fa4: 31 c0   xorl    %eax, %eax
     100000fa6: c7 45 fc 00 00 00 00    movl    $0, -4(%rbp)
     100000fad: 5d  popq    %rbp
     100000fae: c3  retq

     _main:
     100000fa0: 55  pushq   %rbp
     100000fa1: 48 89 e5    movq    %rsp, %rbp
     100000fa4: 31 c0   xorl    %eax, %eax
     100000fa6: c7 45 fc 00 00 00 00    movl    $0, -4(%rbp)
     100000fad: 5d  popq    %rbp
     100000fae: c3  retq
cs95
  • 379,657
  • 97
  • 704
  • 746