Coverage for /Syzygy/trace/common/unittest_util.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%70700.C++test

Line-by-line coverage:

   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_TRUE(!service_process_.IsValid());
  42    :  
  43    :    base::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 :    service_process_ = base::LaunchProcess(service_cmd, options);
  61  E :    ASSERT_TRUE(service_process_.IsValid());
  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_.Handle()};
  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_.IsValid())
  74  E :      return;
  75    :  
  76    :    base::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 :    base::Process stop_process = base::LaunchProcess(service_cmd, options);
  85  E :    ASSERT_TRUE(stop_process.IsValid());
  86    :  
  87  E :    int exit_code = 0;
  88  E :    ASSERT_TRUE(service_process_.WaitForExit(&exit_code));
  89  E :    service_process_.Close();
  90  E :  }
  91    :  
  92  E :  void CallTraceService::SetEnvironment() {
  93    :    // The instance id needs to be in the environment to be picked up by the
  94    :    // client library.
  95  E :    scoped_ptr<base::Environment> env(base::Environment::Create());
  96  E :    ASSERT_FALSE(env.get() == NULL);
  97    :  
  98    :    // Get the existing value.
  99  E :    std::string env_var;
 100  E :    env->GetVar(::kSyzygyRpcInstanceIdEnvVar, &env_var);
 101    :  
 102    :    // Prefix the existing environment variable with the instance ID we've
 103    :    // chosen. This allows previously intended behaviour to persist.
 104  E :    env_var.insert(0, ";");
 105  E :    env_var.insert(0, instance_id_);
 106    :  
 107  E :    ASSERT_TRUE(env->SetVar(::kSyzygyRpcInstanceIdEnvVar, env_var));
 108  E :  }
 109    :  
 110    :  void WriteRecord(uint64 timestamp,
 111    :                   uint16 record_type,
 112    :                   const void* data,
 113    :                   size_t length,
 114  E :                   trace::service::TraceFileWriter* writer) {
 115  E :    ASSERT_TRUE(data != NULL);
 116  E :    ASSERT_TRUE(writer != NULL);
 117    :  
 118  E :    std::vector<uint8> buffer;
 119  E :    ::common::VectorBufferWriter buffer_writer(&buffer);
 120    :  
 121  E :    RecordPrefix record = {};
 122  E :    record.timestamp = timestamp;
 123  E :    record.type = TraceFileSegmentHeader::kTypeId;
 124  E :    record.size = sizeof(TraceFileSegmentHeader);
 125  E :    record.version.hi = TRACE_VERSION_HI;
 126  E :    record.version.lo = TRACE_VERSION_LO;
 127  E :    ASSERT_TRUE(buffer_writer.Write(record));
 128    :  
 129  E :    TraceFileSegmentHeader header = {};
 130  E :    header.segment_length = sizeof(RecordPrefix) + length;
 131  E :    header.thread_id = ::GetCurrentThreadId();
 132  E :    ASSERT_TRUE(buffer_writer.Write(header));
 133    :  
 134  E :    record.type = record_type;
 135  E :    record.size = length;
 136  E :    ASSERT_TRUE(buffer_writer.Write(record));
 137    :  
 138    :    ASSERT_TRUE(buffer_writer.Write(
 139  E :        length, reinterpret_cast<const void*>(data)));
 140    :  
 141  E :    buffer.resize(::common::AlignUp(buffer.size(), writer->block_size()));
 142  E :    ASSERT_TRUE(writer->WriteRecord(buffer.data(), buffer.size()));
 143  E :  }
 144    :  
 145    :  }  // namespace testing

Coverage information generated Thu Jan 14 17:40:38 2016.