upload android base code part6

This commit is contained in:
August 2018-08-08 17:48:24 +08:00
parent 421e214c7d
commit 4e516ec6ed
35396 changed files with 9188716 additions and 0 deletions

View file

@ -0,0 +1,162 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <elf.h>
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <unwindstack/Elf.h>
#include <unwindstack/MapInfo.h>
#include <unwindstack/Maps.h>
#include <unwindstack/Memory.h>
#include <unwindstack/Regs.h>
static bool Attach(pid_t pid) {
if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) {
return false;
}
// Allow at least 1 second to attach properly.
for (size_t i = 0; i < 1000; i++) {
siginfo_t si;
if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
return true;
}
usleep(1000);
}
printf("%d: Failed to stop.\n", pid);
return false;
}
static bool Detach(pid_t pid) {
return ptrace(PTRACE_DETACH, pid, 0, 0) == 0;
}
void DoUnwind(pid_t pid) {
unwindstack::RemoteMaps remote_maps(pid);
if (!remote_maps.Parse()) {
printf("Failed to parse map data.\n");
return;
}
uint32_t machine_type;
unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid, &machine_type);
if (regs == nullptr) {
printf("Unable to get remote reg data\n");
return;
}
bool bits32 = true;
printf("ABI: ");
switch (machine_type) {
case EM_ARM:
printf("arm");
break;
case EM_386:
printf("x86");
break;
case EM_AARCH64:
printf("arm64");
bits32 = false;
break;
case EM_X86_64:
printf("x86_64");
bits32 = false;
break;
default:
printf("unknown\n");
return;
}
printf("\n");
unwindstack::MemoryRemote remote_memory(pid);
for (size_t frame_num = 0; frame_num < 64; frame_num++) {
if (regs->pc() == 0) {
break;
}
unwindstack::MapInfo* map_info = remote_maps.Find(regs->pc());
if (map_info == nullptr) {
printf("Failed to find map data for the pc\n");
break;
}
unwindstack::Elf* elf = map_info->GetElf(pid, true);
uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
uint64_t adjusted_rel_pc = rel_pc;
// Don't need to adjust the first frame pc.
if (frame_num != 0) {
adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
}
std::string name;
if (bits32) {
printf(" #%02zu pc %08" PRIx64, frame_num, adjusted_rel_pc);
} else {
printf(" #%02zu pc %016" PRIx64, frame_num, adjusted_rel_pc);
}
if (!map_info->name.empty()) {
printf(" %s", map_info->name.c_str());
if (map_info->elf_offset != 0) {
printf(" (offset 0x%" PRIx64 ")", map_info->elf_offset);
}
} else {
printf(" <anonymous:%" PRIx64 ">", map_info->offset);
}
uint64_t func_offset;
if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) {
printf(" (%s", name.c_str());
if (func_offset != 0) {
printf("+%" PRId64, func_offset);
}
printf(")");
}
printf("\n");
if (!elf->Step(rel_pc + map_info->elf_offset, regs, &remote_memory)) {
break;
}
}
}
int main(int argc, char** argv) {
if (argc != 2) {
printf("Usage: unwind <PID>\n");
return 1;
}
pid_t pid = atoi(argv[1]);
if (!Attach(pid)) {
printf("Failed to attach to pid %d: %s\n", pid, strerror(errno));
return 1;
}
DoUnwind(pid);
Detach(pid);
return 0;
}

View file

