1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #define DEBUG false
17 #include "Log.h"
18
19 #include "Section.h"
20
21 #include <dirent.h>
22 #include <errno.h>
23
24 #include <mutex>
25 #include <set>
26
27 #include <android-base/file.h>
28 #include <android-base/properties.h>
29 #include <android-base/stringprintf.h>
30 #include <android/util/protobuf.h>
31 #include <android/util/ProtoOutputStream.h>
32 #include <binder/IServiceManager.h>
33 #include <debuggerd/client.h>
34 #include <dumputils/dump_utils.h>
35 #include <log/log_event_list.h>
36 #include <log/log_read.h>
37 #include <log/logprint.h>
38 #include <private/android_logger.h>
39
40 #include "FdBuffer.h"
41 #include "Privacy.h"
42 #include "frameworks/base/core/proto/android/os/backtrace.proto.h"
43 #include "frameworks/base/core/proto/android/os/data.proto.h"
44 #include "frameworks/base/core/proto/android/util/log.proto.h"
45 #include "incidentd_util.h"
46
47 namespace android {
48 namespace os {
49 namespace incidentd {
50
51 using namespace android::base;
52 using namespace android::util;
53
54 // special section ids
55 const int FIELD_ID_INCIDENT_METADATA = 2;
56
57 // incident section parameters
58 const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
59 const char* GZIP[] = {"/system/bin/gzip", NULL};
60
fork_execute_incident_helper(const int id,Fpipe * p2cPipe,Fpipe * c2pPipe)61 static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) {
62 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
63 return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe);
64 }
65
section_requires_specific_mention(int sectionId)66 bool section_requires_specific_mention(int sectionId) {
67 switch (sectionId) {
68 case 3025: // restricted_images
69 return true;
70 case 3026: // system_trace
71 return true;
72 default:
73 return false;
74 }
75 }
76
77 // ================================================================================
Section(int i,int64_t timeoutMs)78 Section::Section(int i, int64_t timeoutMs)
79 : id(i),
80 timeoutMs(timeoutMs) {
81 }
82
~Section()83 Section::~Section() {}
84
85 // ================================================================================
isSysfs(const char * filename)86 static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
87
FileSection(int id,const char * filename,const int64_t timeoutMs)88 FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
89 : Section(id, timeoutMs), mFilename(filename) {
90 name = "file ";
91 name += filename;
92 mIsSysfs = isSysfs(filename);
93 }
94
~FileSection()95 FileSection::~FileSection() {}
96
Execute(ReportWriter * writer) const97 status_t FileSection::Execute(ReportWriter* writer) const {
98 // read from mFilename first, make sure the file is available
99 // add O_CLOEXEC to make sure it is closed when exec incident helper
100 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
101 if (fd.get() == -1) {
102 ALOGW("[%s] failed to open file", this->name.string());
103 // There may be some devices/architectures that won't have the file.
104 // Just return here without an error.
105 return NO_ERROR;
106 }
107
108 FdBuffer buffer;
109 Fpipe p2cPipe;
110 Fpipe c2pPipe;
111 // initiate pipes to pass data to/from incident_helper
112 if (!p2cPipe.init() || !c2pPipe.init()) {
113 ALOGW("[%s] failed to setup pipes", this->name.string());
114 return -errno;
115 }
116
117 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
118 if (pid == -1) {
119 ALOGW("[%s] failed to fork", this->name.string());
120 return -errno;
121 }
122
123 // parent process
124 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
125 std::move(c2pPipe.readFd()),
126 this->timeoutMs, mIsSysfs);
127 writer->setSectionStats(buffer);
128 if (readStatus != NO_ERROR || buffer.timedOut()) {
129 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
130 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
131 kill_child(pid);
132 return readStatus;
133 }
134
135 status_t ihStatus = wait_child(pid);
136 if (ihStatus != NO_ERROR) {
137 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
138 return ihStatus;
139 }
140
141 return writer->writeSection(buffer);
142 }
143 // ================================================================================
GZipSection(int id,const char * filename,...)144 GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
145 va_list args;
146 va_start(args, filename);
147 mFilenames = varargs(filename, args);
148 va_end(args);
149 name = "gzip";
150 for (int i = 0; mFilenames[i] != NULL; i++) {
151 name += " ";
152 name += mFilenames[i];
153 }
154 }
155
~GZipSection()156 GZipSection::~GZipSection() { free(mFilenames); }
157
Execute(ReportWriter * writer) const158 status_t GZipSection::Execute(ReportWriter* writer) const {
159 // Reads the files in order, use the first available one.
160 int index = 0;
161 unique_fd fd;
162 while (mFilenames[index] != NULL) {
163 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
164 if (fd.get() != -1) {
165 break;
166 }
167 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
168 index++; // look at the next file.
169 }
170 if (fd.get() == -1) {
171 ALOGW("[%s] can't open all the files", this->name.string());
172 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
173 }
174 FdBuffer buffer;
175 Fpipe p2cPipe;
176 Fpipe c2pPipe;
177 // initiate pipes to pass data to/from gzip
178 if (!p2cPipe.init() || !c2pPipe.init()) {
179 ALOGW("[%s] failed to setup pipes", this->name.string());
180 return -errno;
181 }
182
183 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
184 if (pid == -1) {
185 ALOGW("[%s] failed to fork", this->name.string());
186 return -errno;
187 }
188 // parent process
189
190 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
191 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
192 sp<EncodedBuffer> internalBuffer = buffer.data();
193 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
194 size_t fileLen = strlen(mFilenames[index]);
195 internalBuffer->writeRawVarint32(fileLen);
196 for (size_t i = 0; i < fileLen; i++) {
197 internalBuffer->writeRawByte(mFilenames[index][i]);
198 }
199 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
200 WIRE_TYPE_LENGTH_DELIMITED);
201 size_t editPos = internalBuffer->wp()->pos();
202 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
203 size_t dataBeginAt = internalBuffer->wp()->pos();
204 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
205
206 status_t readStatus = buffer.readProcessedDataInStream(
207 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
208 isSysfs(mFilenames[index]));
209 writer->setSectionStats(buffer);
210 if (readStatus != NO_ERROR || buffer.timedOut()) {
211 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
212 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
213 kill_child(pid);
214 return readStatus;
215 }
216
217 status_t gzipStatus = wait_child(pid);
218 if (gzipStatus != NO_ERROR) {
219 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
220 return gzipStatus;
221 }
222 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
223 size_t dataSize = buffer.size() - dataBeginAt;
224 internalBuffer->wp()->rewind()->move(editPos);
225 internalBuffer->writeRawVarint32(dataSize);
226 internalBuffer->copy(dataBeginAt, dataSize);
227
228 return writer->writeSection(buffer);
229 }
230
231 // ================================================================================
232 struct WorkerThreadData : public virtual RefBase {
233 const WorkerThreadSection* section;
234 Fpipe pipe;
235
236 // Lock protects these fields
237 mutex lock;
238 bool workerDone;
239 status_t workerError;
240
241 explicit WorkerThreadData(const WorkerThreadSection* section);
242 virtual ~WorkerThreadData();
243 };
244
WorkerThreadData(const WorkerThreadSection * sec)245 WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
246 : section(sec), workerDone(false), workerError(NO_ERROR) {}
247
~WorkerThreadData()248 WorkerThreadData::~WorkerThreadData() {}
249
250 // ================================================================================
WorkerThreadSection(int id,const int64_t timeoutMs)251 WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
252 : Section(id, timeoutMs) {}
253
~WorkerThreadSection()254 WorkerThreadSection::~WorkerThreadSection() {}
255
sigpipe_handler(int signum)256 void sigpipe_handler(int signum) {
257 if (signum == SIGPIPE) {
258 ALOGE("Wrote to a broken pipe\n");
259 } else {
260 ALOGE("Received unexpected signal: %d\n", signum);
261 }
262 }
263
worker_thread_func(void * cookie)264 static void* worker_thread_func(void* cookie) {
265 // Don't crash the service if we write to a closed pipe (which can happen if
266 // dumping times out).
267 signal(SIGPIPE, sigpipe_handler);
268
269 WorkerThreadData* data = (WorkerThreadData*)cookie;
270 status_t err = data->section->BlockingCall(data->pipe.writeFd().get());
271
272 {
273 unique_lock<mutex> lock(data->lock);
274 data->workerDone = true;
275 data->workerError = err;
276 }
277
278 data->pipe.writeFd().reset();
279 data->decStrong(data->section);
280 // data might be gone now. don't use it after this point in this thread.
281 return NULL;
282 }
283
Execute(ReportWriter * writer) const284 status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
285 status_t err = NO_ERROR;
286 pthread_t thread;
287 pthread_attr_t attr;
288 bool workerDone = false;
289 FdBuffer buffer;
290
291 // Data shared between this thread and the worker thread.
292 sp<WorkerThreadData> data = new WorkerThreadData(this);
293
294 // Create the pipe
295 if (!data->pipe.init()) {
296 return -errno;
297 }
298
299 // Create the thread
300 err = pthread_attr_init(&attr);
301 if (err != 0) {
302 return -err;
303 }
304 // TODO: Do we need to tweak thread priority?
305 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
306 if (err != 0) {
307 pthread_attr_destroy(&attr);
308 return -err;
309 }
310
311 // The worker thread needs a reference and we can't let the count go to zero
312 // if that thread is slow to start.
313 data->incStrong(this);
314
315 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
316 pthread_attr_destroy(&attr);
317 if (err != 0) {
318 data->decStrong(this);
319 return -err;
320 }
321
322 // Loop reading until either the timeout or the worker side is done (i.e. eof).
323 err = buffer.read(data->pipe.readFd().get(), this->timeoutMs);
324 if (err != NO_ERROR) {
325 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
326 }
327
328 // Done with the read fd. The worker thread closes the write one so
329 // we never race and get here first.
330 data->pipe.readFd().reset();
331
332 // If the worker side is finished, then return its error (which may overwrite
333 // our possible error -- but it's more interesting anyway). If not, then we timed out.
334 {
335 unique_lock<mutex> lock(data->lock);
336 if (data->workerError != NO_ERROR) {
337 err = data->workerError;
338 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
339 }
340 workerDone = data->workerDone;
341 }
342
343 writer->setSectionStats(buffer);
344 if (err != NO_ERROR) {
345 char errMsg[128];
346 snprintf(errMsg, 128, "[%s] failed with error '%s'",
347 this->name.string(), strerror(-err));
348 writer->error(this, err, "WorkerThreadSection failed.");
349 return NO_ERROR;
350 }
351 if (buffer.truncated()) {
352 ALOGW("[%s] too large, truncating", this->name.string());
353 // Do not write a truncated section. It won't pass through the PrivacyFilter.
354 return NO_ERROR;
355 }
356 if (!workerDone || buffer.timedOut()) {
357 ALOGW("[%s] timed out", this->name.string());
358 return NO_ERROR;
359 }
360
361 // Write the data that was collected
362 return writer->writeSection(buffer);
363 }
364
365 // ================================================================================
CommandSection(int id,const int64_t timeoutMs,const char * command,...)366 CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
367 : Section(id, timeoutMs) {
368 va_list args;
369 va_start(args, command);
370 mCommand = varargs(command, args);
371 va_end(args);
372 name = "cmd";
373 for (int i = 0; mCommand[i] != NULL; i++) {
374 name += " ";
375 name += mCommand[i];
376 }
377 }
378
CommandSection(int id,const char * command,...)379 CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
380 va_list args;
381 va_start(args, command);
382 mCommand = varargs(command, args);
383 va_end(args);
384 name = "cmd";
385 for (int i = 0; mCommand[i] != NULL; i++) {
386 name += " ";
387 name += mCommand[i];
388 }
389 }
390
~CommandSection()391 CommandSection::~CommandSection() { free(mCommand); }
392
Execute(ReportWriter * writer) const393 status_t CommandSection::Execute(ReportWriter* writer) const {
394 FdBuffer buffer;
395 Fpipe cmdPipe;
396 Fpipe ihPipe;
397
398 if (!cmdPipe.init() || !ihPipe.init()) {
399 ALOGW("[%s] failed to setup pipes", this->name.string());
400 return -errno;
401 }
402
403 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
404 if (cmdPid == -1) {
405 ALOGW("[%s] failed to fork", this->name.string());
406 return -errno;
407 }
408 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
409 if (ihPid == -1) {
410 ALOGW("[%s] failed to fork", this->name.string());
411 return -errno;
412 }
413
414 cmdPipe.writeFd().reset();
415 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
416 writer->setSectionStats(buffer);
417 if (readStatus != NO_ERROR || buffer.timedOut()) {
418 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
419 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
420 kill_child(cmdPid);
421 kill_child(ihPid);
422 return readStatus;
423 }
424
425 // Waiting for command here has one trade-off: the failed status of command won't be detected
426 // until buffer timeout, but it has advatage on starting the data stream earlier.
427 status_t cmdStatus = wait_child(cmdPid);
428 status_t ihStatus = wait_child(ihPid);
429 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
430 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
431 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
432 // Not a fatal error.
433 return NO_ERROR;
434 }
435
436 return writer->writeSection(buffer);
437 }
438
439 // ================================================================================
DumpsysSection(int id,const char * service,...)440 DumpsysSection::DumpsysSection(int id, const char* service, ...)
441 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
442 name = "dumpsys ";
443 name += service;
444
445 va_list args;
446 va_start(args, service);
447 while (true) {
448 const char* arg = va_arg(args, const char*);
449 if (arg == NULL) {
450 break;
451 }
452 mArgs.add(String16(arg));
453 name += " ";
454 name += arg;
455 }
456 va_end(args);
457 }
458
~DumpsysSection()459 DumpsysSection::~DumpsysSection() {}
460
BlockingCall(int pipeWriteFd) const461 status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
462 // checkService won't wait for the service to show up like getService will.
463 sp<IBinder> service = defaultServiceManager()->checkService(mService);
464
465 if (service == NULL) {
466 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
467 return NAME_NOT_FOUND;
468 }
469
470 service->dump(pipeWriteFd, mArgs);
471
472 return NO_ERROR;
473 }
474
475 // ================================================================================
476 // initialization only once in Section.cpp.
477 map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
478
LogSection(int id,log_id_t logID)479 LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
480 name = "logcat ";
481 name += android_log_id_to_name(logID);
482 switch (logID) {
483 case LOG_ID_EVENTS:
484 case LOG_ID_STATS:
485 case LOG_ID_SECURITY:
486 mBinary = true;
487 break;
488 default:
489 mBinary = false;
490 }
491 }
492
~LogSection()493 LogSection::~LogSection() {}
494
trimTail(char const * buf,size_t len)495 static size_t trimTail(char const* buf, size_t len) {
496 while (len > 0) {
497 char c = buf[len - 1];
498 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
499 len--;
500 } else {
501 break;
502 }
503 }
504 return len;
505 }
506
get4LE(uint8_t const * src)507 static inline int32_t get4LE(uint8_t const* src) {
508 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
509 }
510
BlockingCall(int pipeWriteFd) const511 status_t LogSection::BlockingCall(int pipeWriteFd) const {
512 // Open log buffer and getting logs since last retrieved time if any.
513 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
514 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
515 ? android_logger_list_alloc(ANDROID_LOG_NONBLOCK, 0, 0)
516 : android_logger_list_alloc_time(ANDROID_LOG_NONBLOCK,
517 gLastLogsRetrieved[mLogID], 0),
518 android_logger_list_free);
519
520 if (android_logger_open(loggers.get(), mLogID) == NULL) {
521 ALOGE("[%s] Can't get logger.", this->name.string());
522 return -1;
523 }
524
525 log_msg msg;
526 log_time lastTimestamp(0);
527
528 ProtoOutputStream proto;
529 while (true) { // keeps reading until logd buffer is fully read.
530 status_t err = android_logger_list_read(loggers.get(), &msg);
531 // err = 0 - no content, unexpected connection drop or EOF.
532 // err = +ive number - size of retrieved data from logger
533 // err = -ive number, OS supplied error _except_ for -EAGAIN
534 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
535 if (err <= 0) {
536 if (err != -EAGAIN) {
537 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
538 }
539 // dump previous logs and don't consider this error a failure.
540 break;
541 }
542 if (mBinary) {
543 // remove the first uint32 which is tag's index in event log tags
544 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
545 msg.len() - sizeof(uint32_t));
546 ;
547 android_log_list_element elem;
548
549 lastTimestamp.tv_sec = msg.entry.sec;
550 lastTimestamp.tv_nsec = msg.entry.nsec;
551
552 // format a BinaryLogEntry
553 uint64_t token = proto.start(LogProto::BINARY_LOGS);
554 proto.write(BinaryLogEntry::SEC, (int32_t)msg.entry.sec);
555 proto.write(BinaryLogEntry::NANOSEC, (int32_t)msg.entry.nsec);
556 proto.write(BinaryLogEntry::UID, (int)msg.entry.uid);
557 proto.write(BinaryLogEntry::PID, msg.entry.pid);
558 proto.write(BinaryLogEntry::TID, (int32_t)msg.entry.tid);
559 proto.write(BinaryLogEntry::TAG_INDEX,
560 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
561 do {
562 elem = android_log_read_next(context);
563 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
564 switch (elem.type) {
565 case EVENT_TYPE_INT:
566 proto.write(BinaryLogEntry::Elem::TYPE,
567 BinaryLogEntry::Elem::EVENT_TYPE_INT);
568 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
569 break;
570 case EVENT_TYPE_LONG:
571 proto.write(BinaryLogEntry::Elem::TYPE,
572 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
573 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
574 break;
575 case EVENT_TYPE_STRING:
576 proto.write(BinaryLogEntry::Elem::TYPE,
577 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
578 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
579 break;
580 case EVENT_TYPE_FLOAT:
581 proto.write(BinaryLogEntry::Elem::TYPE,
582 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
583 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
584 break;
585 case EVENT_TYPE_LIST:
586 proto.write(BinaryLogEntry::Elem::TYPE,
587 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
588 break;
589 case EVENT_TYPE_LIST_STOP:
590 proto.write(BinaryLogEntry::Elem::TYPE,
591 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
592 break;
593 case EVENT_TYPE_UNKNOWN:
594 proto.write(BinaryLogEntry::Elem::TYPE,
595 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
596 break;
597 }
598 proto.end(elemToken);
599 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
600 proto.end(token);
601 if (context) {
602 android_log_destroy(&context);
603 }
604 } else {
605 AndroidLogEntry entry;
606 err = android_log_processLogBuffer(&msg.entry, &entry);
607 if (err != NO_ERROR) {
608 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
609 break;
610 }
611 lastTimestamp.tv_sec = entry.tv_sec;
612 lastTimestamp.tv_nsec = entry.tv_nsec;
613
614 // format a TextLogEntry
615 uint64_t token = proto.start(LogProto::TEXT_LOGS);
616 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
617 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
618 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
619 proto.write(TextLogEntry::UID, entry.uid);
620 proto.write(TextLogEntry::PID, entry.pid);
621 proto.write(TextLogEntry::TID, entry.tid);
622 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
623 proto.write(TextLogEntry::LOG, entry.message,
624 trimTail(entry.message, entry.messageLen));
625 proto.end(token);
626 }
627 }
628 gLastLogsRetrieved[mLogID] = lastTimestamp;
629 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
630 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
631 return EPIPE;
632 }
633 return NO_ERROR;
634 }
635
636 // ================================================================================
637
TombstoneSection(int id,const char * type,const int64_t timeoutMs)638 TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
639 : WorkerThreadSection(id, timeoutMs), mType(type) {
640 name = "tombstone ";
641 name += type;
642 }
643
~TombstoneSection()644 TombstoneSection::~TombstoneSection() {}
645
BlockingCall(int pipeWriteFd) const646 status_t TombstoneSection::BlockingCall(int pipeWriteFd) const {
647 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
648 if (proc.get() == nullptr) {
649 ALOGE("opendir /proc failed: %s\n", strerror(errno));
650 return -errno;
651 }
652
653 const std::set<int> hal_pids = get_interesting_hal_pids();
654
655 ProtoOutputStream proto;
656 struct dirent* d;
657 status_t err = NO_ERROR;
658 while ((d = readdir(proc.get()))) {
659 int pid = atoi(d->d_name);
660 if (pid <= 0) {
661 continue;
662 }
663
664 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
665 std::string exe;
666 if (!android::base::Readlink(link_name, &exe)) {
667 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
668 link_name.c_str(), strerror(errno));
669 continue;
670 }
671
672 bool is_java_process;
673 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
674 if (mType != "java") continue;
675 // Don't bother dumping backtraces for the zygote.
676 if (IsZygote(pid)) {
677 VLOG("Skipping Zygote");
678 continue;
679 }
680
681 is_java_process = true;
682 } else if (should_dump_native_traces(exe.c_str())) {
683 if (mType != "native") continue;
684 is_java_process = false;
685 } else if (hal_pids.find(pid) != hal_pids.end()) {
686 if (mType != "hal") continue;
687 is_java_process = false;
688 } else {
689 // Probably a native process we don't care about, continue.
690 VLOG("Skipping %d", pid);
691 continue;
692 }
693
694 Fpipe dumpPipe;
695 if (!dumpPipe.init()) {
696 ALOGW("[%s] failed to setup dump pipe", this->name.string());
697 err = -errno;
698 break;
699 }
700
701 const uint64_t start = Nanotime();
702 pid_t child = fork();
703 if (child < 0) {
704 ALOGE("Failed to fork child process");
705 break;
706 } else if (child == 0) {
707 // This is the child process.
708 dumpPipe.readFd().reset();
709 const int ret = dump_backtrace_to_file_timeout(
710 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
711 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
712 if (ret == -1) {
713 if (errno == 0) {
714 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
715 } else {
716 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
717 }
718 }
719 dumpPipe.writeFd().reset();
720 _exit(EXIT_SUCCESS);
721 }
722 dumpPipe.writeFd().reset();
723 // Parent process.
724 // Read from the pipe concurrently to avoid blocking the child.
725 FdBuffer buffer;
726 err = buffer.readFully(dumpPipe.readFd().get());
727 // Wait on the child to avoid it becoming a zombie process.
728 status_t cStatus = wait_child(child);
729 if (err != NO_ERROR) {
730 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
731 dumpPipe.readFd().reset();
732 break;
733 }
734 if (cStatus != NO_ERROR) {
735 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
736 }
737
738 auto dump = std::make_unique<char[]>(buffer.size());
739 sp<ProtoReader> reader = buffer.data()->read();
740 int i = 0;
741 while (reader->hasNext()) {
742 dump[i] = reader->next();
743 i++;
744 }
745 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
746 proto.write(android::os::BackTraceProto::Stack::PID, pid);
747 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
748 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
749 static_cast<long long>(Nanotime() - start));
750 proto.end(token);
751 dumpPipe.readFd().reset();
752 }
753
754 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
755 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
756 if (err != NO_ERROR) {
757 return EPIPE;
758 }
759 }
760
761 return err;
762 }
763
764 } // namespace incidentd
765 } // namespace os
766 } // namespace android
767