blob: 056de5da83afb107c5e89eb48bce8277d297adad [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
17#include <stdlib.h>
Elliott Hughes855fcc32014-04-25 16:05:34 -070018#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070019#include <unistd.h>
20
21#include <cutils/debugger.h>
22#include <cutils/sockets.h>
23
24int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
25 int s = socket_local_client(DEBUGGER_SOCKET_NAME,
26 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
27 if (s < 0) {
28 return -1;
29 }
30
31 debugger_msg_t msg;
Elliott Hughes855fcc32014-04-25 16:05:34 -070032 memset(&msg, 0, sizeof(msg));
Jeff Brown053b8652012-06-06 16:25:03 -070033 msg.tid = tid;
34 msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE;
35
36 int result = 0;
37 if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
38 result = -1;
39 } else {
40 char ack;
41 if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
42 result = -1;
43 } else {
44 if (pathbuf && pathlen) {
45 ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1));
46 if (n <= 0) {
47 result = -1;
48 } else {
49 pathbuf[n] = '\0';
50 }
51 }
52 }
53 }
54 TEMP_FAILURE_RETRY(close(s));
55 return result;
56}
57
58int dump_backtrace_to_file(pid_t tid, int fd) {
59 int s = socket_local_client(DEBUGGER_SOCKET_NAME,
60 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
61 if (s < 0) {
62 return -1;
63 }
64
65 debugger_msg_t msg;
Elliott Hughes855fcc32014-04-25 16:05:34 -070066 memset(&msg, 0, sizeof(msg));
Jeff Brown053b8652012-06-06 16:25:03 -070067 msg.tid = tid;
68 msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE;
Jeff Brown053b8652012-06-06 16:25:03 -070069
70 int result = 0;
71 if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
72 result = -1;
73 } else {
74 char ack;
75 if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
76 result = -1;
77 } else {
78 char buffer[4096];
79 ssize_t n;
80 while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) {
81 if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
82 result = -1;
83 break;
84 }
85 }
86 }
87 }
88 TEMP_FAILURE_RETRY(close(s));
89 return result;
90}