@ -0,0 +1,184 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unwindstack/DwarfSection.h>
#include <unwindstack/DwarfStructs.h>
#include <unwindstack/Elf.h>
#include <unwindstack/ElfInterface.h>
#include <unwindstack/Log.h>
#include "ArmExidx.h"
#include "ElfInterfaceArm.h"
namespace unwindstack {
void DumpArm(ElfInterfaceArm* interface) {
if (interface == nullptr) {
printf("No ARM Unwind Information.\n\n");
return;
}
printf("ARM Unwind Information:\n");
for (const auto& entry : interface->pt_loads()) {
uint64_t load_bias = entry.second.table_offset;
printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
entry.second.table_size + load_bias);
for (auto addr : *interface) {
std::string name;
printf(" PC 0x%" PRIx64, addr + load_bias);
uint64_t func_offset;
uint64_t pc = addr + load_bias;
// This might be a thumb function, so set the low bit.
if (interface->GetFunctionName(pc | 1, &name, &func_offset) && !name.empty()) {
printf(" <%s>", name.c_str());
}
printf("\n");
uint64_t entry;
if (!interface->FindEntry(pc, &entry)) {
printf(" Cannot find entry for address.\n");
continue;
}
ArmExidx arm(nullptr, interface->memory(), nullptr);
arm.set_log(true);
arm.set_log_skip_execution(true);
arm.set_log_indent(2);
if (!arm.ExtractEntryData(entry)) {
if (arm.status() != ARM_STATUS_NO_UNWIND) {
printf(" Error trying to extract data.\n");
}
continue;
}
if (arm.data()->size() > 0) {
if (!arm.Eval() && arm.status() != ARM_STATUS_NO_UNWIND) {
printf(" Error trying to evaluate dwarf data.\n");
}
}
}
}
printf("\n");
}
void DumpDwarfSection(ElfInterface* interface, DwarfSection* section, uint64_t load_bias) {
for (const DwarfFde* fde : *section) {
// Sometimes there are entries that have empty length, skip those since
// they don't contain any interesting information.
if (fde->pc_start == fde->pc_end) {
continue;
}
printf("\n PC 0x%" PRIx64, fde->pc_start + load_bias);
std::string name;
uint64_t func_offset;
if (interface->GetFunctionName(fde->pc_start + load_bias, &name, &func_offset) &&
!name.empty()) {
printf(" <%s>", name.c_str());
}
printf("\n");
if (!section->Log(2, UINT64_MAX, load_bias, fde)) {
printf("Failed to process cfa information for entry at 0x%" PRIx64 "\n", fde->pc_start);
}
}
}
int GetElfInfo(const char* file) {
// Send all log messages to stdout.
log_to_stdout(true);
MemoryFileAtOffset* memory = new MemoryFileAtOffset;
if (!memory->Init(file, 0)) {
// Initializatation failed.
printf("Failed to init\n");
return 1;
}
Elf elf(memory);
if (!elf.Init() || !elf.valid()) {
printf("%s is not a valid elf file.\n", file);
return 1;
}
ElfInterface* interface = elf.interface();
if (elf.machine_type() == EM_ARM) {
DumpArm(reinterpret_cast<ElfInterfaceArm*>(interface));
printf("\n");
}
if (interface->eh_frame() != nullptr) {
printf("eh_frame information:\n");
DumpDwarfSection(interface, interface->eh_frame(), interface->load_bias());
printf("\n");
} else {
printf("\nno eh_frame information\n");
}
if (interface->debug_frame() != nullptr) {
printf("\ndebug_frame information:\n");
DumpDwarfSection(interface, interface->debug_frame(), interface->load_bias());
printf("\n");
} else {
printf("\nno debug_frame information\n");
}
// If there is a gnu_debugdata interface, dump the information for that.
ElfInterface* gnu_debugdata_interface = elf.gnu_debugdata_interface();
if (gnu_debugdata_interface != nullptr) {
if (gnu_debugdata_interface->eh_frame() != nullptr) {
printf("\ngnu_debugdata (eh_frame):\n");
DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->eh_frame(), 0);
printf("\n");
}
if (gnu_debugdata_interface->debug_frame() != nullptr) {
printf("\ngnu_debugdata (debug_frame):\n");
DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->debug_frame(), 0);
printf("\n");
}
} else {
printf("\nno valid gnu_debugdata information\n");
}
return 0;
}
} // namespace unwindstack
int main(int argc, char** argv) {
if (argc != 2) {
printf("Need to pass the name of an elf file to the program.\n");
return 1;
}
struct stat st;
if (stat(argv[1], &st) == -1) {
printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
return 1;
}
if (!S_ISREG(st.st_mode)) {
printf("%s is not a regular file.\n", argv[1]);
return 1;
}
return unwindstack::GetElfInfo(argv[1]);
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <elf.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unwindstack/Elf.h>
#include <unwindstack/Log.h>
#include <unwindstack/Memory.h>
int main(int argc, char** argv) {
if (argc != 2) {
printf("Need to pass the name of an elf file to the program.\n");
return 1;
}
struct stat st;
if (stat(argv[1], &st) == -1) {
printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
return 1;
}
if (!S_ISREG(st.st_mode)) {
printf("%s is not a regular file.\n", argv[1]);
return 1;
}
// Send all log messages to stdout.
unwindstack::log_to_stdout(true);
unwindstack::MemoryFileAtOffset* memory = new unwindstack::MemoryFileAtOffset;
if (!memory->Init(argv[1], 0)) {
printf("Failed to init\n");
return 1;
}
unwindstack::Elf elf(memory);
if (!elf.Init() || !elf.valid()) {
printf("%s is not a valid elf file.\n", argv[1]);
return 1;
}
switch (elf.machine_type()) {
case EM_ARM:
printf("ABI: arm\n");
break;
case EM_AARCH64:
printf("ABI: arm64\n");
break;
case EM_386:
printf("ABI: x86\n");
break;
case EM_X86_64:
printf("ABI: x86_64\n");
break;
default:
printf("ABI: unknown\n");
return 1;
}
// This is a crude way to get the symbols in order.
std::string name;
uint64_t load_bias = elf.interface()->load_bias();
for (const auto& entry : elf.interface()->pt_loads()) {
uint64_t start = entry.second.offset + load_bias;
uint64_t end = entry.second.table_size + load_bias;
for (uint64_t addr = start; addr < end; addr += 4) {
std::string cur_name;
uint64_t func_offset;
if (elf.GetFunctionName(addr, &cur_name, &func_offset)) {
if (cur_name != name) {
printf("<0x%" PRIx64 "> Function: %s\n", addr - func_offset, cur_name.c_str());
}
name = cur_name;
}
}
}
return 0;
}