1 : // Copyright 2014 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/agent/memprof/parameters.h"
16 :
17 : #include "base/command_line.h"
18 : #include "base/environment.h"
19 : #include "base/logging.h"
20 : #include "base/strings/utf_string_conversions.h"
21 :
22 : namespace agent {
23 : namespace memprof {
24 :
25 : // An array mapping StackTraceTracking values to strings.
26 : const char* kStackTraceTrackingValues[] = {
27 : "none", "track", "emit",
28 : };
29 : static_assert(arraysize(kStackTraceTrackingValues) == kStackTraceTrackingMax,
30 : "Stack trace tracking values out of sync.");
31 :
32 : // The environment variable that is used for extracting parameters.
33 : const char kParametersEnvVar[] = "SYZYGY_MEMPROF_OPTIONS";
34 :
35 : // Default parameter values.
36 : StackTraceTracking kDefaultStackTraceTracking = kTrackingNone;
37 : bool kDefaultSerializeTimestamps = false;
38 : bool kDefaultHashContentsAtFree = false;
39 :
40 : // Parameter names for parsing.
41 : const char kParamStackTraceTracking[] = "stack-trace-tracking";
42 : const char kParamSerializeTimestamps[] = "serialize-timestamps";
43 : const char kParamHashContentsAtFree[] = "hash-contents-at-free";
44 :
45 E : void SetDefaultParameters(Parameters* parameters) {
46 E : DCHECK_NE(static_cast<Parameters*>(nullptr), parameters);
47 E : parameters->stack_trace_tracking = kDefaultStackTraceTracking;
48 E : parameters->serialize_timestamps = false;
49 E : parameters->hash_contents_at_free = false;
50 E : }
51 :
52 : bool ParseParameters(const base::StringPiece& param_string,
53 E : Parameters* parameters) {
54 E : DCHECK_NE(static_cast<Parameters*>(nullptr), parameters);
55 :
56 : // Prepends the flags with a dummy executable name to keep the
57 : // base::CommandLine parser happy.
58 E : std::wstring str = base::UTF8ToWide(param_string);
59 E : str.insert(0, L" ");
60 E : str.insert(0, L"dummy.exe");
61 E : base::CommandLine cmd_line = base::CommandLine::FromString(str);
62 :
63 E : bool success = true;
64 :
65 : // Parse the stack trace tracking enum.
66 E : std::string value = cmd_line.GetSwitchValueASCII(kParamStackTraceTracking);
67 E : if (!value.empty()) {
68 E : bool parsed = false;
69 E : for (size_t i = 0; i < kStackTraceTrackingMax; ++i) {
70 E : if (value == kStackTraceTrackingValues[i]) {
71 E : parsed = true;
72 E : parameters->stack_trace_tracking = static_cast<StackTraceTracking>(i);
73 E : break;
74 : }
75 E : }
76 E : if (!parsed) {
77 E : LOG(ERROR) << "Unknown value for --" << kParamStackTraceTracking
78 : << ": " << value;
79 E : success = false;
80 : }
81 : }
82 :
83 E : if (cmd_line.HasSwitch(kParamSerializeTimestamps))
84 E : parameters->serialize_timestamps = true;
85 :
86 E : if (cmd_line.HasSwitch(kParamHashContentsAtFree))
87 E : parameters->hash_contents_at_free = true;
88 :
89 E : return success;
90 E : }
91 :
92 E : bool ParseParametersFromEnv(Parameters* parameters) {
93 E : DCHECK_NE(static_cast<Parameters*>(nullptr), parameters);
94 :
95 E : scoped_ptr<base::Environment> env(base::Environment::Create());
96 E : DCHECK_NE(static_cast<base::Environment*>(nullptr), env.get());
97 :
98 E : std::string value;
99 E : if (!env->GetVar(kParametersEnvVar, &value))
100 E : return true;
101 :
102 E : if (!ParseParameters(value, parameters))
103 E : return false;
104 :
105 E : return true;
106 E : }
107 :
108 : } // namespace memprof
109 : } // namespace agent
|