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 "verbstat.h"
8#include "simpletimer.h"
9#include "methodcontext.h"
10#include "methodcontextiterator.h"
11#include "errorhandling.h"
12
13int verbStat::DoWork(const char* nameOfInput, const char* nameOfOutput, int indexCount, const int* indexes)
14{
15 LogVerbose("Stat'ing from '%s' and writing output into '%s'", nameOfInput, nameOfOutput);
16
17 MethodContextIterator mci(indexCount, indexes, true);
18 if (!mci.Initialize(nameOfInput))
19 return -1;
20
21 int savedCount = 0;
22
23 HANDLE hFileOut = CreateFileA(nameOfOutput, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
24 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
25 if (hFileOut == INVALID_HANDLE_VALUE)
26 {
27 LogError("Failed to open input 1 '%s'. GetLastError()=%u", nameOfOutput, GetLastError());
28 return -1;
29 }
30
31#define bufflen 50000
32 DWORD bytesWritten;
33 char buff[bufflen];
34 int offset = 0;
35 ZeroMemory(&buff[0], bufflen);
36 offset += sprintf_s(buff, bufflen, "Title,MC#,");
37 offset += MethodContext::dumpStatTitleToBuffer(&buff[offset], bufflen - offset);
38 buff[offset++] = 0x0d;
39 buff[offset++] = 0x0a;
40 WriteFile(hFileOut, &buff[0], offset, &bytesWritten, nullptr);
41
42 while (mci.MoveNext())
43 {
44 MethodContext* mc = mci.Current();
45
46 offset = 0;
47 ZeroMemory(&buff[0], bufflen);
48 if ((mc->cr->ProcessName != nullptr) && (mc->cr->ProcessName->GetCount() > 0))
49 {
50 const char* procname = mc->cr->repProcessName();
51 strcpy_s(&buff[offset], bufflen, procname);
52 offset += (int)strlen(procname);
53 }
54 buff[offset++] = ',';
55 offset += sprintf_s(&buff[offset], bufflen - offset, "%d,", mci.MethodContextNumber());
56 offset += mc->dumpStatToBuffer(&buff[offset], bufflen - offset);
57 buff[offset++] = 0x0d;
58 buff[offset++] = 0x0a;
59 WriteFile(hFileOut, &buff[0], offset, &bytesWritten, nullptr);
60 savedCount++;
61 }
62
63 if (!CloseHandle(hFileOut))
64 {
65 LogError("2nd CloseHandle failed. GetLastError()=%u", GetLastError());
66 return -1;
67 }
68
69 LogInfo("Loaded %d, Stat'd %d", mci.MethodContextNumber(), savedCount);
70
71 if (!mci.Destroy())
72 return -1;
73
74 return 0;
75}
76