Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: third_party/crashpad/crashpad/minidump/minidump_misc_info_writer.cc

Issue 1505213004: Copy Crashpad into the Chrome tree instead of importing it via DEPS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. 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 "minidump/minidump_misc_info_writer.h"
16
17 #include <limits>
18
19 #include "base/logging.h"
20 #include "base/numerics/safe_conversions.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "build/build_config.h"
24 #include "minidump/minidump_writer_util.h"
25 #include "package.h"
26 #include "snapshot/process_snapshot.h"
27 #include "snapshot/system_snapshot.h"
28 #include "util/file/file_writer.h"
29 #include "util/numeric/in_range_cast.h"
30 #include "util/numeric/safe_assignment.h"
31
32 #if defined(OS_MACOSX)
33 #include <AvailabilityMacros.h>
34 #endif
35
36 namespace crashpad {
37 namespace {
38
39 uint32_t TimevalToRoundedSeconds(const timeval& tv) {
40 uint32_t seconds =
41 InRangeCast<uint32_t>(tv.tv_sec, std::numeric_limits<uint32_t>::max());
42 const int kMicrosecondsPerSecond = static_cast<int>(1E6);
43 if (tv.tv_usec >= kMicrosecondsPerSecond / 2 &&
44 seconds != std::numeric_limits<uint32_t>::max()) {
45 ++seconds;
46 }
47 return seconds;
48 }
49
50 // For MINIDUMP_MISC_INFO_4::BuildString. dbghelp only places OS version
51 // information here, but if a machine description is also available, this is the
52 // only reasonable place in a minidump file to put it.
53 std::string BuildString(const SystemSnapshot* system_snapshot) {
54 std::string os_version_full = system_snapshot->OSVersionFull();
55 std::string machine_description = system_snapshot->MachineDescription();
56 if (!os_version_full.empty()) {
57 if (!machine_description.empty()) {
58 return base::StringPrintf(
59 "%s; %s", os_version_full.c_str(), machine_description.c_str());
60 }
61 return os_version_full;
62 }
63 return machine_description;
64 }
65
66 #if defined(OS_MACOSX)
67 // Converts the value of the MAC_OS_VERSION_MIN_REQUIRED or
68 // MAC_OS_X_VERSION_MAX_ALLOWED macro from <AvailabilityMacros.h> to a number
69 // identifying the minor Mac OS X version that it represents. For example, with
70 // an argument of MAC_OS_X_VERSION_10_6, this function will return 6.
71 int AvailabilityVersionToMacOSXMinorVersion(int availability) {
72 // Through MAC_OS_X_VERSION_10_9, the minor version is the tens digit.
73 if (availability >= 1000 && availability <= 1099) {
74 return (availability / 10) % 10;
75 }
76
77 // After MAC_OS_X_VERSION_10_9, the older format was insufficient to represent
78 // versions. Since then, the minor version is the thousands and hundreds
79 // digits.
80 if (availability >= 100000 && availability <= 109999) {
81 return (availability / 100) % 100;
82 }
83
84 return 0;
85 }
86 #endif
87
88 } // namespace
89
90 namespace internal {
91
92 // For MINIDUMP_MISC_INFO_4::DbgBldStr. dbghelp produces strings like
93 // “dbghelp.i386,6.3.9600.16520” and “dbghelp.amd64,6.3.9600.16520”. Mimic that
94 // format, and add the OS that wrote the minidump along with any relevant
95 // platform-specific data describing the compilation environment.
96 std::string MinidumpMiscInfoDebugBuildString() {
97 // Caution: the minidump file format only has room for 39 UTF-16 code units
98 // plus a UTF-16 NUL terminator. Don’t let strings get longer than this, or
99 // they will be truncated and a message will be logged.
100 #if defined(OS_MACOSX)
101 const char kOS[] = "mac";
102 #elif defined(OS_LINUX)
103 const char kOS[] = "linux";
104 #elif defined(OS_WIN)
105 const char kOS[] = "win";
106 #else
107 #error define kOS for this operating system
108 #endif
109
110 #if defined(ARCH_CPU_X86)
111 const char kCPU[] = "i386";
112 #elif defined(ARCH_CPU_X86_64)
113 const char kCPU[] = "amd64";
114 #else
115 #error define kCPU for this CPU
116 #endif
117
118 std::string debug_build_string = base::StringPrintf("%s.%s,%s,%s",
119 PACKAGE_TARNAME,
120 kCPU,
121 PACKAGE_VERSION,
122 kOS);
123
124 #if defined(OS_MACOSX)
125 debug_build_string += base::StringPrintf(
126 ",%d,%d",
127 AvailabilityVersionToMacOSXMinorVersion(MAC_OS_X_VERSION_MIN_REQUIRED),
128 AvailabilityVersionToMacOSXMinorVersion(MAC_OS_X_VERSION_MAX_ALLOWED));
129 #endif
130
131 return debug_build_string;
132 }
133
134 } // namespace internal
135
136 MinidumpMiscInfoWriter::MinidumpMiscInfoWriter()
137 : MinidumpStreamWriter(), misc_info_() {
138 }
139
140 MinidumpMiscInfoWriter::~MinidumpMiscInfoWriter() {
141 }
142
143 void MinidumpMiscInfoWriter::InitializeFromSnapshot(
144 const ProcessSnapshot* process_snapshot) {
145 DCHECK_EQ(state(), kStateMutable);
146 DCHECK_EQ(misc_info_.Flags1, 0u);
147
148 SetProcessID(InRangeCast<uint32_t>(process_snapshot->ProcessID(), 0));
149
150 const SystemSnapshot* system_snapshot = process_snapshot->System();
151
152 uint64_t current_mhz;
153 uint64_t max_mhz;
154 system_snapshot->CPUFrequency(&current_mhz, &max_mhz);
155 const uint32_t kHzPerMHz = static_cast<const uint32_t>(1E6);
156 SetProcessorPowerInfo(
157 InRangeCast<uint32_t>(current_mhz / kHzPerMHz,
158 std::numeric_limits<uint32_t>::max()),
159 InRangeCast<uint32_t>(max_mhz / kHzPerMHz,
160 std::numeric_limits<uint32_t>::max()),
161 0,
162 0,
163 0);
164
165 timeval start_time;
166 process_snapshot->ProcessStartTime(&start_time);
167
168 timeval user_time;
169 timeval system_time;
170 process_snapshot->ProcessCPUTimes(&user_time, &system_time);
171
172 // Round the resource usage fields to the nearest second, because the minidump
173 // format only has one-second resolution. The start_time field is truncated
174 // instead of rounded so that the process uptime is reflected more accurately
175 // when the start time is compared to the snapshot time in the
176 // MINIDUMP_HEADER, which is also truncated, not rounded.
177 uint32_t user_seconds = TimevalToRoundedSeconds(user_time);
178 uint32_t system_seconds = TimevalToRoundedSeconds(system_time);
179
180 SetProcessTimes(start_time.tv_sec, user_seconds, system_seconds);
181
182 // This determines the system’s time zone, which may be different than the
183 // process’ notion of the time zone.
184 SystemSnapshot::DaylightSavingTimeStatus dst_status;
185 int standard_offset_seconds;
186 int daylight_offset_seconds;
187 std::string standard_name;
188 std::string daylight_name;
189 system_snapshot->TimeZone(&dst_status,
190 &standard_offset_seconds,
191 &daylight_offset_seconds,
192 &standard_name,
193 &daylight_name);
194
195 // standard_offset_seconds is seconds east of UTC, but the minidump file wants
196 // minutes west of UTC. daylight_offset_seconds is also seconds east of UTC,
197 // but the minidump file wants minutes west of the standard offset. The empty
198 // ({}) arguments are for the transition times in and out of daylight saving
199 // time. These are not determined because no API exists to do so, and the
200 // transition times may vary from year to year.
201 SetTimeZone(dst_status,
202 standard_offset_seconds / -60,
203 standard_name,
204 {},
205 0,
206 daylight_name,
207 {},
208 (standard_offset_seconds - daylight_offset_seconds) / 60);
209
210 SetBuildString(BuildString(system_snapshot),
211 internal::MinidumpMiscInfoDebugBuildString());
212 }
213
214 void MinidumpMiscInfoWriter::SetProcessID(uint32_t process_id) {
215 DCHECK_EQ(state(), kStateMutable);
216
217 misc_info_.ProcessId = process_id;
218 misc_info_.Flags1 |= MINIDUMP_MISC1_PROCESS_ID;
219 }
220
221 void MinidumpMiscInfoWriter::SetProcessTimes(time_t process_create_time,
222 uint32_t process_user_time,
223 uint32_t process_kernel_time) {
224 DCHECK_EQ(state(), kStateMutable);
225
226 internal::MinidumpWriterUtil::AssignTimeT(&misc_info_.ProcessCreateTime,
227 process_create_time);
228
229 misc_info_.ProcessUserTime = process_user_time;
230 misc_info_.ProcessKernelTime = process_kernel_time;
231 misc_info_.Flags1 |= MINIDUMP_MISC1_PROCESS_TIMES;
232 }
233
234 void MinidumpMiscInfoWriter::SetProcessorPowerInfo(
235 uint32_t processor_max_mhz,
236 uint32_t processor_current_mhz,
237 uint32_t processor_mhz_limit,
238 uint32_t processor_max_idle_state,
239 uint32_t processor_current_idle_state) {
240 DCHECK_EQ(state(), kStateMutable);
241
242 misc_info_.ProcessorMaxMhz = processor_max_mhz;
243 misc_info_.ProcessorCurrentMhz = processor_current_mhz;
244 misc_info_.ProcessorMhzLimit = processor_mhz_limit;
245 misc_info_.ProcessorMaxIdleState = processor_max_idle_state;
246 misc_info_.ProcessorCurrentIdleState = processor_current_idle_state;
247 misc_info_.Flags1 |= MINIDUMP_MISC1_PROCESSOR_POWER_INFO;
248 }
249
250 void MinidumpMiscInfoWriter::SetProcessIntegrityLevel(
251 uint32_t process_integrity_level) {
252 DCHECK_EQ(state(), kStateMutable);
253
254 misc_info_.ProcessIntegrityLevel = process_integrity_level;
255 misc_info_.Flags1 |= MINIDUMP_MISC3_PROCESS_INTEGRITY;
256 }
257
258 void MinidumpMiscInfoWriter::SetProcessExecuteFlags(
259 uint32_t process_execute_flags) {
260 DCHECK_EQ(state(), kStateMutable);
261
262 misc_info_.ProcessExecuteFlags = process_execute_flags;
263 misc_info_.Flags1 |= MINIDUMP_MISC3_PROCESS_EXECUTE_FLAGS;
264 }
265
266 void MinidumpMiscInfoWriter::SetProtectedProcess(uint32_t protected_process) {
267 DCHECK_EQ(state(), kStateMutable);
268
269 misc_info_.ProtectedProcess = protected_process;
270 misc_info_.Flags1 |= MINIDUMP_MISC3_PROTECTED_PROCESS;
271 }
272
273 void MinidumpMiscInfoWriter::SetTimeZone(uint32_t time_zone_id,
274 int32_t bias,
275 const std::string& standard_name,
276 const SYSTEMTIME& standard_date,
277 int32_t standard_bias,
278 const std::string& daylight_name,
279 const SYSTEMTIME& daylight_date,
280 int32_t daylight_bias) {
281 DCHECK_EQ(state(), kStateMutable);
282
283 misc_info_.TimeZoneId = time_zone_id;
284 misc_info_.TimeZone.Bias = bias;
285
286 internal::MinidumpWriterUtil::AssignUTF8ToUTF16(
287 misc_info_.TimeZone.StandardName,
288 arraysize(misc_info_.TimeZone.StandardName),
289 standard_name);
290
291 misc_info_.TimeZone.StandardDate = standard_date;
292 misc_info_.TimeZone.StandardBias = standard_bias;
293
294 internal::MinidumpWriterUtil::AssignUTF8ToUTF16(
295 misc_info_.TimeZone.DaylightName,
296 arraysize(misc_info_.TimeZone.DaylightName),
297 daylight_name);
298
299 misc_info_.TimeZone.DaylightDate = daylight_date;
300 misc_info_.TimeZone.DaylightBias = daylight_bias;
301
302 misc_info_.Flags1 |= MINIDUMP_MISC3_TIMEZONE;
303 }
304
305 void MinidumpMiscInfoWriter::SetBuildString(
306 const std::string& build_string,
307 const std::string& debug_build_string) {
308 DCHECK_EQ(state(), kStateMutable);
309
310 misc_info_.Flags1 |= MINIDUMP_MISC4_BUILDSTRING;
311
312 internal::MinidumpWriterUtil::AssignUTF8ToUTF16(
313 misc_info_.BuildString, arraysize(misc_info_.BuildString), build_string);
314 internal::MinidumpWriterUtil::AssignUTF8ToUTF16(
315 misc_info_.DbgBldStr,
316 arraysize(misc_info_.DbgBldStr),
317 debug_build_string);
318 }
319
320 bool MinidumpMiscInfoWriter::Freeze() {
321 DCHECK_EQ(state(), kStateMutable);
322
323 if (!MinidumpStreamWriter::Freeze()) {
324 return false;
325 }
326
327 size_t size = CalculateSizeOfObjectFromFlags();
328 if (!AssignIfInRange(&misc_info_.SizeOfInfo, size)) {
329 LOG(ERROR) << "size " << size << " out of range";
330 return false;
331 }
332
333 return true;
334 }
335
336 size_t MinidumpMiscInfoWriter::SizeOfObject() {
337 DCHECK_GE(state(), kStateFrozen);
338
339 return CalculateSizeOfObjectFromFlags();
340 }
341
342 bool MinidumpMiscInfoWriter::WriteObject(FileWriterInterface* file_writer) {
343 DCHECK_EQ(state(), kStateWritable);
344
345 return file_writer->Write(&misc_info_, CalculateSizeOfObjectFromFlags());
346 }
347
348 MinidumpStreamType MinidumpMiscInfoWriter::StreamType() const {
349 return kMinidumpStreamTypeMiscInfo;
350 }
351
352 size_t MinidumpMiscInfoWriter::CalculateSizeOfObjectFromFlags() const {
353 DCHECK_GE(state(), kStateFrozen);
354
355 if (misc_info_.Flags1 & MINIDUMP_MISC4_BUILDSTRING) {
356 return sizeof(MINIDUMP_MISC_INFO_4);
357 }
358 if (misc_info_.Flags1 &
359 (MINIDUMP_MISC3_PROCESS_INTEGRITY | MINIDUMP_MISC3_PROCESS_EXECUTE_FLAGS |
360 MINIDUMP_MISC3_TIMEZONE | MINIDUMP_MISC3_PROTECTED_PROCESS)) {
361 return sizeof(MINIDUMP_MISC_INFO_3);
362 }
363 if (misc_info_.Flags1 & MINIDUMP_MISC1_PROCESSOR_POWER_INFO) {
364 return sizeof(MINIDUMP_MISC_INFO_2);
365 }
366 return sizeof(MINIDUMP_MISC_INFO);
367 }
368
369 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698