Skip to content

Commit 8087ca2

Browse files
author
Greg Clayton
committed
Added mutex protection to the Symtab class.
Added a new SortOrder enumeration and hooked it up to the "image dump symtab" command so we can dump symbol tables in the original order, sorted by address, or sorted by name. llvm-svn: 116049
1 parent bb6a881 commit 8087ca2

File tree

7 files changed

+352
-84
lines changed

7 files changed

+352
-84
lines changed

lldb/include/lldb/Symbol/Symtab.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "lldb/lldb-private.h"
1717
#include "lldb/Core/UniqueCStringMap.h"
18+
#include "lldb/Host/Mutex.h"
1819
#include "lldb/Symbol/Symbol.h"
1920

2021
namespace lldb_private {
@@ -41,9 +42,13 @@ class Symtab
4142
Symbol * Resize (uint32_t count);
4243
uint32_t AddSymbol(const Symbol& symbol);
4344
size_t GetNumSymbols() const;
44-
void Dump(Stream *s, Target *target) const;
45+
void Dump(Stream *s, Target *target, lldb::SortOrder sort_type);
4546
void Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const;
4647
uint32_t GetIndexForSymbol (const Symbol *symbol) const;
48+
Mutex & GetMutex ()
49+
{
50+
return m_mutex;
51+
}
4752
Symbol * FindSymbolByID (lldb::user_id_t uid) const;
4853
Symbol * SymbolAtIndex (uint32_t idx);
4954
const Symbol * SymbolAtIndex (uint32_t idx) const;
@@ -84,7 +89,9 @@ class Symtab
8489
collection m_symbols;
8590
std::vector<uint32_t> m_addr_indexes;
8691
UniqueCStringMap<uint32_t> m_name_to_index;
87-
92+
mutable Mutex m_mutex; // Provide thread safety for this symbol table
93+
bool m_addr_indexes_computed:1,
94+
m_name_indexes_computed:1;
8895
private:
8996

9097
bool

lldb/include/lldb/lldb-enumerations.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ typedef enum CommandArgumentType
558558
eArgTypeSettingVariableName,
559559
eArgTypeShlibName,
560560
eArgTypeSourceFile,
561+
eArgTypeSortOrder,
561562
eArgTypeStartAddress,
562563
eArgTypeSymbol,
563564
eArgTypeThreadID,
@@ -573,19 +574,25 @@ typedef enum CommandArgumentType
573574

574575
typedef enum ArgumentRepetitionType
575576
{
576-
eArgRepeatPlain, // Exactly one occurrence
577-
eArgRepeatOptional, // At most one occurrence, but it's optional
578-
eArgRepeatPlus, // One or more occurrences
579-
eArgRepeatStar, // Zero or more occurrences
580-
eArgRepeatRange, // Repetition of same argument, from 1 to n
581-
eArgRepeatPairPlain, // A pair of arguments that must always go together ([arg-type arg-value]), occurs exactly once
582-
eArgRepeatPairOptional, // A pair that occurs at most once (optional)
583-
eArgRepeatPairPlus, // One or more occurrences of a pair
584-
eArgRepeatPairStar, // Zero or more occurrences of a pair
585-
eArgRepeatPairRange, // A pair that repeats from 1 to n
586-
eArgRepeatPairRangeOptional, // A pair that repeats from 1 to n, but is optional
577+
eArgRepeatPlain, // Exactly one occurrence
578+
eArgRepeatOptional, // At most one occurrence, but it's optional
579+
eArgRepeatPlus, // One or more occurrences
580+
eArgRepeatStar, // Zero or more occurrences
581+
eArgRepeatRange, // Repetition of same argument, from 1 to n
582+
eArgRepeatPairPlain, // A pair of arguments that must always go together ([arg-type arg-value]), occurs exactly once
583+
eArgRepeatPairOptional, // A pair that occurs at most once (optional)
584+
eArgRepeatPairPlus, // One or more occurrences of a pair
585+
eArgRepeatPairStar, // Zero or more occurrences of a pair
586+
eArgRepeatPairRange, // A pair that repeats from 1 to n
587+
eArgRepeatPairRangeOptional // A pair that repeats from 1 to n, but is optional
587588
} ArgumentRepetitionType;
588589

590+
typedef enum SortOrder
591+
{
592+
eSortOrderNone,
593+
eSortOrderByAddress,
594+
eSortOrderByName,
595+
} SortOrder;
589596

590597
} // namespace lldb
591598

