1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/trace/common/unittest_util.h"
16 :
17 : #include "base/command_line.h"
18 : #include "base/environment.h"
19 : #include "base/process/kill.h"
20 : #include "base/process/launch.h"
21 : #include "base/strings/stringprintf.h"
22 : #include "base/strings/utf_string_conversions.h"
23 : #include "base/win/scoped_handle.h"
24 : #include "syzygy/common/align.h"
25 : #include "syzygy/common/buffer_writer.h"
26 : #include "syzygy/core/unittest_util.h"
27 : #include "syzygy/trace/protocol/call_trace_defs.h"
28 :
29 : namespace testing {
30 :
31 : CallTraceService::CallTraceService()
32 : : instance_id_(base::StringPrintf("%d", ::GetCurrentProcessId())),
33 E : service_process_(base::kNullProcessHandle) {
34 E : }
35 :
36 E : CallTraceService::~CallTraceService() {
37 E : Stop();
38 E : }
39 :
40 E : void CallTraceService::Start(const base::FilePath& trace_dir) {
41 E : ASSERT_EQ(base::kNullProcessHandle, service_process_);
42 :
43 : CommandLine service_cmd(
44 E : testing::GetExeRelativePath(L"call_trace_service.exe"));
45 E : service_cmd.AppendArg("start");
46 E : service_cmd.AppendSwitch("--verbose");
47 E : service_cmd.AppendSwitchPath("--trace-dir", trace_dir);
48 E : service_cmd.AppendSwitchASCII("--instance-id", instance_id_);
49 :
50 E : base::LaunchOptions options;
51 E : options.start_hidden = true;
52 :
53 E : std::wstring event_name;
54 : ::GetSyzygyCallTraceRpcEventName(base::UTF8ToUTF16(instance_id_),
55 E : &event_name);
56 : base::win::ScopedHandle event(
57 E : ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()));
58 E : ASSERT_TRUE(event.IsValid());
59 :
60 E : ASSERT_TRUE(base::LaunchProcess(service_cmd, options, &service_process_));
61 E : ASSERT_NE(base::kNullProcessHandle, service_process_);
62 :
63 : // We wait on both the "ready" handle and the process, as if the process
64 : // fails for any reason, it'll exit and its handle will become signaled.
65 E : HANDLE handles[] = { event.Get(), service_process_ };
66 : ASSERT_EQ(WAIT_OBJECT_0, ::WaitForMultipleObjects(arraysize(handles),
67 : handles,
68 : FALSE,
69 E : INFINITE));
70 E : }
71 :
72 E : void CallTraceService::Stop() {
73 E : if (service_process_ == base::kNullProcessHandle)
74 E : return;
75 :
76 : CommandLine service_cmd(
77 E : testing::GetExeRelativePath(L"call_trace_service.exe"));
78 E : service_cmd.AppendArg("stop");
79 E : service_cmd.AppendSwitchASCII("--instance-id", instance_id_);
80 :
81 E : base::LaunchOptions options;
82 E : options.start_hidden = true;
83 E : options.wait = true;
84 E : ASSERT_TRUE(base::LaunchProcess(service_cmd, options, NULL));
85 :
86 E : int exit_code = 0;
87 E : ASSERT_TRUE(base::WaitForExitCode(service_process_, &exit_code));
88 E : service_process_ = base::kNullProcessHandle;
89 E : }
90 :
91 E : void CallTraceService::SetEnvironment() {
92 : // The instance id needs to be in the environment to be picked up by the
93 : // client library.
94 E : scoped_ptr<base::Environment> env(base::Environment::Create());
95 E : ASSERT_FALSE(env.get() == NULL);
96 :
97 : // Get the existing value.
98 E : std::string env_var;
99 E : env->GetVar(::kSyzygyRpcInstanceIdEnvVar, &env_var);
100 :
101 : // Prefix the existing environment variable with the instance ID we've
102 : // chosen. This allows previously intended behaviour to persist.
103 E : env_var.insert(0, ";");
104 E : env_var.insert(0, instance_id_);
105 :
106 E : ASSERT_TRUE(env->SetVar(::kSyzygyRpcInstanceIdEnvVar, env_var));
107 E : }
108 :
109 : void WriteRecord(uint64 timestamp,
110 : uint16 record_type,
111 : const void* data,
112 : size_t length,
113 E : trace::service::TraceFileWriter* writer) {
114 E : ASSERT_TRUE(data != NULL);
115 E : ASSERT_TRUE(writer != NULL);
116 :
117 E : std::vector<uint8> buffer;
118 E : ::common::VectorBufferWriter buffer_writer(&buffer);
119 :
120 E : RecordPrefix record = {};
121 E : record.timestamp = timestamp;
122 E : record.type = TraceFileSegmentHeader::kTypeId;
123 E : record.size = sizeof(TraceFileSegmentHeader);
124 E : record.version.hi = TRACE_VERSION_HI;
125 E : record.version.lo = TRACE_VERSION_LO;
126 E : ASSERT_TRUE(buffer_writer.Write(record));
127 :
128 E : TraceFileSegmentHeader header = {};
129 E : header.segment_length = sizeof(RecordPrefix) + length;
130 E : header.thread_id = ::GetCurrentThreadId();
131 E : ASSERT_TRUE(buffer_writer.Write(header));
132 :
133 E : record.type = record_type;
134 E : record.size = length;
135 E : ASSERT_TRUE(buffer_writer.Write(record));
136 :
137 : ASSERT_TRUE(buffer_writer.Write(
138 E : length, reinterpret_cast<const void*>(data)));
139 :
140 E : buffer.resize(::common::AlignUp(buffer.size(), writer->block_size()));
141 E : ASSERT_TRUE(writer->WriteRecord(buffer.data(), buffer.size()));
142 E : }
143 :
144 : } // namespace testing
|