blob: 9bf3c18ddd89073c10dc24d98c10f7d4dc6f696a [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 */
Christopher Tate7716aef2013-04-02 14:00:27 -070042 LOG("AM write failure (%d / %s)\n", errno, strerror(errno));
Christopher Tateded2e5a2013-03-19 13:12:23 -070043 return -1;
44 }
45 to_write -= written;
46 }
47 return len;
48}
49
Christopher Tate7716aef2013-04-02 14:00:27 -070050void _LOG(log_t* log, int scopeFlags, const char *fmt, ...) {
Jeff Brown13e715b2011-10-21 12:14:56 -070051 char buf[512];
Christopher Tateded2e5a2013-03-19 13:12:23 -070052 bool want_tfd_write;
53 bool want_log_write;
54 bool want_amfd_write;
Christopher Tate7716aef2013-04-02 14:00:27 -070055 int len = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Jeff Brown13e715b2011-10-21 12:14:56 -070057 va_list ap;
58 va_start(ap, fmt);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Christopher Tateded2e5a2013-03-19 13:12:23 -070060 // where is the information going to go?
Christopher Tate7716aef2013-04-02 14:00:27 -070061 want_tfd_write = log && log->tfd >= 0;
62 want_log_write = IS_AT_FAULT(scopeFlags) && (!log || !log->quiet);
63 want_amfd_write = IS_AT_FAULT(scopeFlags) && !IS_SENSITIVE(scopeFlags) && log && log->amfd >= 0;
Christopher Tateded2e5a2013-03-19 13:12:23 -070064
65 // if we're going to need the literal string, generate it once here
66 if (want_tfd_write || want_amfd_write) {
Jeff Brown13e715b2011-10-21 12:14:56 -070067 vsnprintf(buf, sizeof(buf), fmt, ap);
68 len = strlen(buf);
Christopher Tateded2e5a2013-03-19 13:12:23 -070069 }
70
71 if (want_tfd_write) {
Jeff Brown053b8652012-06-06 16:25:03 -070072 write(log->tfd, buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
74
Christopher Tateded2e5a2013-03-19 13:12:23 -070075 if (want_log_write) {
76 // whatever goes to logcat also goes to the Activity Manager
Jeff Brown13e715b2011-10-21 12:14:56 -070077 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
Christopher Tateded2e5a2013-03-19 13:12:23 -070078 if (want_amfd_write && len > 0) {
79 int written = write_to_am(log->amfd, buf, len);
80 if (written <= 0) {
81 // timeout or other failure on write; stop informing the activity manager
Christopher Tateded2e5a2013-03-19 13:12:23 -070082 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}