debugger.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <cutils/debugger.h>
  19. #include <cutils/sockets.h>
  20. int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
  21. int s = socket_local_client(DEBUGGER_SOCKET_NAME,
  22. ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
  23. if (s < 0) {
  24. return -1;
  25. }
  26. debugger_msg_t msg;
  27. msg.tid = tid;
  28. msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE;
  29. int result = 0;
  30. if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
  31. result = -1;
  32. } else {
  33. char ack;
  34. if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
  35. result = -1;
  36. } else {
  37. if (pathbuf && pathlen) {
  38. ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1));
  39. if (n <= 0) {
  40. result = -1;
  41. } else {
  42. pathbuf[n] = '\0';
  43. }
  44. }
  45. }
  46. }
  47. TEMP_FAILURE_RETRY(close(s));
  48. return result;
  49. }
  50. int dump_backtrace_to_file(pid_t tid, int fd) {
  51. int s = socket_local_client(DEBUGGER_SOCKET_NAME,
  52. ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
  53. if (s < 0) {
  54. return -1;
  55. }
  56. debugger_msg_t msg;
  57. msg.tid = tid;
  58. msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE;
  59. int result = 0;
  60. if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
  61. result = -1;
  62. } else {
  63. char ack;
  64. if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
  65. result = -1;
  66. } else {
  67. char buffer[4096];
  68. ssize_t n;
  69. while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) {
  70. if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
  71. result = -1;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. TEMP_FAILURE_RETRY(close(s));
  78. return result;
  79. }