1 : // Copyright 2012 Google Inc.
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_util.h"
20 : #include "base/stringprintf.h"
21 : #include "base/utf_string_conversions.h"
22 : #include "base/win/scoped_handle.h"
23 : #include "syzygy/core/unittest_util.h"
24 : #include "syzygy/trace/protocol/call_trace_defs.h"
25 :
26 : namespace testing {
27 :
28 : CallTraceService::CallTraceService()
29 : : instance_id_(base::StringPrintf("%d", ::GetCurrentProcessId())),
30 E : service_process_(base::kNullProcessHandle) {
31 E : }
32 :
33 E : CallTraceService::~CallTraceService() {
34 E : Stop();
35 E : }
36 :
37 E : void CallTraceService::Start(const FilePath& trace_dir) {
38 E : ASSERT_EQ(base::kNullProcessHandle, service_process_);
39 :
40 : CommandLine service_cmd(
41 E : testing::GetExeRelativePath(L"call_trace_service.exe"));
42 E : service_cmd.AppendArg("start");
43 E : service_cmd.AppendSwitch("--verbose");
44 E : service_cmd.AppendSwitchPath("--trace-dir", trace_dir);
45 E : service_cmd.AppendSwitchASCII("--instance-id", instance_id_);
46 :
47 E : base::LaunchOptions options;
48 E : options.start_hidden = true;
49 :
50 E : std::wstring event_name;
51 : ::GetSyzygyCallTraceRpcEventName(UTF8ToUTF16(instance_id_),
52 E : &event_name);
53 : base::win::ScopedHandle event(
54 E : ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()));
55 E : ASSERT_TRUE(event.IsValid());
56 :
57 E : ASSERT_TRUE(base::LaunchProcess(service_cmd, options, &service_process_));
58 E : ASSERT_NE(base::kNullProcessHandle, service_process_);
59 :
60 : // We wait on both the "ready" handle and the process, as if the process
61 : // fails for any reason, it'll exit and its handle will become signalled.
62 E : HANDLE handles[] = { event.Get(), service_process_ };
63 : ASSERT_EQ(WAIT_OBJECT_0, ::WaitForMultipleObjects(arraysize(handles),
64 : handles,
65 : FALSE,
66 E : INFINITE));
67 E : }
68 :
69 E : void CallTraceService::Stop() {
70 E : if (service_process_ == base::kNullProcessHandle)
71 E : return;
72 :
73 : CommandLine service_cmd(
74 E : testing::GetExeRelativePath(L"call_trace_service.exe"));
75 E : service_cmd.AppendArg("stop");
76 E : service_cmd.AppendSwitchASCII("--instance-id", instance_id_);
77 :
78 E : base::LaunchOptions options;
79 E : options.start_hidden = true;
80 E : options.wait = true;
81 E : ASSERT_TRUE(base::LaunchProcess(service_cmd, options, NULL));
82 :
83 E : int exit_code = 0;
84 E : ASSERT_TRUE(base::WaitForExitCode(service_process_, &exit_code));
85 E : service_process_ = base::kNullProcessHandle;
86 E : }
87 :
88 E : void CallTraceService::SetEnvironment() {
89 : // The instance id needs to be in the environment to be picked up by the
90 : // client library.
91 E : scoped_ptr<base::Environment> env(base::Environment::Create());
92 E : ASSERT_FALSE(env.get() == NULL);
93 E : ASSERT_TRUE(env->SetVar(::kSyzygyRpcInstanceIdEnvVar, instance_id_));
94 E : }
95 :
96 : } // namespace testing
|