lldb/source/Commands/CommandObjectImage.cpp

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
156156

157157

158158
static void
159-
DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module)
159+
DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, lldb::SortOrder sort_order)
160160
{
161161
if (module)
162162
{
@@ -165,7 +165,7 @@ DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module)
165165
{
166166
Symtab *symtab = objfile->GetSymtab();
167167
if (symtab)
168-
symtab->Dump(&strm, interpreter.GetDebugger().GetExecutionContext().target);
168+
symtab->Dump(&strm, interpreter.GetDebugger().GetExecutionContext().target, sort_order);
169169
}
170170
}
171171
}
@@ -634,8 +634,13 @@ class CommandObjectImageDumpSymtab : public CommandObjectImageDumpModuleList
634634
result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
635635
for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
636636
{
637+
if (num_dumped > 0)
638+
{
639+
result.GetOutputStream().EOL();
640+
result.GetOutputStream().EOL();
641+
}
637642
num_dumped++;
638-
DumpModuleSymtab (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
643+
DumpModuleSymtab (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx), m_options.m_sort_order);
639644
}
640645
}
641646
else
@@ -672,8 +677,13 @@ class CommandObjectImageDumpSymtab : public CommandObjectImageDumpModuleList
672677
Module *image_module = matching_modules.GetModulePointerAtIndex(i);
673678
if (image_module)
674679
{
680+
if (num_dumped > 0)
681+
{
682+
result.GetOutputStream().EOL();
683+
result.GetOutputStream().EOL();
684+
}
675685
num_dumped++;
676-
DumpModuleSymtab (m_interpreter, result.GetOutputStream(), image_module);
686+
DumpModuleSymtab (m_interpreter, result.GetOutputStream(), image_module, m_options.m_sort_order);
677687
}
678688
}
679689
}
@@ -692,9 +702,100 @@ class CommandObjectImageDumpSymtab : public CommandObjectImageDumpModuleList
692702
}
693703
return result.Succeeded();
694704
}
705+
706+
virtual Options *
707+
GetOptions ()
708+
{
709+
return &m_options;
710+
}
711+
712+
class CommandOptions : public Options
713+
{
714+
public:
715+
716+
CommandOptions () :
717+
Options(),
718+
m_sort_order (eSortOrderNone)
719+
{
720+
}
721+
722+
virtual
723+
~CommandOptions ()
724+
{
725+
}
726+
727+
virtual Error
728+
SetOptionValue (int option_idx, const char *option_arg)
729+
{
730+
Error error;
731+
char short_option = (char) m_getopt_table[option_idx].val;
732+
733+
switch (short_option)
734+
{
735+
case 's':
736+
{
737+
bool found_one = false;
738+
m_sort_order = (lldb::SortOrder) Args::StringToOptionEnum (option_arg,
739+
g_option_table[option_idx].enum_values,
740+
eSortOrderNone,
741+
&found_one);
742+
if (!found_one)
743+
error.SetErrorStringWithFormat("Invalid enumeration value '%s' for option '%c'.\n",
744+
option_arg,
745+
short_option);
746+
}
747+
break;
695748

749+
default:
750+
error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
751+
break;
752+
753+
}
754+
return error;
755+
}
756+
757+
void
758+
ResetOptionValues ()
759+
{
760+
Options::ResetOptionValues();
761+
m_sort_order = eSortOrderNone;
762+
}
763+
764+
const lldb::OptionDefinition*
765+
GetDefinitions ()
766+
{
767+
return g_option_table;
768+
}
769+
770+
// Options table: Required for subclasses of Options.
771+
static lldb::OptionDefinition g_option_table[];
772+
773+
SortOrder m_sort_order;
774+
};
775+
776+
protected:
777+
778+
CommandOptions m_options;
779+
};
780+
781+
lldb::OptionEnumValueElement
782+
g_sort_option_enumeration[4] =
783+
{
784+
{ eSortOrderNone, "none", "No sorting, use the original symbol table order."},
785+
{ eSortOrderByAddress, "address", "Sort output by symbol address."},
786+
{ eSortOrderByName, "name", "Sort output by symbol name."},
787+
{ 0, NULL, NULL }
696788
};
697789

