blob: 04a6792fbf01bfee2ca5afeb2263556181285ef2 [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
Deepak Panickald66b50c2013-10-22 12:27:43 +000022#if defined(__linux__) || defined(__FreeBSD__)
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +000023#include "Plugins/Process/elf-core/ProcessElfCore.h"
24#endif
25
Johnny Chen9ed5b492012-01-05 21:48:15 +000026#include "AuxVector.h"
27
28using namespace lldb;
29using namespace lldb_private;
30
31static bool
32GetMaxU64(DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +000033 lldb::offset_t *offset_ptr,
34 uint64_t *value,
35 unsigned int byte_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +000036{
Greg Claytonc7bece562013-01-25 18:06:21 +000037 lldb::offset_t saved_offset = *offset_ptr;
38 *value = data.GetMaxU64(offset_ptr, byte_size);
39 return *offset_ptr != saved_offset;
Johnny Chen9ed5b492012-01-05 21:48:15 +000040}
41
42static bool
Greg Claytonc7bece562013-01-25 18:06:21 +000043ParseAuxvEntry(DataExtractor &data,
44 AuxVector::Entry &entry,
45 lldb::offset_t *offset_ptr,
46 unsigned int byte_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +000047{
Greg Claytonc7bece562013-01-25 18:06:21 +000048 if (!GetMaxU64(data, offset_ptr, &entry.type, byte_size))
Johnny Chen9ed5b492012-01-05 21:48:15 +000049 return false;
50
Greg Claytonc7bece562013-01-25 18:06:21 +000051 if (!GetMaxU64(data, offset_ptr, &entry.value, byte_size))
Johnny Chen9ed5b492012-01-05 21:48:15 +000052 return false;
53
54 return true;
55}
56
57DataBufferSP
58AuxVector::GetAuxvData()
59{
Todd Fialaaf245d12014-06-30 21:05:18 +000060 if (m_process)
61 return m_process->GetAuxvData ();
62 else
63 return DataBufferSP ();
Johnny Chen9ed5b492012-01-05 21:48:15 +000064}
65
66void
67AuxVector::ParseAuxv(DataExtractor &data)
68{
69 const unsigned int byte_size = m_process->GetAddressByteSize();
Greg Claytonc7bece562013-01-25 18:06:21 +000070 lldb::offset_t offset = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +000071
72 for (;;)
73 {
74 Entry entry;
75
76 if (!ParseAuxvEntry(data, entry, &offset, byte_size))
77 break;
78
79 if (entry.type == AT_NULL)
80 break;
81
82 if (entry.type == AT_IGNORE)
83 continue;
84
85 m_auxv.push_back(entry);
86 }
87}
88
89AuxVector::AuxVector(Process *process)
90 : m_process(process)
91{
92 DataExtractor data;
Greg Clayton5160ce52013-03-27 23:08:40 +000093 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Johnny Chen9ed5b492012-01-05 21:48:15 +000094
95 data.SetData(GetAuxvData());
96 data.SetByteOrder(m_process->GetByteOrder());
97 data.SetAddressByteSize(m_process->GetAddressByteSize());
98
99 ParseAuxv(data);
100
101 if (log)
102 DumpToLog(log);
103}
104
105AuxVector::iterator
106AuxVector::FindEntry(EntryType type) const
107{
108 for (iterator I = begin(); I != end(); ++I)
109 {
110 if (I->type == static_cast<uint64_t>(type))
111 return I;
112 }
113
114 return end();
115}
116
117void
Greg Clayton5160ce52013-03-27 23:08:40 +0000118AuxVector::DumpToLog(Log *log) const
Johnny Chen9ed5b492012-01-05 21:48:15 +0000119{
120 if (!log)
121 return;
122
123 log->PutCString("AuxVector: ");
124 for (iterator I = begin(); I != end(); ++I)
125 {
Daniel Malead01b2952012-11-29 21:49:15 +0000126 log->Printf(" %s [%" PRIu64 "]: %" PRIx64, GetEntryName(*I), I->type, I->value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000127 }
128}
129
130const char *
131AuxVector::GetEntryName(EntryType type)
132{
Michael Sartainff5b4972013-06-15 00:25:52 +0000133 const char *name = "AT_???";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000134
135#define ENTRY_NAME(_type) _type: name = #_type
136 switch (type)
137 {
Michael Sartaind55eadf2013-06-17 17:44:53 +0000138 case ENTRY_NAME(AT_NULL); break;
139 case ENTRY_NAME(AT_IGNORE); break;
140 case ENTRY_NAME(AT_EXECFD); break;
141 case ENTRY_NAME(AT_PHDR); break;
142 case ENTRY_NAME(AT_PHENT); break;
143 case ENTRY_NAME(AT_PHNUM); break;
144 case ENTRY_NAME(AT_PAGESZ); break;
145 case ENTRY_NAME(AT_BASE); break;
146 case ENTRY_NAME(AT_FLAGS); break;
147 case ENTRY_NAME(AT_ENTRY); break;
148 case ENTRY_NAME(AT_NOTELF); break;
149 case ENTRY_NAME(AT_UID); break;
150 case ENTRY_NAME(AT_EUID); break;
151 case ENTRY_NAME(AT_GID); break;
152 case ENTRY_NAME(AT_EGID); break;
153 case ENTRY_NAME(AT_CLKTCK); break;
154 case ENTRY_NAME(AT_PLATFORM); break;
155 case ENTRY_NAME(AT_HWCAP); break;
156 case ENTRY_NAME(AT_FPUCW); break;
157 case ENTRY_NAME(AT_DCACHEBSIZE); break;
158 case ENTRY_NAME(AT_ICACHEBSIZE); break;
159 case ENTRY_NAME(AT_UCACHEBSIZE); break;
160 case ENTRY_NAME(AT_IGNOREPPC); break;
161 case ENTRY_NAME(AT_SECURE); break;
162 case ENTRY_NAME(AT_BASE_PLATFORM); break;
163 case ENTRY_NAME(AT_RANDOM); break;
164 case ENTRY_NAME(AT_EXECFN); break;
165 case ENTRY_NAME(AT_SYSINFO); break;
166 case ENTRY_NAME(AT_SYSINFO_EHDR); break;
167 case ENTRY_NAME(AT_L1I_CACHESHAPE); break;
168 case ENTRY_NAME(AT_L1D_CACHESHAPE); break;
169 case ENTRY_NAME(AT_L2_CACHESHAPE); break;
170 case ENTRY_NAME(AT_L3_CACHESHAPE); break;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000171 }
172#undef ENTRY_NAME
173
174 return name;
175}
176