blob: 3ec67fe52d84e2469856c3063a5df781c321f41c [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- 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
Ashok Thirumurthic0373832013-07-12 21:25:02 +000022#include "Plugins/Process/elf-core/ProcessElfCore.h"
23
Johnny Chen9ed5b492012-01-05 21:48:15 +000024#include "AuxVector.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29static bool
30GetMaxU64(DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +000031 lldb::offset_t *offset_ptr,
32 uint64_t *value,
33 unsigned int byte_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +000034{
Greg Claytonc7bece562013-01-25 18:06:21 +000035 lldb::offset_t saved_offset = *offset_ptr;
36 *value = data.GetMaxU64(offset_ptr, byte_size);
37 return *offset_ptr != saved_offset;
Johnny Chen9ed5b492012-01-05 21:48:15 +000038}
39
40static bool
Greg Claytonc7bece562013-01-25 18:06:21 +000041ParseAuxvEntry(DataExtractor &data,
42 AuxVector::Entry &entry,
43 lldb::offset_t *offset_ptr,
44 unsigned int byte_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +000045{
Greg Claytonc7bece562013-01-25 18:06:21 +000046 if (!GetMaxU64(data, offset_ptr, &entry.type, byte_size))
Johnny Chen9ed5b492012-01-05 21:48:15 +000047 return false;
48
Greg Claytonc7bece562013-01-25 18:06:21 +000049 if (!GetMaxU64(data, offset_ptr, &entry.value, byte_size))
Johnny Chen9ed5b492012-01-05 21:48:15 +000050 return false;
51
52 return true;
53}
54
55DataBufferSP
56AuxVector::GetAuxvData()
57{
Ashok Thirumurthic0373832013-07-12 21:25:02 +000058 if (m_process->GetPluginName() == ProcessElfCore::GetPluginNameStatic())
59 return static_cast<ProcessElfCore *>(m_process)->GetAuxvData();
60 else
61 return lldb_private::Host::GetAuxvData(m_process);
Johnny Chen9ed5b492012-01-05 21:48:15 +000062}
63
64void
65AuxVector::ParseAuxv(DataExtractor &data)
66{
67 const unsigned int byte_size = m_process->GetAddressByteSize();
Greg Claytonc7bece562013-01-25 18:06:21 +000068 lldb::offset_t offset = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +000069
70 for (;;)
71 {
72 Entry entry;
73
74 if (!ParseAuxvEntry(data, entry, &offset, byte_size))
75 break;
76
77 if (entry.type == AT_NULL)
78 break;
79
80 if (entry.type == AT_IGNORE)
81 continue;
82
83 m_auxv.push_back(entry);
84 }
85}
86
87AuxVector::AuxVector(Process *process)
88 : m_process(process)
89{
90 DataExtractor data;
Greg Clayton5160ce52013-03-27 23:08:40 +000091 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Johnny Chen9ed5b492012-01-05 21:48:15 +000092
93 data.SetData(GetAuxvData());
94 data.SetByteOrder(m_process->GetByteOrder());
95 data.SetAddressByteSize(m_process->GetAddressByteSize());
96
97 ParseAuxv(data);
98
99 if (log)
100 DumpToLog(log);
101}
102
103AuxVector::iterator
104AuxVector::FindEntry(EntryType type) const
105{
106 for (iterator I = begin(); I != end(); ++I)
107 {
108 if (I->type == static_cast<uint64_t>(type))
109 return I;
110 }
111
112 return end();
113}
114
115void
Greg Clayton5160ce52013-03-27 23:08:40 +0000116AuxVector::DumpToLog(Log *log) const
Johnny Chen9ed5b492012-01-05 21:48:15 +0000117{
118 if (!log)
119 return;
120
121 log->PutCString("AuxVector: ");
122 for (iterator I = begin(); I != end(); ++I)
123 {
Daniel Malead01b2952012-11-29 21:49:15 +0000124 log->Printf(" %s [%" PRIu64 "]: %" PRIx64, GetEntryName(*I), I->type, I->value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000125 }
126}
127
128const char *
129AuxVector::GetEntryName(EntryType type)
130{
Michael Sartainff5b4972013-06-15 00:25:52 +0000131 const char *name = "AT_???";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000132
133#define ENTRY_NAME(_type) _type: name = #_type
134 switch (type)
135 {
Michael Sartaind55eadf2013-06-17 17:44:53 +0000136 case ENTRY_NAME(AT_NULL); break;
137 case ENTRY_NAME(AT_IGNORE); break;
138 case ENTRY_NAME(AT_EXECFD); break;
139 case ENTRY_NAME(AT_PHDR); break;
140 case ENTRY_NAME(AT_PHENT); break;
141 case ENTRY_NAME(AT_PHNUM); break;
142 case ENTRY_NAME(AT_PAGESZ); break;
143 case ENTRY_NAME(AT_BASE); break;
144 case ENTRY_NAME(AT_FLAGS); break;
145 case ENTRY_NAME(AT_ENTRY); break;
146 case ENTRY_NAME(AT_NOTELF); break;
147 case ENTRY_NAME(AT_UID); break;
148 case ENTRY_NAME(AT_EUID); break;
149 case ENTRY_NAME(AT_GID); break;
150 case ENTRY_NAME(AT_EGID); break;
151 case ENTRY_NAME(AT_CLKTCK); break;
152 case ENTRY_NAME(AT_PLATFORM); break;
153 case ENTRY_NAME(AT_HWCAP); break;
154 case ENTRY_NAME(AT_FPUCW); break;
155 case ENTRY_NAME(AT_DCACHEBSIZE); break;
156 case ENTRY_NAME(AT_ICACHEBSIZE); break;
157 case ENTRY_NAME(AT_UCACHEBSIZE); break;
158 case ENTRY_NAME(AT_IGNOREPPC); break;
159 case ENTRY_NAME(AT_SECURE); break;
160 case ENTRY_NAME(AT_BASE_PLATFORM); break;
161 case ENTRY_NAME(AT_RANDOM); break;
162 case ENTRY_NAME(AT_EXECFN); break;
163 case ENTRY_NAME(AT_SYSINFO); break;
164 case ENTRY_NAME(AT_SYSINFO_EHDR); break;
165 case ENTRY_NAME(AT_L1I_CACHESHAPE); break;
166 case ENTRY_NAME(AT_L1D_CACHESHAPE); break;
167 case ENTRY_NAME(AT_L2_CACHESHAPE); break;
168 case ENTRY_NAME(AT_L3_CACHESHAPE); break;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000169 }
170#undef ENTRY_NAME
171
172 return name;
173}
174