blob: 4035ee1604074289ccb3cc1f2423886eff68f51b [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
2 * Copyright (C) 2012 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
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -070017#include <stdbool.h>
18#include <fcntl.h>
19#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070020#include <stdlib.h>
Elliott Hughes855fcc32014-04-25 16:05:34 -070021#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <unistd.h>
23
24#include <cutils/debugger.h>
25#include <cutils/sockets.h>
26
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -070027#if defined(__LP64__)
28#include <elf.h>
Jeff Brown053b8652012-06-06 16:25:03 -070029
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -070030static bool is32bit(pid_t tid) {
31 char* exeline;
32 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
33 return false;
34 }
35 int fd = open(exeline, O_RDONLY | O_CLOEXEC);
36 free(exeline);
37 if (fd == -1) {
38 return false;
39 }
40
41 char ehdr[EI_NIDENT];
42 ssize_t bytes = read(fd, &ehdr, sizeof(ehdr));
43 close(fd);
44 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
45 return false;
46 }
47 if (ehdr[EI_CLASS] == ELFCLASS32) {
48 return true;
49 }
50 return false;
51}
52#endif
53
54static int send_request(int sock_fd, void* msg_ptr, size_t msg_len) {
55 int result = 0;
56 if (TEMP_FAILURE_RETRY(write(sock_fd, msg_ptr, msg_len)) != (ssize_t) msg_len) {
57 result = -1;
58 } else {
59 char ack;
60 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) != 1) {
61 result = -1;
62 }
63 }
64 return result;
65}
66
67static int make_dump_request(debugger_action_t action, pid_t tid) {
68 const char* socket_name;
69 debugger_msg_t msg;
70 size_t msg_len;
71 void* msg_ptr;
72
73#if defined(__LP64__)
74 debugger32_msg_t msg32;
75 if (is32bit(tid)) {
76 msg_len = sizeof(debugger32_msg_t);
77 memset(&msg32, 0, msg_len);
78 msg32.tid = tid;
79 msg32.action = action;
80 msg_ptr = &msg32;
81
82 socket_name = DEBUGGER32_SOCKET_NAME;
83 } else
84#endif
85 {
86 msg_len = sizeof(debugger_msg_t);
87 memset(&msg, 0, msg_len);
Jeff Brown053b8652012-06-06 16:25:03 -070088 msg.tid = tid;
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -070089 msg.action = action;
90 msg_ptr = &msg;
Jeff Brown053b8652012-06-06 16:25:03 -070091
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -070092 socket_name = DEBUGGER_SOCKET_NAME;
93 }
94
95 int sock_fd = socket_local_client(socket_name, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
96 SOCK_STREAM | SOCK_CLOEXEC);
97 if (sock_fd < 0) {
98 return -1;
99 }
100
101 if (send_request(sock_fd, msg_ptr, msg_len) < 0) {
102 TEMP_FAILURE_RETRY(close(sock_fd));
103 return -1;
104 }
105
106 return sock_fd;
Jeff Brown053b8652012-06-06 16:25:03 -0700107}
108
109int dump_backtrace_to_file(pid_t tid, int fd) {
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -0700110 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_BACKTRACE, tid);
111 if (sock_fd < 0) {
112 return -1;
113 }
Jeff Brown053b8652012-06-06 16:25:03 -0700114
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -0700115 /* Write the data read from the socket to the fd. */
116 int result = 0;
117 char buffer[1024];
118 ssize_t n;
119 while ((n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
120 if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
121 result = -1;
122 break;
Jeff Brown053b8652012-06-06 16:25:03 -0700123 }
Christopher Ferrisa9fa7b82014-09-12 14:44:20 -0700124 }
125 TEMP_FAILURE_RETRY(close(sock_fd));
126 return result;
127}
128
129int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
130 int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_TOMBSTONE, tid);
131 if (sock_fd < 0) {
132 return -1;
133 }
134
135 /* Read the tombstone file name. */
136 char buffer[100]; /* This is larger than the largest tombstone path. */
137 int result = 0;
138 ssize_t n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer) - 1));
139 if (n <= 0) {
140 result = -1;
141 } else {
142 if (pathbuf && pathlen) {
143 if (n >= (ssize_t) pathlen) {
144 n = pathlen - 1;
145 }
146 buffer[n] = '\0';
147 memcpy(pathbuf, buffer, n + 1);
148 }
149 }
150 TEMP_FAILURE_RETRY(close(sock_fd));
151 return result;
Jeff Brown053b8652012-06-06 16:25:03 -0700152}