blob: aabaf74684b518a3815054b594150143f4ef1370 [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
29#include "utility.h"
30
Jeff Brown053b8652012-06-06 16:25:03 -070031const int sleep_time_usec = 50000; /* 0.05 seconds */
32const int max_total_sleep_usec = 10000000; /* 10 seconds */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Jeff Brown053b8652012-06-06 16:25:03 -070034void _LOG(log_t* log, bool in_tombstone_only, const char *fmt, ...) {
Jeff Brown13e715b2011-10-21 12:14:56 -070035 char buf[512];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Jeff Brown13e715b2011-10-21 12:14:56 -070037 va_list ap;
38 va_start(ap, fmt);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Jeff Brown053b8652012-06-06 16:25:03 -070040 if (log && log->tfd >= 0) {
Jeff Brown13e715b2011-10-21 12:14:56 -070041 int len;
42 vsnprintf(buf, sizeof(buf), fmt, ap);
43 len = strlen(buf);
Jeff Brown053b8652012-06-06 16:25:03 -070044 write(log->tfd, buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 }
46
Jeff Brown053b8652012-06-06 16:25:03 -070047 if (!in_tombstone_only && (!log || !log->quiet)) {
Jeff Brown13e715b2011-10-21 12:14:56 -070048 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
Jeff Brown053b8652012-06-06 16:25:03 -070049 }
Jeff Brown13e715b2011-10-21 12:14:56 -070050 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051}
52
Jeff Brown053b8652012-06-06 16:25:03 -070053int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
54 for (;;) {
55 int status;
56 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
57 if (n < 0) {
58 if(errno == EAGAIN) continue;
59 LOG("waitpid failed: %s\n", strerror(errno));
60 return -1;
61 } else if (n > 0) {
62 XLOG("waitpid: n=%d status=%08x\n", n, status);
63 if (WIFSTOPPED(status)) {
64 return WSTOPSIG(status);
Jeff Brown13e715b2011-10-21 12:14:56 -070065 } else {
Jeff Brown053b8652012-06-06 16:25:03 -070066 LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status);
67 return -1;
Jeff Brown13e715b2011-10-21 12:14:56 -070068 }
69 }
70
Jeff Brown053b8652012-06-06 16:25:03 -070071 if (*total_sleep_time_usec > max_total_sleep_usec) {
72 LOG("timed out waiting for tid=%d to die\n", tid);
73 return -1;
74 }
75
76 /* not ready yet */
77 XLOG("not ready yet\n");
78 usleep(sleep_time_usec);
79 *total_sleep_time_usec += sleep_time_usec;
Jeff Brown13e715b2011-10-21 12:14:56 -070080 }
81}
82
Jeff Brown053b8652012-06-06 16:25:03 -070083void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Jeff Brown13e715b2011-10-21 12:14:56 -070084 siginfo_t si;
Jeff Brown053b8652012-06-06 16:25:03 -070085 while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
86 if (*total_sleep_time_usec > max_total_sleep_usec) {
87 LOG("timed out waiting for tid=%d to stop\n", tid);
Jeff Brown13e715b2011-10-21 12:14:56 -070088 break;
89 }
90
Jeff Brown053b8652012-06-06 16:25:03 -070091 usleep(sleep_time_usec);
92 *total_sleep_time_usec += sleep_time_usec;
Jeff Brown13e715b2011-10-21 12:14:56 -070093 }
94}