blob: f5a36209d01dcf016c6606dd6b87a99318d17c8f [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
22#include "AuxVector.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27static bool
28GetMaxU64(DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +000029 lldb::offset_t *offset_ptr,
30 uint64_t *value,
31 unsigned int byte_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +000032{
Greg Claytonc7bece562013-01-25 18:06:21 +000033 lldb::offset_t saved_offset = *offset_ptr;
34 *value = data.GetMaxU64(offset_ptr, byte_size);
35 return *offset_ptr != saved_offset;
Johnny Chen9ed5b492012-01-05 21:48:15 +000036}
37
38static bool
Greg Claytonc7bece562013-01-25 18:06:21 +000039ParseAuxvEntry(DataExtractor &data,
40 AuxVector::Entry &entry,
41 lldb::offset_t *offset_ptr,
42 unsigned int byte_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +000043{
Greg Claytonc7bece562013-01-25 18:06:21 +000044 if (!GetMaxU64(data, offset_ptr, &entry.type, byte_size))
Johnny Chen9ed5b492012-01-05 21:48:15 +000045 return false;
46
Greg Claytonc7bece562013-01-25 18:06:21 +000047 if (!GetMaxU64(data, offset_ptr, &entry.value, byte_size))
Johnny Chen9ed5b492012-01-05 21:48:15 +000048 return false;
49
50 return true;
51}
52
53DataBufferSP
54AuxVector::GetAuxvData()
55{
56
57 return lldb_private::Host::GetAuxvData(m_process);
58}
59
60void
61AuxVector::ParseAuxv(DataExtractor &data)
62{
63 const unsigned int byte_size = m_process->GetAddressByteSize();
Greg Claytonc7bece562013-01-25 18:06:21 +000064 lldb::offset_t offset = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +000065
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
83AuxVector::AuxVector(Process *process)
84 : m_process(process)
85{
86 DataExtractor data;
Greg Clayton5160ce52013-03-27 23:08:40 +000087 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Johnny Chen9ed5b492012-01-05 21:48:15 +000088
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
99AuxVector::iterator
100AuxVector::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
111void
Greg Clayton5160ce52013-03-27 23:08:40 +0000112AuxVector::DumpToLog(Log *log) const
Johnny Chen9ed5b492012-01-05 21:48:15 +0000113{
114 if (!log)
115 return;
116
117 log->PutCString("AuxVector: ");
118 for (iterator I = begin(); I != end(); ++I)
119 {
Daniel Malead01b2952012-11-29 21:49:15 +0000120 log->Printf(" %s [%" PRIu64 "]: %" PRIx64, GetEntryName(*I), I->type, I->value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000121 }
122}
123
124const char *
125AuxVector::GetEntryName(EntryType type)
126{
Michael Sartainff5b4972013-06-15 00:25:52 +0000127 const char *name = "AT_???";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000128
129#define ENTRY_NAME(_type) _type: name = #_type
130 switch (type)
131 {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000132 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