2010-06-09 00:52:24 +08:00
|
|
|
//===-- ConstString.cpp -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lldb/Core/ConstString.h"
|
|
|
|
#include "lldb/Core/Stream.h"
|
|
|
|
#include "lldb/Host/Mutex.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
|
|
|
|
class Pool
|
|
|
|
{
|
|
|
|
public:
|
2011-06-10 06:34:34 +08:00
|
|
|
typedef const char * StringPoolValueType;
|
|
|
|
typedef llvm::StringMap<StringPoolValueType, llvm::BumpPtrAllocator> StringPool;
|
|
|
|
typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Default constructor
|
|
|
|
//
|
|
|
|
// Initialize the member variables and create the empty string.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Pool () :
|
|
|
|
m_mutex (Mutex::eMutexTypeRecursive),
|
|
|
|
m_string_map ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Destructor
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
~Pool ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-10 06:34:34 +08:00
|
|
|
static StringPoolEntryType &
|
2010-06-09 00:52:24 +08:00
|
|
|
GetStringMapEntryFromKeyData (const char *keyData)
|
|
|
|
{
|
2011-06-10 06:34:34 +08:00
|
|
|
char *ptr = const_cast<char*>(keyData) - sizeof (StringPoolEntryType);
|
|
|
|
return *reinterpret_cast<StringPoolEntryType*>(ptr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2011-06-10 06:34:34 +08:00
|
|
|
GetConstCStringLength (const char *ccstr) const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (ccstr)
|
|
|
|
{
|
2011-06-10 06:34:34 +08:00
|
|
|
const StringPoolEntryType&entry = GetStringMapEntryFromKeyData (ccstr);
|
2010-06-09 00:52:24 +08:00
|
|
|
return entry.getKey().size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-10 06:34:34 +08:00
|
|
|
StringPoolValueType
|
|
|
|
GetMangledCounterpart (const char *ccstr) const
|
|
|
|
{
|
|
|
|
if (ccstr)
|
|
|
|
return GetStringMapEntryFromKeyData (ccstr).getValue();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SetMangledCounterparts (const char *key_ccstr, const char *value_ccstr)
|
|
|
|
{
|
|
|
|
if (key_ccstr && value_ccstr)
|
|
|
|
{
|
|
|
|
GetStringMapEntryFromKeyData (key_ccstr).setValue(value_ccstr);
|
|
|
|
GetStringMapEntryFromKeyData (value_ccstr).setValue(key_ccstr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *
|
|
|
|
GetConstCString (const char *cstr)
|
|
|
|
{
|
|
|
|
if (cstr)
|
2010-06-22 23:28:34 +08:00
|
|
|
return GetConstCStringWithLength (cstr, strlen (cstr));
|
2010-06-09 00:52:24 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2013-01-26 02:06:21 +08:00
|
|
|
GetConstCStringWithLength (const char *cstr, size_t cstr_len)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (cstr)
|
|
|
|
{
|
|
|
|
Mutex::Locker locker (m_mutex);
|
|
|
|
llvm::StringRef string_ref (cstr, cstr_len);
|
2011-06-10 08:00:19 +08:00
|
|
|
StringPoolEntryType& entry = m_string_map.GetOrCreateValue (string_ref, (StringPoolValueType)NULL);
|
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
|
|
|
return entry.getKeyData();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
GetConstCStringWithStringRef (const llvm::StringRef &string_ref)
|
|
|
|
{
|
|
|
|
if (string_ref.data())
|
|
|
|
{
|
|
|
|
Mutex::Locker locker (m_mutex);
|
|
|
|
StringPoolEntryType& entry = m_string_map.GetOrCreateValue (string_ref, (StringPoolValueType)NULL);
|
2010-07-10 04:39:50 +08:00
|
|
|
return entry.getKeyData();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-10 06:34:34 +08:00
|
|
|
const char *
|
|
|
|
GetConstCStringAndSetMangledCounterPart (const char *demangled_cstr, const char *mangled_ccstr)
|
|
|
|
{
|
|
|
|
if (demangled_cstr)
|
|
|
|
{
|
|
|
|
Mutex::Locker locker (m_mutex);
|
|
|
|
// Make string pool entry with the mangled counterpart already set
|
|
|
|
StringPoolEntryType& entry = m_string_map.GetOrCreateValue (llvm::StringRef (demangled_cstr), mangled_ccstr);
|
|
|
|
|
|
|
|
// Extract the const version of the demangled_cstr
|
|
|
|
const char *demangled_ccstr = entry.getKeyData();
|
|
|
|
// Now assign the demangled const string as the counterpart of the
|
|
|
|
// mangled const string...
|
|
|
|
GetStringMapEntryFromKeyData (mangled_ccstr).setValue(demangled_ccstr);
|
|
|
|
// Return the constant demangled C string
|
|
|
|
return demangled_ccstr;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *
|
2013-01-26 02:06:21 +08:00
|
|
|
GetConstTrimmedCStringWithLength (const char *cstr, size_t cstr_len)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (cstr)
|
|
|
|
{
|
2013-01-26 02:06:21 +08:00
|
|
|
const size_t trimmed_len = std::min<size_t> (strlen (cstr), cstr_len);
|
2010-06-22 23:28:34 +08:00
|
|
|
return GetConstCStringWithLength (cstr, trimmed_len);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Return the size in bytes that this object and any items in its
|
2011-09-12 11:55:58 +08:00
|
|
|
// collection of uniqued strings + data count values takes in
|
2010-06-09 00:52:24 +08:00
|
|
|
// memory.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
MemorySize() const
|
|
|
|
{
|
|
|
|
Mutex::Locker locker (m_mutex);
|
|
|
|
size_t mem_size = sizeof(Pool);
|
|
|
|
const_iterator end = m_string_map.end();
|
|
|
|
for (const_iterator pos = m_string_map.begin(); pos != end; ++pos)
|
|
|
|
{
|
2011-06-10 06:34:34 +08:00
|
|
|
mem_size += sizeof(StringPoolEntryType) + pos->getKey().size();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return mem_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Typedefs
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
typedef StringPool::iterator iterator;
|
|
|
|
typedef StringPool::const_iterator const_iterator;
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Member variables
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
mutable Mutex m_mutex;
|
|
|
|
StringPool m_string_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Frameworks and dylibs aren't supposed to have global C++
|
|
|
|
// initializers so we hide the string pool in a static function so
|
|
|
|
// that it will get initialized on the first call to this static
|
|
|
|
// function.
|
2012-05-10 02:37:10 +08:00
|
|
|
//
|
|
|
|
// Note, for now we make the string pool a pointer to the pool, because
|
|
|
|
// we can't guarantee that some objects won't get destroyed after the
|
|
|
|
// global destructor chain is run, and trying to make sure no destructors
|
|
|
|
// touch ConstStrings is difficult. So we leak the pool instead.
|
|
|
|
//
|
|
|
|
// FIXME: If we are going to keep it this way we should come up with some
|
|
|
|
// abstraction to "pthread_once" so we don't have to check the pointer
|
|
|
|
// every time.
|
2010-06-09 00:52:24 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
static Pool &
|
|
|
|
StringPool()
|
|
|
|
{
|
2012-05-10 02:37:10 +08:00
|
|
|
static Mutex g_pool_initialization_mutex;
|
|
|
|
static Pool *g_string_pool = NULL;
|
|
|
|
|
|
|
|
if (g_string_pool == NULL)
|
|
|
|
{
|
|
|
|
Mutex::Locker initialization_locker(g_pool_initialization_mutex);
|
|
|
|
if (g_string_pool == NULL)
|
|
|
|
{
|
|
|
|
g_string_pool = new Pool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *g_string_pool;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstString::ConstString (const char *cstr) :
|
|
|
|
m_string (StringPool().GetConstCString (cstr))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstString::ConstString (const char *cstr, size_t cstr_len) :
|
|
|
|
m_string (StringPool().GetConstCStringWithLength (cstr, cstr_len))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-23 01:17:09 +08:00
|
|
|
ConstString::ConstString (const llvm::StringRef &s) :
|
|
|
|
m_string (StringPool().GetConstCStringWithLength (s.data(), s.size()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
bool
|
|
|
|
ConstString::operator < (const ConstString& rhs) const
|
|
|
|
{
|
|
|
|
if (m_string == rhs.m_string)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
llvm::StringRef lhs_string_ref (m_string, StringPool().GetConstCStringLength (m_string));
|
|
|
|
llvm::StringRef rhs_string_ref (rhs.m_string, StringPool().GetConstCStringLength (rhs.m_string));
|
|
|
|
|
|
|
|
// If both have valid C strings, then return the comparison
|
|
|
|
if (lhs_string_ref.data() && rhs_string_ref.data())
|
|
|
|
return lhs_string_ref < rhs_string_ref;
|
|
|
|
|
|
|
|
// Else one of them was NULL, so if LHS is NULL then it is less than
|
|
|
|
return lhs_string_ref.data() == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream&
|
|
|
|
lldb_private::operator << (Stream& s, const ConstString& str)
|
|
|
|
{
|
|
|
|
const char *cstr = str.GetCString();
|
|
|
|
if (cstr)
|
|
|
|
s << cstr;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ConstString::GetLength () const
|
|
|
|
{
|
|
|
|
return StringPool().GetConstCStringLength (m_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ConstString::Compare (const ConstString& lhs, const ConstString& rhs)
|
|
|
|
{
|
|
|
|
// If the iterators are the same, this is the same string
|
2013-08-31 01:50:57 +08:00
|
|
|
const char *lhs_cstr = lhs.m_string;
|
|
|
|
const char *rhs_cstr = rhs.m_string;
|
2010-06-09 00:52:24 +08:00
|
|
|
if (lhs_cstr == rhs_cstr)
|
|
|
|
return 0;
|
|
|
|
if (lhs_cstr && rhs_cstr)
|
|
|
|
{
|
|
|
|
llvm::StringRef lhs_string_ref (lhs_cstr, StringPool().GetConstCStringLength (lhs_cstr));
|
|
|
|
llvm::StringRef rhs_string_ref (rhs_cstr, StringPool().GetConstCStringLength (rhs_cstr));
|
|
|
|
return lhs_string_ref.compare(rhs_string_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs_cstr)
|
|
|
|
return +1; // LHS isn't NULL but RHS is
|
|
|
|
else
|
|
|
|
return -1; // LHS is NULL but RHS isn't
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstString::Dump(Stream *s, const char *fail_value) const
|
|
|
|
{
|
2012-11-03 08:09:46 +08:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
const char *cstr = AsCString (fail_value);
|
|
|
|
if (cstr)
|
|
|
|
s->PutCString (cstr);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstString::DumpDebug(Stream *s) const
|
|
|
|
{
|
|
|
|
const char *cstr = GetCString ();
|
|
|
|
size_t cstr_len = GetLength();
|
|
|
|
// Only print the parens if we have a non-NULL string
|
|
|
|
const char *parens = cstr ? "\"" : "";
|
2014-04-04 12:06:10 +08:00
|
|
|
s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
|
|
|
|
static_cast<int>(sizeof(void*) * 2),
|
|
|
|
static_cast<const void*>(this), parens, cstr, parens,
|
|
|
|
static_cast<uint64_t>(cstr_len));
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstString::SetCString (const char *cstr)
|
|
|
|
{
|
|
|
|
m_string = StringPool().GetConstCString (cstr);
|
|
|
|
}
|
|
|
|
|
2012-08-23 01:17:09 +08:00
|
|
|
void
|
|
|
|
ConstString::SetString (const llvm::StringRef &s)
|
|
|
|
{
|
|
|
|
m_string = StringPool().GetConstCStringWithLength (s.data(), s.size());
|
|
|
|
}
|
|
|
|
|
2011-06-10 06:34:34 +08:00
|
|
|
void
|
|
|
|
ConstString::SetCStringWithMangledCounterpart (const char *demangled, const ConstString &mangled)
|
|
|
|
{
|
|
|
|
m_string = StringPool().GetConstCStringAndSetMangledCounterPart (demangled, mangled.m_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConstString::GetMangledCounterpart (ConstString &counterpart) const
|
|
|
|
{
|
|
|
|
counterpart.m_string = StringPool().GetMangledCounterpart(m_string);
|
2013-10-04 06:27:29 +08:00
|
|
|
return (bool)counterpart;
|
2011-06-10 06:34:34 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void
|
|
|
|
ConstString::SetCStringWithLength (const char *cstr, size_t cstr_len)
|
|
|
|
{
|
|
|
|
m_string = StringPool().GetConstCStringWithLength(cstr, cstr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstString::SetTrimmedCStringWithLength (const char *cstr, size_t cstr_len)
|
|
|
|
{
|
|
|
|
m_string = StringPool().GetConstTrimmedCStringWithLength (cstr, cstr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ConstString::StaticMemorySize()
|
|
|
|
{
|
|
|
|
// Get the size of the static string pool
|
|
|
|
return StringPool().MemorySize();
|
|
|
|
}
|