| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 1 | /* | 
|  | 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> | 
|  | 18 | #include <unistd.h> | 
|  | 19 |  | 
|  | 20 | #include <cutils/debugger.h> | 
|  | 21 | #include <cutils/sockets.h> | 
|  | 22 |  | 
|  | 23 | int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) { | 
|  | 24 | int s = socket_local_client(DEBUGGER_SOCKET_NAME, | 
|  | 25 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); | 
|  | 26 | if (s < 0) { | 
|  | 27 | return -1; | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | debugger_msg_t msg; | 
|  | 31 | msg.tid = tid; | 
|  | 32 | msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE; | 
| Christopher Ferris | 1da83c3 | 2014-01-10 14:46:03 -0800 | [diff] [blame] | 33 | msg.abort_msg_address = 0; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | int result = 0; | 
|  | 36 | if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) { | 
|  | 37 | result = -1; | 
|  | 38 | } else { | 
|  | 39 | char ack; | 
|  | 40 | if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) { | 
|  | 41 | result = -1; | 
|  | 42 | } else { | 
|  | 43 | if (pathbuf && pathlen) { | 
|  | 44 | ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1)); | 
|  | 45 | if (n <= 0) { | 
|  | 46 | result = -1; | 
|  | 47 | } else { | 
|  | 48 | pathbuf[n] = '\0'; | 
|  | 49 | } | 
|  | 50 | } | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 | TEMP_FAILURE_RETRY(close(s)); | 
|  | 54 | return result; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | int dump_backtrace_to_file(pid_t tid, int fd) { | 
|  | 58 | int s = socket_local_client(DEBUGGER_SOCKET_NAME, | 
|  | 59 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); | 
|  | 60 | if (s < 0) { | 
|  | 61 | return -1; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | debugger_msg_t msg; | 
|  | 65 | msg.tid = tid; | 
|  | 66 | msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE; | 
| Christopher Ferris | 1da83c3 | 2014-01-10 14:46:03 -0800 | [diff] [blame] | 67 | msg.abort_msg_address = 0; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | int result = 0; | 
|  | 70 | if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) { | 
|  | 71 | result = -1; | 
|  | 72 | } else { | 
|  | 73 | char ack; | 
|  | 74 | if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) { | 
|  | 75 | result = -1; | 
|  | 76 | } else { | 
|  | 77 | char buffer[4096]; | 
|  | 78 | ssize_t n; | 
|  | 79 | while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) { | 
|  | 80 | if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) { | 
|  | 81 | result = -1; | 
|  | 82 | break; | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 | TEMP_FAILURE_RETRY(close(s)); | 
|  | 88 | return result; | 
|  | 89 | } |