Skip to content

Commit

Permalink
-Use std::optional for getdbgSourceFile
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-gomez-windhover committed Oct 16, 2024
1 parent 1f91f61 commit 3f6a0e7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
25 changes: 15 additions & 10 deletions src/Juicer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ Symbol *Juicer::process_DW_TAG_pointer_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf
*/
/* This branch represents a "void*" since there is no valid type.
* Read section 5.2 of DWARF4 for details on this.*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(voidType, byteSize, newArtifact);
Expand Down Expand Up @@ -1087,7 +1087,7 @@ Symbol *Juicer::getBaseTypeSymbol(ElfFile &elf, Dwarf_Die inDie, DimensionList &
* indicates that no source file has been specified.
*
*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(cName, byteSize, newArtifact);
Expand Down Expand Up @@ -1260,7 +1260,7 @@ Symbol *Juicer::getBaseTypeSymbol(ElfFile &elf, Dwarf_Die inDie, DimensionList &
* indicates that no source file has been specified.
*
*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(cName, byteSize, newArtifact);
Expand Down Expand Up @@ -1416,7 +1416,7 @@ Symbol *Juicer::getBaseTypeSymbol(ElfFile &elf, Dwarf_Die inDie, DimensionList &
* indicates that no source file has been specified.
*
*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(cName, byteSize, newArtifact);
Expand Down Expand Up @@ -3337,7 +3337,7 @@ Symbol *Juicer::process_DW_TAG_base_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Di
* indicates that no source file has been specified.
*
*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(sDieName, byteSize, newArtifact);
Expand Down Expand Up @@ -3627,7 +3627,7 @@ Symbol *Juicer::process_DW_TAG_typedef(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die
* indicates that no source file has been specified.
*
*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(sDieName, byteSize, newArtifact, baseTypeSymbol);
Expand Down Expand Up @@ -4417,7 +4417,7 @@ int Juicer::getDieAndSiblings(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Die in_die, i
* indicates that no source file has been specified.
*
*/
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex)};
Artifact newArtifact{elf, getdbgSourceFile(elf, pathIndex).value_or(std::string{"NOT_FOUND:"})};
std::string checkSum = generateMD5SumForFile(newArtifact.getFilePath());
newArtifact.setMD5(checkSum);
outSymbol = elf.addSymbol(sDieName, byteSize, newArtifact);
Expand Down Expand Up @@ -5726,8 +5726,13 @@ std::string Juicer::generateMD5SumForFile(std::string filePath)
* handles debug source files lookups for different DWARF versions.
* It is assumed the pathIndex is the value of DW_AT_decl_file attribute
*/
std::string &Juicer::getdbgSourceFile(ElfFile &elf, int pathIndex)
std::optional<std::string> Juicer::getdbgSourceFile(ElfFile &elf, int pathIndex)
{
if (dbgSourceFiles.empty())
{
return std::nullopt;
}

switch (dwarfVersion)
{
/**
Expand All @@ -5739,11 +5744,11 @@ std::string &Juicer::getdbgSourceFile(ElfFile &elf, int pathIndex)

case 4:
{
return dbgSourceFiles.at(pathIndex - 1);
return std::string{dbgSourceFiles.at(pathIndex - 1)};
}
case 5:
{
return dbgSourceFiles.at(pathIndex);
return std::string{dbgSourceFiles.at(pathIndex)};
}
default:
{
Expand Down
84 changes: 42 additions & 42 deletions src/Juicer.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,48 +106,48 @@ class Juicer
unsigned int getDwarfVersion();

private:
Dwarf_Debug dbg = 0;
int res = DW_DLV_ERROR;
Dwarf_Handler errhand;
Dwarf_Ptr errarg = 0;
int readCUList(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Error& error);
int getDieAndSiblings(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die in_die, int in_level);
Symbol* process_DW_TAG_typedef(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die in_die);
Symbol* process_DW_TAG_base_type(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die in_die);
void process_DW_TAG_structure_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
Symbol* process_DW_TAG_pointer_type(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die inDie);
Symbol* process_DW_TAG_variable_type(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die inDie);
void process_DW_TAG_enumeration_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
int process_DW_TAG_array_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
void process_DW_TAG_union_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
char* getFirstAncestorName(Dwarf_Die inDie);
int printDieData(Dwarf_Debug dbg, Dwarf_Die print_me, uint32_t level);
char* dwarfStringToChar(char* dwarfString);
void addBitFields(Dwarf_Die dataMemberDie, Field& dataMemberField);
void addPaddingToStruct(Symbol& symbol);
void addPaddingEndToStruct(Symbol& symbol);
bool isDWARFVersionSupported(Dwarf_Die);
int elfFile = 0;
Logger logger;
IDataContainer* idc = 0;
bool isIDCSet(void);
Symbol* getBaseTypeSymbol(ElfFile& elf, Dwarf_Die inDie, DimensionList& multiplicity);
void DisplayDie(Dwarf_Die inDie, uint32_t level);

std::vector<Dwarf_Die> getChildrenVector(Dwarf_Debug dbg, Dwarf_Die die);
int getNumberOfSiblingsForDie(Dwarf_Debug dbg, Dwarf_Die die);

uint32_t calcArraySizeForDimension(Dwarf_Debug dbg, Dwarf_Die die);

DimensionList getDimList(Dwarf_Debug dbg, Dwarf_Die die);

std::vector<std::string> dbgSourceFiles{};

std::string generateMD5SumForFile(std::string filePath);
std::string& getdbgSourceFile(ElfFile& elf, int pathIndex);
DefineMacro getDefineMacro(Dwarf_Half macro_operator, Dwarf_Macro_Context mac_context, int i, Dwarf_Unsigned line_number, Dwarf_Unsigned index,
Dwarf_Unsigned offset, const char* macro_string, Dwarf_Half& forms_count, Dwarf_Error& error, Dwarf_Die cu_die, ElfFile& elf);
DefineMacro getDefineMacroFromString(std::string macro_string);
Dwarf_Debug dbg = 0;
int res = DW_DLV_ERROR;
Dwarf_Handler errhand;
Dwarf_Ptr errarg = 0;
int readCUList(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Error& error);
int getDieAndSiblings(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die in_die, int in_level);
Symbol* process_DW_TAG_typedef(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die in_die);
Symbol* process_DW_TAG_base_type(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die in_die);
void process_DW_TAG_structure_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
Symbol* process_DW_TAG_pointer_type(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die inDie);
Symbol* process_DW_TAG_variable_type(ElfFile& elf, Dwarf_Debug dbg, Dwarf_Die inDie);
void process_DW_TAG_enumeration_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
int process_DW_TAG_array_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
void process_DW_TAG_union_type(ElfFile& elf, Symbol& symbol, Dwarf_Debug dbg, Dwarf_Die inDie);
char* getFirstAncestorName(Dwarf_Die inDie);
int printDieData(Dwarf_Debug dbg, Dwarf_Die print_me, uint32_t level);
char* dwarfStringToChar(char* dwarfString);
void addBitFields(Dwarf_Die dataMemberDie, Field& dataMemberField);
void addPaddingToStruct(Symbol& symbol);
void addPaddingEndToStruct(Symbol& symbol);
bool isDWARFVersionSupported(Dwarf_Die);
int elfFile = 0;
Logger logger;
IDataContainer* idc = 0;
bool isIDCSet(void);
Symbol* getBaseTypeSymbol(ElfFile& elf, Dwarf_Die inDie, DimensionList& multiplicity);
void DisplayDie(Dwarf_Die inDie, uint32_t level);

std::vector<Dwarf_Die> getChildrenVector(Dwarf_Debug dbg, Dwarf_Die die);
int getNumberOfSiblingsForDie(Dwarf_Debug dbg, Dwarf_Die die);

uint32_t calcArraySizeForDimension(Dwarf_Debug dbg, Dwarf_Die die);

DimensionList getDimList(Dwarf_Debug dbg, Dwarf_Die die);

std::vector<std::string> dbgSourceFiles{};

std::string generateMD5SumForFile(std::string filePath);
std::optional<std::string> getdbgSourceFile(ElfFile& elf, int pathIndex);
DefineMacro getDefineMacro(Dwarf_Half macro_operator, Dwarf_Macro_Context mac_context, int i, Dwarf_Unsigned line_number, Dwarf_Unsigned index,
Dwarf_Unsigned offset, const char* macro_string, Dwarf_Half& forms_count, Dwarf_Error& error, Dwarf_Die cu_die, ElfFile& elf);
DefineMacro getDefineMacroFromString(std::string macro_string);
std::map<std::string, std::vector<uint8_t>> getObjDataFromElf(ElfFile* elfFileObj);

bool extras;
Expand Down

0 comments on commit 3f6a0e7

Please sign in to comment.