| 1 | // | 
| 2 | // Copyright (c) Microsoft. All rights reserved. | 
| 3 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. | 
| 4 | // | 
| 5 |  | 
| 6 | #include "standardpch.h" | 
| 7 | #include "verbasmdump.h" | 
| 8 | #include "simpletimer.h" | 
| 9 | #include "methodcontext.h" | 
| 10 | #include "methodcontextiterator.h" | 
| 11 | #include "asmdumper.h" | 
| 12 | #include "errorhandling.h" | 
| 13 |  | 
| 14 | #define BUFFER_SIZE 0xFFFFFF | 
| 15 |  | 
| 16 | int verbASMDump::DoWork(const char* nameOfInput, const char* nameOfOutput, int indexCount, const int* indexes) | 
| 17 | { | 
| 18 |     LogVerbose("Loading from '%s' and writing ASM output into '%s-MC#.asm'" , nameOfInput, nameOfOutput); | 
| 19 |  | 
| 20 |     MethodContextIterator mci(indexCount, indexes, true); | 
| 21 |     if (!mci.Initialize(nameOfInput)) | 
| 22 |         return -1; | 
| 23 |  | 
| 24 |     int savedCount = 0; | 
| 25 |  | 
| 26 |     while (mci.MoveNext()) | 
| 27 |     { | 
| 28 |         MethodContext* mc = mci.Current(); | 
| 29 |  | 
| 30 |         char buff[500]; | 
| 31 |         sprintf_s(buff, 500, "%s-%d.asm" , nameOfOutput, mci.MethodContextNumber()); | 
| 32 |  | 
| 33 |         HANDLE hFileOut = CreateFileA(buff, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, | 
| 34 |                                       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); | 
| 35 |         if (hFileOut == INVALID_HANDLE_VALUE) | 
| 36 |         { | 
| 37 |             LogError("Failed to open output '%s'. GetLastError()=%u" , buff, GetLastError()); | 
| 38 |             return -1; | 
| 39 |         } | 
| 40 |  | 
| 41 |         if (mc->cr->IsEmpty()) | 
| 42 |         { | 
| 43 |             const size_t bufflen = 4096; | 
| 44 |             DWORD        bytesWritten; | 
| 45 |             char         buff[bufflen]; | 
| 46 |             ZeroMemory(buff, bufflen * sizeof(char)); | 
| 47 |             int buff_offset = sprintf_s(buff, bufflen, ";;Method context has no compile result" ); | 
| 48 |             WriteFile(hFileOut, buff, buff_offset * sizeof(char), &bytesWritten, nullptr); | 
| 49 |         } | 
| 50 |         else | 
| 51 |         { | 
| 52 |             ASMDumper::DumpToFile(hFileOut, mc, mc->cr); | 
| 53 |         } | 
| 54 |  | 
| 55 |         if (!CloseHandle(hFileOut)) | 
| 56 |         { | 
| 57 |             LogError("CloseHandle failed. GetLastError()=%u" , GetLastError()); | 
| 58 |             return -1; | 
| 59 |         } | 
| 60 |         savedCount++; | 
| 61 |     } | 
| 62 |  | 
| 63 |     LogInfo("Asm'd %d" , savedCount); | 
| 64 |  | 
| 65 |     if (!mci.Destroy()) | 
| 66 |         return -1; | 
| 67 |  | 
| 68 |     return 0; | 
| 69 | } | 
| 70 |  |