Home | History | Annotate | Download | only in libdebuggerd
      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 #define LOG_TAG "DEBUG"
     18 
     19 #include "libdebuggerd/backtrace.h"
     20 
     21 #include <errno.h>
     22 #include <dirent.h>
     23 #include <limits.h>
     24 #include <stddef.h>
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <string.h>
     28 #include <sys/ptrace.h>
     29 #include <sys/types.h>
     30 #include <time.h>
     31 #include <unistd.h>
     32 
     33 #include <map>
     34 #include <memory>
     35 #include <string>
     36 
     37 #include <android-base/unique_fd.h>
     38 #include <backtrace/Backtrace.h>
     39 #include <log/log.h>
     40 
     41 #include "libdebuggerd/types.h"
     42 #include "libdebuggerd/utility.h"
     43 
     44 static void dump_process_header(log_t* log, pid_t pid, const char* process_name) {
     45   time_t t = time(NULL);
     46   struct tm tm;
     47   localtime_r(&t, &tm);
     48   char timestr[64];
     49   strftime(timestr, sizeof(timestr), "%F %T", &tm);
     50   _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
     51 
     52   if (process_name) {
     53     _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", process_name);
     54   }
     55   _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
     56 }
     57 
     58 static void dump_process_footer(log_t* log, pid_t pid) {
     59   _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
     60 }
     61 
     62 void dump_backtrace_thread(int output_fd, BacktraceMap* map, const ThreadInfo& thread) {
     63   log_t log;
     64   log.tfd = output_fd;
     65   log.amfd_data = nullptr;
     66 
     67   _LOG(&log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread.thread_name.c_str(), thread.tid);
     68 
     69   std::vector<backtrace_frame_data_t> frames;
     70   if (!Backtrace::Unwind(thread.registers.get(), map, &frames, 0, nullptr)) {
     71     _LOG(&log, logtype::THREAD, "Unwind failed: tid = %d", thread.tid);
     72     return;
     73   }
     74 
     75   for (auto& frame : frames) {
     76     _LOG(&log, logtype::BACKTRACE, "  %s\n", Backtrace::FormatFrameData(&frame).c_str());
     77   }
     78 }
     79 
     80 void dump_backtrace(android::base::unique_fd output_fd, BacktraceMap* map,
     81                     const std::map<pid_t, ThreadInfo>& thread_info, pid_t target_thread) {
     82   log_t log;
     83   log.tfd = output_fd.get();
     84   log.amfd_data = nullptr;
     85 
     86   auto target = thread_info.find(target_thread);
     87   if (target == thread_info.end()) {
     88     ALOGE("failed to find target thread in thread info");
     89     return;
     90   }
     91 
     92   dump_process_header(&log, target->second.pid, target->second.process_name.c_str());
     93 
     94   dump_backtrace_thread(output_fd.get(), map, target->second);
     95   for (const auto& [tid, info] : thread_info) {
     96     if (tid != target_thread) {
     97       dump_backtrace_thread(output_fd.get(), map, info);
     98     }
     99   }
    100 
    101   dump_process_footer(&log, target->second.pid);
    102 }
    103 
    104 void dump_backtrace_header(int output_fd) {
    105   log_t log;
    106   log.tfd = output_fd;
    107   log.amfd_data = nullptr;
    108 
    109   char process_name[128];
    110   read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
    111   dump_process_header(&log, getpid(), process_name);
    112 }
    113 
    114 void dump_backtrace_footer(int output_fd) {
    115   log_t log;
    116   log.tfd = output_fd;
    117   log.amfd_data = nullptr;
    118 
    119   dump_process_footer(&log, getpid());
    120 }
    121