Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1 | //===-- AuxVector.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
| 11 | #include <fcntl.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/types.h> |
| 14 | |
| 15 | // C++ Includes |
| 16 | // Other libraries and framework includes |
| 17 | #include "lldb/Core/DataBufferHeap.h" |
| 18 | #include "lldb/Core/DataExtractor.h" |
| 19 | #include "lldb/Core/Log.h" |
| 20 | #include "lldb/Target/Process.h" |
| 21 | |
| 22 | #include "AuxVector.h" |
| 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | static bool |
| 28 | GetMaxU64(DataExtractor &data, |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame^] | 29 | lldb::offset_t *offset_ptr, |
| 30 | uint64_t *value, |
| 31 | unsigned int byte_size) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 32 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame^] | 33 | lldb::offset_t saved_offset = *offset_ptr; |
| 34 | *value = data.GetMaxU64(offset_ptr, byte_size); |
| 35 | return *offset_ptr != saved_offset; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static bool |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame^] | 39 | ParseAuxvEntry(DataExtractor &data, |
| 40 | AuxVector::Entry &entry, |
| 41 | lldb::offset_t *offset_ptr, |
| 42 | unsigned int byte_size) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 43 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame^] | 44 | if (!GetMaxU64(data, offset_ptr, &entry.type, byte_size)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 45 | return false; |
| 46 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame^] | 47 | if (!GetMaxU64(data, offset_ptr, &entry.value, byte_size)) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 48 | return false; |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | DataBufferSP |
| 54 | AuxVector::GetAuxvData() |
| 55 | { |
| 56 | |
| 57 | return lldb_private::Host::GetAuxvData(m_process); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | AuxVector::ParseAuxv(DataExtractor &data) |
| 62 | { |
| 63 | const unsigned int byte_size = m_process->GetAddressByteSize(); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame^] | 64 | lldb::offset_t offset = 0; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 65 | |
| 66 | for (;;) |
| 67 | { |
| 68 | Entry entry; |
| 69 | |
| 70 | if (!ParseAuxvEntry(data, entry, &offset, byte_size)) |
| 71 | break; |
| 72 | |
| 73 | if (entry.type == AT_NULL) |
| 74 | break; |
| 75 | |
| 76 | if (entry.type == AT_IGNORE) |
| 77 | continue; |
| 78 | |
| 79 | m_auxv.push_back(entry); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | AuxVector::AuxVector(Process *process) |
| 84 | : m_process(process) |
| 85 | { |
| 86 | DataExtractor data; |
| 87 | LogSP log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 88 | |
| 89 | data.SetData(GetAuxvData()); |
| 90 | data.SetByteOrder(m_process->GetByteOrder()); |
| 91 | data.SetAddressByteSize(m_process->GetAddressByteSize()); |
| 92 | |
| 93 | ParseAuxv(data); |
| 94 | |
| 95 | if (log) |
| 96 | DumpToLog(log); |
| 97 | } |
| 98 | |
| 99 | AuxVector::iterator |
| 100 | AuxVector::FindEntry(EntryType type) const |
| 101 | { |
| 102 | for (iterator I = begin(); I != end(); ++I) |
| 103 | { |
| 104 | if (I->type == static_cast<uint64_t>(type)) |
| 105 | return I; |
| 106 | } |
| 107 | |
| 108 | return end(); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | AuxVector::DumpToLog(LogSP log) const |
| 113 | { |
| 114 | if (!log) |
| 115 | return; |
| 116 | |
| 117 | log->PutCString("AuxVector: "); |
| 118 | for (iterator I = begin(); I != end(); ++I) |
| 119 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 120 | log->Printf(" %s [%" PRIu64 "]: %" PRIx64, GetEntryName(*I), I->type, I->value); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | const char * |
| 125 | AuxVector::GetEntryName(EntryType type) |
| 126 | { |
| 127 | const char *name; |
| 128 | |
| 129 | #define ENTRY_NAME(_type) _type: name = #_type |
| 130 | switch (type) |
| 131 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 132 | case ENTRY_NAME(AT_NULL); break; |
| 133 | case ENTRY_NAME(AT_IGNORE); break; |
| 134 | case ENTRY_NAME(AT_EXECFD); break; |
| 135 | case ENTRY_NAME(AT_PHDR); break; |
| 136 | case ENTRY_NAME(AT_PHENT); break; |
| 137 | case ENTRY_NAME(AT_PHNUM); break; |
| 138 | case ENTRY_NAME(AT_PAGESZ); break; |
| 139 | case ENTRY_NAME(AT_BASE); break; |
| 140 | case ENTRY_NAME(AT_FLAGS); break; |
| 141 | case ENTRY_NAME(AT_ENTRY); break; |
| 142 | case ENTRY_NAME(AT_NOTELF); break; |
| 143 | case ENTRY_NAME(AT_UID); break; |
| 144 | case ENTRY_NAME(AT_EUID); break; |
| 145 | case ENTRY_NAME(AT_GID); break; |
| 146 | case ENTRY_NAME(AT_EGID); break; |
| 147 | case ENTRY_NAME(AT_CLKTCK); break; |
| 148 | } |
| 149 | #undef ENTRY_NAME |
| 150 | |
| 151 | return name; |
| 152 | } |
| 153 | |