blob: bb3db11663b45f5a24163c5163927ea0d44f93e0 [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,
29 uint32_t *offset, uint64_t *value, unsigned int byte_size)
30{
31 uint32_t saved_offset = *offset;
32 *value = data.GetMaxU64(offset, byte_size);
33 return *offset != saved_offset;
34}
35
36static bool
37ParseAuxvEntry(DataExtractor &data, AuxVector::Entry &entry,
38 uint32_t *offset, unsigned int byte_size)
39{
40 if (!GetMaxU64(data, offset, &entry.type, byte_size))
41 return false;
42
43 if (!GetMaxU64(data, offset, &entry.value, byte_size))
44 return false;
45
46 return true;
47}
48
49DataBufferSP
50AuxVector::GetAuxvData()
51{
52
53 return lldb_private::Host::GetAuxvData(m_process);
54}
55
56void
57AuxVector::ParseAuxv(DataExtractor &data)
58{
59 const unsigned int byte_size = m_process->GetAddressByteSize();
60 uint32_t offset = 0;
61
62 for (;;)
63 {
64 Entry entry;
65
66 if (!ParseAuxvEntry(data, entry, &offset, byte_size))
67 break;
68
69 if (entry.type == AT_NULL)
70 break;
71
72 if (entry.type == AT_IGNORE)
73 continue;
74
75 m_auxv.push_back(entry);
76 }
77}
78
79AuxVector::AuxVector(Process *process)
80 : m_process(process)
81{
82 DataExtractor data;
83 LogSP log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
84
85 data.SetData(GetAuxvData());
86 data.SetByteOrder(m_process->GetByteOrder());
87 data.SetAddressByteSize(m_process->GetAddressByteSize());
88
89 ParseAuxv(data);
90
91 if (log)
92 DumpToLog(log);
93}
94
95AuxVector::iterator
96AuxVector::FindEntry(EntryType type) const
97{
98 for (iterator I = begin(); I != end(); ++I)
99 {
100 if (I->type == static_cast<uint64_t>(type))
101 return I;
102 }
103
104 return end();
105}
106
107void
108AuxVector::DumpToLog(LogSP log) const
109{
110 if (!log)
111 return;
112
113 log->PutCString("AuxVector: ");
114 for (iterator I = begin(); I != end(); ++I)
115 {
Daniel Malead01b2952012-11-29 21:49:15 +0000116 log->Printf(" %s [%" PRIu64 "]: %" PRIx64, GetEntryName(*I), I->type, I->value);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000117 }
118}
119
120const char *
121AuxVector::GetEntryName(EntryType type)
122{
123 const char *name;
124
125#define ENTRY_NAME(_type) _type: name = #_type
126 switch (type)
127 {
128 default:
129 name = "unkown";
130 break;
131
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