790+
791+
lldb::OptionDefinition
792+
CommandObjectImageDumpSymtab::CommandOptions::g_option_table[] =
793+
{
794+
{ LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
795+
{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
796+
};
797+
798+
698799
//----------------------------------------------------------------------
699800
// Image section dumping command
700801
//----------------------------------------------------------------------
@@ -1380,8 +1481,7 @@ class CommandObjectImageLookup : public CommandObject
13801481
{
13811482
}
13821483

1383-
virtual
1384-
Options *
1484+
virtual Options *
13851485
GetOptions ()
13861486
{
13871487
return &m_options;

lldb/source/Commands/CommandObjectThread.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,16 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObject
506506

507507
switch (short_option)
508508
{
509-
case 'a':
509+
case 'a':
510510
{
511511
bool success;
512512
m_avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
513513
if (!success)
514514
error.SetErrorStringWithFormat("Invalid boolean value for option '%c'.\n", short_option);
515515
}
516516
break;
517-
case 'm':
517+
518+
case 'm':
518519
{
519520
bool found_one = false;
520521
OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
@@ -523,15 +524,17 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObject
523524
error.SetErrorStringWithFormat("Invalid enumeration value for option '%c'.\n", short_option);
524525
}
525526
break;
526-
case 'r':
527+
528+
case 'r':
527529
{
528530
m_avoid_regexp.clear();
529531
m_avoid_regexp.assign(option_arg);
530532
}
531533
break;
532-
default:
533-
error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
534-
break;
534+
535+
default:
536+
error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
537+
break;
535538

536539
}
537540
return error;

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ ObjectFileELF::GetSymtab()
647647
Symtab *symbol_table = new Symtab(this);
648648
m_symtab_ap.reset(symbol_table);
649649

650+
Mutex::Locker locker (symbol_table->GetMutex ());
651+
650652
if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
651653
return symbol_table;
652654

@@ -658,7 +660,7 @@ ObjectFileELF::GetSymtab()
658660
{
659661
const ELFSectionHeader &symtab_section = *I;
660662
user_id_t section_id = SectionIndex(I);
661-
ParseSymbolTable(symbol_table, symtab_section, section_id);
663+
ParseSymbolTable (symbol_table, symtab_section, section_id);
662664
}
663665
}
664666

@@ -685,7 +687,7 @@ ObjectFileELF::Dump(Stream *s)
685687
section_list->Dump(s, NULL, true);
686688
Symtab *symtab = GetSymtab();
687689
if (symtab)
688-
symtab->Dump(s, NULL);
690+
symtab->Dump(s, NULL, lldb::eSortOrderNone);
689691
s->EOL();
690692
DumpDependentModules(s);
691693
s->EOL();

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ ObjectFileMachO::GetSymtab()
206206
if (m_symtab_ap.get() == NULL)
207207
{
208208
m_symtab_ap.reset(new Symtab(this));
209+
Mutex::Locker locker (m_symtab_ap->GetMutex());
209210
ParseSymtab (true);
210211
}
211212
return m_symtab_ap.get();
@@ -1348,7 +1349,7 @@ ObjectFileMachO::Dump (Stream *s)
13481349
m_sections_ap->Dump(s, NULL, true);
13491350

13501351
if (m_symtab_ap.get())
1351-
m_symtab_ap->Dump(s, NULL);
1352+
m_symtab_ap->Dump(s, NULL, eSortOrderNone);
13521353
}
13531354

13541355

0 commit comments

Comments
 (0)