blob: de9200abf6d963b61f9d864992ed9c1f602db2a2 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* system/debuggerd/utility.c
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Jeff Brown053b8652012-06-06 16:25:03 -070018#include <stddef.h>
19#include <stdbool.h>
20#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <errno.h>
23#include <unistd.h>
24#include <signal.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070025#include <cutils/logd.h>
26#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070027#include <sys/wait.h>
Christopher Tateded2e5a2013-03-19 13:12:23 -070028#include <arpa/inet.h>
29#include <assert.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
31#include "utility.h"
32
Jeff Brown053b8652012-06-06 16:25:03 -070033const int sleep_time_usec = 50000; /* 0.05 seconds */
34const int max_total_sleep_usec = 10000000; /* 10 seconds */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Christopher Tateded2e5a2013-03-19 13:12:23 -070036static int write_to_am(int fd, const char* buf, int len) {
37 int to_write = len;
38 while (to_write > 0) {
39 int written = TEMP_FAILURE_RETRY( write(fd, buf + len - to_write, to_write) );
40 if (written < 0) {
41 /* hard failure */
42 return -1;
43 }
44 to_write -= written;
45 }
46 return len;
47}
48
Jeff Brown053b8652012-06-06 16:25:03 -070049void _LOG(log_t* log, bool in_tombstone_only, const char *fmt, ...) {
Jeff Brown13e715b2011-10-21 12:14:56 -070050 char buf[512];
Christopher Tateded2e5a2013-03-19 13:12:23 -070051 bool want_tfd_write;
52 bool want_log_write;
53 bool want_amfd_write;
54 int len;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Jeff Brown13e715b2011-10-21 12:14:56 -070056 va_list ap;
57 va_start(ap, fmt);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Christopher Tateded2e5a2013-03-19 13:12:23 -070059 // where is the information going to go?
60 want_tfd_write = log && log->tfd >= 0; // write to the tombstone fd?
61 want_log_write = !in_tombstone_only && (!log || !log->quiet);
62 want_amfd_write = log && log->amfd >= 0; // only used when want_log_write is true
63
64 // if we're going to need the literal string, generate it once here
65 if (want_tfd_write || want_amfd_write) {
Jeff Brown13e715b2011-10-21 12:14:56 -070066 vsnprintf(buf, sizeof(buf), fmt, ap);
67 len = strlen(buf);
Christopher Tateded2e5a2013-03-19 13:12:23 -070068 }
69
70 if (want_tfd_write) {
Jeff Brown053b8652012-06-06 16:25:03 -070071 write(log->tfd, buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 }
73
Christopher Tateded2e5a2013-03-19 13:12:23 -070074 if (want_log_write) {
75 // whatever goes to logcat also goes to the Activity Manager
Jeff Brown13e715b2011-10-21 12:14:56 -070076 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
Christopher Tateded2e5a2013-03-19 13:12:23 -070077 if (want_amfd_write && len > 0) {
78 int written = write_to_am(log->amfd, buf, len);
79 if (written <= 0) {
80 // timeout or other failure on write; stop informing the activity manager
81 LOG("AM write failure, giving up\n");
82 log->amfd = -1;
83 }
84 }
Jeff Brown053b8652012-06-06 16:25:03 -070085 }
Jeff Brown13e715b2011-10-21 12:14:56 -070086 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087}
88
Jeff Brown053b8652012-06-06 16:25:03 -070089int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
90 for (;;) {
91 int status;
92 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
93 if (n < 0) {
94 if(errno == EAGAIN) continue;
95 LOG("waitpid failed: %s\n", strerror(errno));
96 return -1;
97 } else if (n > 0) {
98 XLOG("waitpid: n=%d status=%08x\n", n, status);
99 if (WIFSTOPPED(status)) {
100 return WSTOPSIG(status);
Jeff Brown13e715b2011-10-21 12:14:56 -0700101 } else {
Jeff Brown053b8652012-06-06 16:25:03 -0700102 LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status);
103 return -1;
Jeff Brown13e715b2011-10-21 12:14:56 -0700104 }
105 }
106
Jeff Brown053b8652012-06-06 16:25:03 -0700107 if (*total_sleep_time_usec > max_total_sleep_usec) {
108 LOG("timed out waiting for tid=%d to die\n", tid);
109 return -1;
110 }
111
112 /* not ready yet */
113 XLOG("not ready yet\n");
114 usleep(sleep_time_usec);
115 *total_sleep_time_usec += sleep_time_usec;
Jeff Brown13e715b2011-10-21 12:14:56 -0700116 }
117}
118
Jeff Brown053b8652012-06-06 16:25:03 -0700119void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700120 siginfo_t si;
Jeff Brown053b8652012-06-06 16:25:03 -0700121 while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
122 if (*total_sleep_time_usec > max_total_sleep_usec) {
123 LOG("timed out waiting for tid=%d to stop\n", tid);
Jeff Brown13e715b2011-10-21 12:14:56 -0700124 break;
125 }
126
Jeff Brown053b8652012-06-06 16:25:03 -0700127 usleep(sleep_time_usec);
128 *total_sleep_time_usec += sleep_time_usec;
Jeff Brown13e715b2011-10-21 12:14:56 -0700129 }
130}