Skip to content

Commit 8a308e5

Browse files
-Refactor and cleanup process_DW_TAG_namespace
1 parent 8df859a commit 8a308e5

File tree

2 files changed

+80
-77
lines changed

2 files changed

+80
-77
lines changed

src/Juicer.cpp

+79-76
Original file line numberDiff line numberDiff line change
@@ -629,14 +629,90 @@ char *Juicer::getFirstAncestorName(Dwarf_Die inDie)
629629
return outName;
630630
}
631631

632-
int Juicer::process_DW_TAG_namespace(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die inDie, int in_level, Namespace *currentNamespace)
632+
/**
633+
* @brief Add new namespace to elf, if it does not exist.
634+
*
635+
* @return The new namespace that was added. Not that this namespace may be null if it already exists.
636+
*/
637+
Namespace *Juicer::process_DW_TAG_namespace(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die inDie, int in_level, Namespace *currentNamespace)
633638
{
634639
Dwarf_Attribute attr_struct;
635640
Dwarf_Error error = 0;
636641
char *name = nullptr;
637642
int res = 0;
643+
Namespace ns{};
644+
std::string namespaceName{};
638645

639-
return res;
646+
Namespace *newParentNamespace = nullptr;
647+
648+
bool namespaceExists = false;
649+
650+
// Need to figure out if this die has already been processed.
651+
652+
res = dwarf_attr(inDie, DW_AT_name, &attr_struct, &error);
653+
if (res != DW_DLV_OK)
654+
{
655+
logger.logError("Error in dwarf_attr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
656+
}
657+
658+
if (res == DW_DLV_OK)
659+
{
660+
res = dwarf_formstring(attr_struct, &name, &error);
661+
if (res != DW_DLV_OK)
662+
{
663+
logger.logError("Error in dwarf_formstring. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
664+
}
665+
666+
namespaceName = name;
667+
668+
std::string currentNamespaceFQN = name;
669+
670+
if (currentNamespace)
671+
{
672+
currentNamespaceFQN = currentNamespace->getFullyQualifiedName() + "::" + name;
673+
}
674+
// check if namespace already exists
675+
for (auto &ns : elf.getNamespaces())
676+
{
677+
if (currentNamespace != nullptr)
678+
{
679+
std::string nsFQN = ns->getFullyQualifiedName();
680+
681+
if (nsFQN == currentNamespaceFQN)
682+
{
683+
namespaceExists = true;
684+
break;
685+
}
686+
}
687+
688+
else
689+
{
690+
if (ns->getName() == name)
691+
{
692+
namespaceExists = true;
693+
break;
694+
}
695+
}
696+
}
697+
698+
if (!namespaceExists)
699+
{
700+
if (res == DW_DLV_OK)
701+
{
702+
ns.setName(name);
703+
ns.setParent(currentNamespace);
704+
elf.addNamespace(ns);
705+
newParentNamespace = elf.getNamespace(ns.getFullyQualifiedName());
706+
707+
if (currentNamespace != nullptr)
708+
{
709+
currentNamespace->addChild(elf.getNamespace(ns.getFullyQualifiedName()));
710+
}
711+
}
712+
}
713+
}
714+
715+
return newParentNamespace;
640716
}
641717

642718
Symbol *Juicer::process_DW_TAG_pointer_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die inDie)
@@ -4491,80 +4567,7 @@ int Juicer::getDieAndSiblings(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die in_die, i
44914567

44924568
case DW_TAG_namespace:
44934569
{
4494-
// res = process_DW_TAG_namespace(elf, dbg, cur_die, in_level, currentNamespace);
4495-
Dwarf_Attribute attr_struct;
4496-
Dwarf_Error error = 0;
4497-
char *name = nullptr;
4498-
int res = 0;
4499-
Namespace ns{};
4500-
4501-
bool namespaceExists = false;
4502-
4503-
// Need to figure out if this die has already been processed.
4504-
4505-
res = dwarf_attr(cur_die, DW_AT_name, &attr_struct, &error);
4506-
if (res != DW_DLV_OK)
4507-
{
4508-
logger.logError("Error in dwarf_attr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
4509-
}
4510-
4511-
if (res == DW_DLV_OK)
4512-
{
4513-
res = dwarf_formstring(attr_struct, &name, &error);
4514-
if (res != DW_DLV_OK)
4515-
{
4516-
logger.logError("Error in dwarf_formstring. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
4517-
}
4518-
4519-
namespaceName = name;
4520-
4521-
std::string currentNamespaceFQN = name;
4522-
4523-
if (currentNamespace)
4524-
{
4525-
currentNamespaceFQN = currentNamespace->getFullyQualifiedName() + "::" + name;
4526-
}
4527-
// check if namespace already exists
4528-
for (auto &ns : elf.getNamespaces())
4529-
{
4530-
if (currentNamespace != nullptr)
4531-
{
4532-
std::string nsFQN = ns->getFullyQualifiedName();
4533-
4534-
if (nsFQN == currentNamespaceFQN)
4535-
{
4536-
namespaceExists = true;
4537-
break;
4538-
}
4539-
}
4540-
4541-
else
4542-
{
4543-
if (ns->getName() == name)
4544-
{
4545-
namespaceExists = true;
4546-
break;
4547-
}
4548-
}
4549-
}
4550-
4551-
if (!namespaceExists)
4552-
{
4553-
if (res == DW_DLV_OK)
4554-
{
4555-
ns.setName(name);
4556-
ns.setParent(currentNamespace);
4557-
elf.addNamespace(ns);
4558-
newParentNamespace = elf.getNamespace(ns.getFullyQualifiedName());
4559-
4560-
if (currentNamespace != nullptr)
4561-
{
4562-
currentNamespace->addChild(elf.getNamespace(ns.getFullyQualifiedName()));
4563-
}
4564-
}
4565-
}
4566-
}
4567-
4570+
newParentNamespace = process_DW_TAG_namespace(elf, dbg, cur_die, in_level, currentNamespace);
45684571
break;
45694572
}
45704573
}

src/Juicer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Juicer
121121
void process_DW_TAG_enumeration_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
122122
int process_DW_TAG_array_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
123123
void process_DW_TAG_union_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
124-
int process_DW_TAG_namespace(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die inDie, int in_level, Namespace* currentNamespace);
124+
Namespace* process_DW_TAG_namespace(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die inDie, int in_level, Namespace* currentNamespace);
125125
char* getFirstAncestorName(Dwarf_Die inDie);
126126
int printDieData(Dwarf_Debug dbg, Dwarf_Die print_me, uint32_t level);
127127
char* dwarfStringToChar(char* dwarfString);

0 commit comments

Comments
 (0)