1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #include "chromecast/crash/linux/dump_info.h"
8 #include "base/logging.h"
9 #include "base/values.h"
11 namespace chromecast
{
15 const char kDumpTimeFormat
[] = "%Y-%m-%d %H:%M:%S";
16 const unsigned kDumpTimeMaxLen
= 255;
18 const int kNumRequiredParams
= 5;
20 const char kNameKey
[] = "name";
21 const char kDumpTimeKey
[] = "dump_time";
22 const char kDumpKey
[] = "dump";
23 const char kUptimeKey
[] = "uptime";
24 const char kLogfileKey
[] = "logfile";
25 const char kSuffixKey
[] = "suffix";
26 const char kPrevAppNameKey
[] = "prev_app_name";
27 const char kCurAppNameKey
[] = "cur_app_name";
28 const char kLastAppNameKey
[] = "last_app_name";
29 const char kReleaseVersionKey
[] = "release_version";
30 const char kBuildNumberKey
[] = "build_number";
34 DumpInfo::DumpInfo(const base::Value
* entry
) : valid_(ParseEntry(entry
)) {
37 DumpInfo::DumpInfo(const std::string
& crashed_process_dump
,
38 const std::string
& logfile
,
39 const time_t& dump_time
,
40 const MinidumpParams
& params
)
41 : crashed_process_dump_(crashed_process_dump
),
43 dump_time_(dump_time
),
47 // Validate the time passed in.
48 struct tm
* tm
= gmtime(&dump_time
);
49 char buf
[kDumpTimeMaxLen
];
50 int n
= strftime(buf
, kDumpTimeMaxLen
, kDumpTimeFormat
, tm
);
52 LOG(INFO
) << "strftime failed";
59 DumpInfo::~DumpInfo() {
62 scoped_ptr
<base::Value
> DumpInfo::GetAsValue() const {
63 scoped_ptr
<base::Value
> result
= make_scoped_ptr(new base::DictionaryValue());
64 base::DictionaryValue
* entry
;
65 result
->GetAsDictionary(&entry
);
66 entry
->SetString(kNameKey
, params_
.process_name
);
68 struct tm
* tm
= gmtime(&dump_time_
);
69 char buf
[kDumpTimeMaxLen
];
70 int n
= strftime(buf
, kDumpTimeMaxLen
, kDumpTimeFormat
, tm
);
72 std::string
dump_time(buf
);
73 entry
->SetString(kDumpTimeKey
, dump_time
);
75 entry
->SetString(kDumpKey
, crashed_process_dump_
);
76 std::string uptime
= std::to_string(params_
.process_uptime
);
77 entry
->SetString(kUptimeKey
, uptime
);
78 entry
->SetString(kLogfileKey
, logfile_
);
79 entry
->SetString(kSuffixKey
, params_
.suffix
);
80 entry
->SetString(kPrevAppNameKey
, params_
.previous_app_name
);
81 entry
->SetString(kCurAppNameKey
, params_
.current_app_name
);
82 entry
->SetString(kLastAppNameKey
, params_
.last_app_name
);
83 entry
->SetString(kReleaseVersionKey
, params_
.cast_release_version
);
84 entry
->SetString(kBuildNumberKey
, params_
.cast_build_number
);
89 bool DumpInfo::ParseEntry(const base::Value
* entry
) {
96 const base::DictionaryValue
* dict
;
97 if (!entry
->GetAsDictionary(&dict
)) {
101 // Extract required fields.
102 if (!dict
->GetString(kNameKey
, ¶ms_
.process_name
)) {
106 std::string dump_time
;
107 if (!dict
->GetString(kDumpTimeKey
, &dump_time
)) {
110 if (!SetDumpTimeFromString(dump_time
)) {
114 if (!dict
->GetString(kDumpKey
, &crashed_process_dump_
)) {
119 if (!dict
->GetString(kUptimeKey
, &uptime
)) {
123 params_
.process_uptime
= strtoull(uptime
.c_str(), nullptr, 0);
128 if (!dict
->GetString(kLogfileKey
, &logfile_
)) {
131 size_t num_params
= kNumRequiredParams
;
133 // Extract all other optional fields.
134 if (dict
->GetString(kSuffixKey
, ¶ms_
.suffix
)) {
137 if (dict
->GetString(kPrevAppNameKey
, ¶ms_
.previous_app_name
)) {
140 if (dict
->GetString(kCurAppNameKey
, ¶ms_
.current_app_name
)) {
143 if (dict
->GetString(kLastAppNameKey
, ¶ms_
.last_app_name
)) {
146 if (dict
->GetString(kReleaseVersionKey
, ¶ms_
.cast_release_version
)) {
149 if (dict
->GetString(kBuildNumberKey
, ¶ms_
.cast_build_number
)) {
153 // Disallow extraneous params
154 if (dict
->size() != num_params
) {
162 bool DumpInfo::SetDumpTimeFromString(const std::string
& timestr
) {
164 char* text
= strptime(timestr
.c_str(), kDumpTimeFormat
, &tm
);
165 dump_time_
= mktime(&tm
);
166 if (!text
|| dump_time_
< 0) {
167 LOG(INFO
) << "Failed to convert dump time invalid";
173 } // namespace chromecast