AuxVector.cpp revision 263363
1185304Strasz//===-- AuxVector.cpp -------------------------------------------*- C++ -*-===//
2185304Strasz//
3196938Strasz//                     The LLVM Compiler Infrastructure
4196938Strasz//
5196938Strasz// This file is distributed under the University of Illinois Open Source
6196938Strasz// License. See LICENSE.TXT for details.
7196938Strasz//
8196938Strasz//===----------------------------------------------------------------------===//
9196938Strasz
10196938Strasz// C Includes
11196938Strasz#include <fcntl.h>
12196938Strasz#include <sys/stat.h>
13196938Strasz#include <sys/types.h>
14196938Strasz
15196938Strasz// C++ Includes
16196938Strasz// Other libraries and framework includes
17196938Strasz#include "lldb/Core/DataBufferHeap.h"
18196938Strasz#include "lldb/Core/DataExtractor.h"
19196938Strasz#include "lldb/Core/Log.h"
20196938Strasz#include "lldb/Target/Process.h"
21196938Strasz
22196938Strasz#if defined(__linux__) || defined(__FreeBSD__)
23196938Strasz#include "Plugins/Process/elf-core/ProcessElfCore.h"
24196938Strasz#endif
25196938Strasz
26196938Strasz#include "AuxVector.h"
27196938Strasz
28196938Straszusing namespace lldb;
29196938Straszusing namespace lldb_private;
30197436Strasz
31185304Straszstatic bool
32185304StraszGetMaxU64(DataExtractor &data,
33185304Strasz          lldb::offset_t *offset_ptr,
34185304Strasz          uint64_t *value,
35185304Strasz          unsigned int byte_size)
36185304Strasz{
37185304Strasz    lldb::offset_t saved_offset = *offset_ptr;
38185304Strasz    *value = data.GetMaxU64(offset_ptr, byte_size);
39185304Strasz    return *offset_ptr != saved_offset;
40288680Sngie}
41288680Sngie
42288680Sngiestatic bool
43288680SngieParseAuxvEntry(DataExtractor &data,
44288680Sngie               AuxVector::Entry &entry,
45288680Sngie               lldb::offset_t *offset_ptr,
46288680Sngie               unsigned int byte_size)
47288680Sngie{
48288680Sngie    if (!GetMaxU64(data, offset_ptr, &entry.type, byte_size))
49185304Strasz        return false;
50185304Strasz
51219266Strasz    if (!GetMaxU64(data, offset_ptr, &entry.value, byte_size))
52185304Strasz        return false;
53185304Strasz
54185304Strasz    return true;
55185304Strasz}
56185304Strasz
57288680SngieDataBufferSP
58185304StraszAuxVector::GetAuxvData()
59185304Strasz{
60185304Strasz#if defined(__linux__) || defined(__FreeBSD__)
61288680Sngie    if (m_process->GetPluginName() == ProcessElfCore::GetPluginNameStatic())
62185304Strasz        return static_cast<ProcessElfCore *>(m_process)->GetAuxvData();
63185304Strasz#endif
64185304Strasz    return lldb_private::Host::GetAuxvData(m_process);
65185304Strasz}
66185304Strasz
67185304Straszvoid
68185304StraszAuxVector::ParseAuxv(DataExtractor &data)
69185304Strasz{
70185304Strasz    const unsigned int byte_size  = m_process->GetAddressByteSize();
71185304Strasz    lldb::offset_t offset = 0;
72185304Strasz
73185304Strasz    for (;;)
74185304Strasz    {
75185304Strasz        Entry entry;
76185304Strasz
77185304Strasz        if (!ParseAuxvEntry(data, entry, &offset, byte_size))
78305227Sngie            break;
79185304Strasz
80185304Strasz        if (entry.type == AT_NULL)
81185304Strasz            break;
82185304Strasz
83185304Strasz        if (entry.type == AT_IGNORE)
84185304Strasz            continue;
85185304Strasz
86185304Strasz        m_auxv.push_back(entry);
87185304Strasz    }
88185304Strasz}
89
90AuxVector::AuxVector(Process *process)
91    : m_process(process)
92{
93    DataExtractor data;
94    Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
95
96    data.SetData(GetAuxvData());
97    data.SetByteOrder(m_process->GetByteOrder());
98    data.SetAddressByteSize(m_process->GetAddressByteSize());
99
100    ParseAuxv(data);
101
102    if (log)
103        DumpToLog(log);
104}
105
106AuxVector::iterator
107AuxVector::FindEntry(EntryType type) const
108{
109    for (iterator I = begin(); I != end(); ++I)
110    {
111        if (I->type == static_cast<uint64_t>(type))
112            return I;
113    }
114
115    return end();
116}
117
118void
119AuxVector::DumpToLog(Log *log) const
120{
121    if (!log)
122        return;
123
124    log->PutCString("AuxVector: ");
125    for (iterator I = begin(); I != end(); ++I)
126    {
127        log->Printf("   %s [%" PRIu64 "]: %" PRIx64, GetEntryName(*I), I->type, I->value);
128    }
129}
130
131const char *
132AuxVector::GetEntryName(EntryType type)
133{
134    const char *name = "AT_???";
135
136#define ENTRY_NAME(_type) _type: name = #_type
137    switch (type)
138    {
139    case ENTRY_NAME(AT_NULL);           break;
140    case ENTRY_NAME(AT_IGNORE);         break;
141    case ENTRY_NAME(AT_EXECFD);         break;
142    case ENTRY_NAME(AT_PHDR);           break;
143    case ENTRY_NAME(AT_PHENT);          break;
144    case ENTRY_NAME(AT_PHNUM);          break;
145    case ENTRY_NAME(AT_PAGESZ);         break;
146    case ENTRY_NAME(AT_BASE);           break;
147    case ENTRY_NAME(AT_FLAGS);          break;
148    case ENTRY_NAME(AT_ENTRY);          break;
149    case ENTRY_NAME(AT_NOTELF);         break;
150    case ENTRY_NAME(AT_UID);            break;
151    case ENTRY_NAME(AT_EUID);           break;
152    case ENTRY_NAME(AT_GID);            break;
153    case ENTRY_NAME(AT_EGID);           break;
154    case ENTRY_NAME(AT_CLKTCK);         break;
155    case ENTRY_NAME(AT_PLATFORM);       break;
156    case ENTRY_NAME(AT_HWCAP);          break;
157    case ENTRY_NAME(AT_FPUCW);          break;
158    case ENTRY_NAME(AT_DCACHEBSIZE);    break;
159    case ENTRY_NAME(AT_ICACHEBSIZE);    break;
160    case ENTRY_NAME(AT_UCACHEBSIZE);    break;
161    case ENTRY_NAME(AT_IGNOREPPC);      break;
162    case ENTRY_NAME(AT_SECURE);         break;
163    case ENTRY_NAME(AT_BASE_PLATFORM);  break;
164    case ENTRY_NAME(AT_RANDOM);         break;
165    case ENTRY_NAME(AT_EXECFN);         break;
166    case ENTRY_NAME(AT_SYSINFO);        break;
167    case ENTRY_NAME(AT_SYSINFO_EHDR);   break;
168    case ENTRY_NAME(AT_L1I_CACHESHAPE); break;
169    case ENTRY_NAME(AT_L1D_CACHESHAPE); break;
170    case ENTRY_NAME(AT_L2_CACHESHAPE);  break;
171    case ENTRY_NAME(AT_L3_CACHESHAPE);  break;
172    }
173#undef ENTRY_NAME
174
175    return name;
176}
177
178