| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> |
| 6 #include <stdio.h> |
| 7 #include <stdlib.h> |
| 8 #include <unistd.h> |
| 9 |
| 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. | 10 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. |
| 6 #if defined(TARGET_ARCH_X64) | 11 #if defined(TARGET_ARCH_X64) |
| 7 | 12 |
| 8 #include "vm/disassembler.h" | 13 #include "vm/disassembler.h" |
| 9 | 14 |
| 10 #include "vm/assert.h" | 15 #include "vm/assert.h" |
| 11 | 16 |
| 12 namespace dart { | 17 namespace dart { |
| 13 | 18 |
| 19 void Disassembler::Disassemble(uword start, |
| 20 uword end, |
| 21 DisassemblyFormatter* formatter) { |
| 22 // First print the actual addresses so that we know where in memory this is |
| 23 // being disassembled from. |
| 24 formatter->Print("start: %p end: %p\n", start, end); |
| 25 |
| 26 // Write code block to tmp file. |
| 27 char tmp[] = "/tmp/codeblock.XXXXXX"; |
| 28 int fd = mkstemp(tmp); |
| 29 if (fd < 0) { |
| 30 int errsv = errno; |
| 31 formatter->Print("Could not open tmp file %s, errno=%s\n", |
| 32 tmp, |
| 33 strerror(errsv)); |
| 34 return; // failed |
| 35 } |
| 36 ssize_t size = write(fd, reinterpret_cast<const void*>(start), end - start); |
| 37 if (size <= 0) { |
| 38 if (size < 0) { |
| 39 int errsv = errno; |
| 40 formatter->Print("Could not write to tmp file %s, errno=%s\n", |
| 41 tmp, |
| 42 strerror(errsv)); |
| 43 } |
| 44 close(fd); |
| 45 remove(tmp); |
| 46 return; |
| 47 } |
| 48 close(fd); |
| 49 |
| 50 // Disassemble tmp file to stdout. |
| 51 char cmd[256]; |
| 52 #if defined(__APPLE__) |
| 53 snprintf(cmd, sizeof(cmd), |
| 54 "( cat %1$s | " |
| 55 " hexdump -v -e '\".byte \" 1/1 \"0x%%02x\" \"\n\"' | " |
| 56 " as - -arch x86_64 -o %1$s.o ; otool -tV %1$s.o" |
| 57 ") </dev/null 2>&1", tmp); |
| 58 #else |
| 59 snprintf(cmd, sizeof(cmd), "( /usr/bin/objdump -b binary -m i386:x86-64 -D %s" |
| 60 " ) </dev/null 2>&1", tmp); |
| 61 #endif |
| 62 FILE* output = popen(cmd, "r"); |
| 63 if (output == NULL) { |
| 64 int errsv = errno; |
| 65 formatter->Print("Could not run \"%s\", errno=%s\n", cmd, strerror(errsv)); |
| 66 remove(tmp); |
| 67 return; // failed |
| 68 } |
| 69 const int kMaxOutputLine = 1024; |
| 70 char line[kMaxOutputLine]; |
| 71 #if defined(__APPLE__) |
| 72 const char* header = "(__TEXT,__text) section\n"; |
| 73 #else |
| 74 const char* header = "<.data>:\n"; |
| 75 #endif |
| 76 char* header_pos = NULL; |
| 77 while (header_pos == NULL && fgets(line, sizeof(line), output) != NULL) { |
| 78 header_pos = strstr(line, header); |
| 79 if (header_pos != NULL) { |
| 80 formatter->Print("%s", header_pos + strlen(header)); |
| 81 } |
| 82 } |
| 83 while (fgets(line, sizeof(line), output) != NULL) { |
| 84 formatter->Print("%s", line); |
| 85 } |
| 86 pclose(output); |
| 87 |
| 88 // Delete tmp files. |
| 89 remove(tmp); |
| 90 #if defined(__APPLE__) |
| 91 char tmp_o[32]; |
| 92 snprintf(tmp_o, sizeof(tmp_o), "%s.o", tmp); |
| 93 remove(tmp_o); |
| 94 #endif |
| 95 } |
| 96 |
| 97 |
| 14 int Disassembler::DecodeInstruction(char* hexa_buffer, intptr_t hexa_size, | 98 int Disassembler::DecodeInstruction(char* hexa_buffer, intptr_t hexa_size, |
| 15 char* human_buffer, intptr_t human_size, | 99 char* human_buffer, intptr_t human_size, |
| 16 uword pc) { | 100 uword pc) { |
| 17 UNIMPLEMENTED(); | 101 UNIMPLEMENTED(); |
| 18 return 0; | 102 return 0; |
| 19 } | 103 } |
| 20 | 104 |
| 21 | |
| 22 const char* Disassembler::RegisterName(Register reg) { | |
| 23 UNIMPLEMENTED(); | |
| 24 return NULL; | |
| 25 } | |
| 26 | |
| 27 } // namespace dart | 105 } // namespace dart |
| 28 | 106 |
| 29 #endif // defined TARGET_ARCH_X64 | 107 #endif // defined TARGET_ARCH_X64 |
| OLD | NEW |