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

Side by Side Diff: minidump/minidump_misc_info_writer.cc

Issue 701783004: Add MinidumpMiscInfoWriter::InitializeFromSnapshot() and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@minidump_init_from_snapshot_system_info
Patch Set: Rebase Created 6 years, 1 month 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
« no previous file with comments | « minidump/minidump_misc_info_writer.h ('k') | minidump/minidump_misc_info_writer_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "minidump/minidump_misc_info_writer.h" 15 #include "minidump/minidump_misc_info_writer.h"
16 16
17 #include <limits>
18
17 #include "base/logging.h" 19 #include "base/logging.h"
18 #include "base/numerics/safe_conversions.h" 20 #include "base/numerics/safe_conversions.h"
21 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "build/build_config.h"
20 #include "minidump/minidump_writer_util.h" 24 #include "minidump/minidump_writer_util.h"
25 #include "package.h"
26 #include "snapshot/process_snapshot.h"
27 #include "snapshot/system_snapshot.h"
21 #include "util/file/file_writer.h" 28 #include "util/file/file_writer.h"
29 #include "util/numeric/in_range_cast.h"
22 #include "util/numeric/safe_assignment.h" 30 #include "util/numeric/safe_assignment.h"
23 31
32 #if defined(OS_MACOSX)
33 #include <AvailabilityMacros.h>
34 #endif
35
24 namespace crashpad { 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 = 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 #else
105 #error define kOS for this operating system
106 #endif
107
108 #if defined(ARCH_CPU_X86)
109 const char kCPU[] = "i386";
110 #elif defined(ARCH_CPU_X86_64)
111 const char kCPU[] = "amd64";
112 #else
113 #error define kCPU for this CPU
114 #endif
115
116 std::string debug_build_string = base::StringPrintf("%s.%s,%s,%s",
117 PACKAGE_TARNAME,
118 kCPU,
119 PACKAGE_VERSION,
120 kOS);
121
122 #if defined(OS_MACOSX)
123 debug_build_string += base::StringPrintf(
124 ",%d,%d",
125 AvailabilityVersionToMacOSXMinorVersion(MAC_OS_X_VERSION_MIN_REQUIRED),
126 AvailabilityVersionToMacOSXMinorVersion(MAC_OS_X_VERSION_MAX_ALLOWED));
127 #endif
128
129 return debug_build_string;
130 }
131
132 } // namespace internal
25 133
26 MinidumpMiscInfoWriter::MinidumpMiscInfoWriter() 134 MinidumpMiscInfoWriter::MinidumpMiscInfoWriter()
27 : MinidumpStreamWriter(), misc_info_() { 135 : MinidumpStreamWriter(), misc_info_() {
28 } 136 }
29 137
30 MinidumpMiscInfoWriter::~MinidumpMiscInfoWriter() { 138 MinidumpMiscInfoWriter::~MinidumpMiscInfoWriter() {
31 } 139 }
32 140
33 void MinidumpMiscInfoWriter::SetProcessId(uint32_t process_id) { 141 void MinidumpMiscInfoWriter::InitializeFromSnapshot(
142 const ProcessSnapshot* process_snapshot) {
143 DCHECK_EQ(state(), kStateMutable);
144 DCHECK_EQ(misc_info_.Flags1, 0u);
145
146 SetProcessID(InRangeCast<uint32_t>(process_snapshot->ProcessID(), 0));
147
148 const SystemSnapshot* system_snapshot = process_snapshot->System();
149
150 uint64_t current_mhz;
151 uint64_t max_mhz;
152 system_snapshot->CPUFrequency(&current_mhz, &max_mhz);
153 const uint32_t kHzPerMHz = 1E6;
154 SetProcessorPowerInfo(
155 InRangeCast<uint32_t>(current_mhz / kHzPerMHz,
156 std::numeric_limits<uint32_t>::max()),
157 InRangeCast<uint32_t>(max_mhz / kHzPerMHz,
158 std::numeric_limits<uint32_t>::max()),
159 0,
160 0,
161 0);
162
163 timeval start_time;
164 process_snapshot->ProcessStartTime(&start_time);
165
166 timeval user_time;
167 timeval system_time;
168 process_snapshot->ProcessCPUTimes(&user_time, &system_time);
169
170 // Round the resource usage fields to the nearest second, because the minidump
171 // format only has one-second resolution. The start_time field is truncated
172 // instead of rounded so that the process uptime is reflected more accurately
173 // when the start time is compared to the snapshot time in the
174 // MINIDUMP_HEADER, which is also truncated, not rounded.
175 uint32_t user_seconds = TimevalToRoundedSeconds(user_time);
176 uint32_t system_seconds = TimevalToRoundedSeconds(system_time);
177
178 SetProcessTimes(start_time.tv_sec, user_seconds, system_seconds);
179
180 // This determines the system’s time zone, which may be different than the
181 // process’ notion of the time zone.
182 SystemSnapshot::DaylightSavingTimeStatus dst_status;
183 int standard_offset_seconds;
184 int daylight_offset_seconds;
185 std::string standard_name;
186 std::string daylight_name;
187 system_snapshot->TimeZone(&dst_status,
188 &standard_offset_seconds,
189 &daylight_offset_seconds,
190 &standard_name,
191 &daylight_name);
192
193 // standard_offset_seconds is seconds east of UTC, but the minidump file wants
194 // minutes west of UTC. daylight_offset_seconds is also seconds east of UTC,
195 // but the minidump file wants minutes west of the standard offset. The empty
196 // ({}) arguments are for the transition times in and out of daylight saving
197 // time. These are not determined because no API exists to do so, and the
198 // transition times may vary from year to year.
199 SetTimeZone(dst_status,
200 standard_offset_seconds / -60,
201 standard_name,
202 {},
203 0,
204 daylight_name,
205 {},
206 (standard_offset_seconds - daylight_offset_seconds) / 60);
207
208 SetBuildString(BuildString(system_snapshot),
209 internal::MinidumpMiscInfoDebugBuildString());
210 }
211
212 void MinidumpMiscInfoWriter::SetProcessID(uint32_t process_id) {
34 DCHECK_EQ(state(), kStateMutable); 213 DCHECK_EQ(state(), kStateMutable);
35 214
36 misc_info_.ProcessId = process_id; 215 misc_info_.ProcessId = process_id;
37 misc_info_.Flags1 |= MINIDUMP_MISC1_PROCESS_ID; 216 misc_info_.Flags1 |= MINIDUMP_MISC1_PROCESS_ID;
38 } 217 }
39 218
40 void MinidumpMiscInfoWriter::SetProcessTimes(time_t process_create_time, 219 void MinidumpMiscInfoWriter::SetProcessTimes(time_t process_create_time,
41 uint32_t process_user_time, 220 uint32_t process_user_time,
42 uint32_t process_kernel_time) { 221 uint32_t process_kernel_time) {
43 DCHECK_EQ(state(), kStateMutable); 222 DCHECK_EQ(state(), kStateMutable);
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 MINIDUMP_MISC3_TIMEZONE | MINIDUMP_MISC3_PROTECTED_PROCESS)) { 358 MINIDUMP_MISC3_TIMEZONE | MINIDUMP_MISC3_PROTECTED_PROCESS)) {
180 return sizeof(MINIDUMP_MISC_INFO_3); 359 return sizeof(MINIDUMP_MISC_INFO_3);
181 } 360 }
182 if (misc_info_.Flags1 & MINIDUMP_MISC1_PROCESSOR_POWER_INFO) { 361 if (misc_info_.Flags1 & MINIDUMP_MISC1_PROCESSOR_POWER_INFO) {
183 return sizeof(MINIDUMP_MISC_INFO_2); 362 return sizeof(MINIDUMP_MISC_INFO_2);
184 } 363 }
185 return sizeof(MINIDUMP_MISC_INFO); 364 return sizeof(MINIDUMP_MISC_INFO);
186 } 365 }
187 366
188 } // namespace crashpad 367 } // namespace crashpad
OLDNEW
« no previous file with comments | « minidump/minidump_misc_info_writer.h ('k') | minidump/minidump_misc_info_writer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698