Skip to content

Commit

Permalink
macho-parser: Report file to enable --collect-only/--report-only + fo…
Browse files Browse the repository at this point in the history
…rmatting
  • Loading branch information
SimonKagstrom committed Jul 9, 2024
1 parent a5bab74 commit 06f6255
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/parsers/macho-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fcntl.h>
#include <file-parser.hh>
#include <filter.hh>
#include <iostream>
#include <libdwarf.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
Expand All @@ -21,7 +22,6 @@
#include <unistd.h>
#include <utils.hh>
#include <vector>
#include <iostream>

using namespace kcov;

Expand All @@ -46,6 +46,11 @@ class MachoParser : public IFileParser
{
m_filename = filename;

for (const auto l : m_fileListeners)
{
l->onFile(File(m_filename, IFileParser::FLG_NONE));
}

return true;
}

Expand Down Expand Up @@ -76,10 +81,11 @@ class MachoParser : public IFileParser
conf.keyAsString("binary-path").c_str(),
conf.keyAsString("binary-name").c_str(),
conf.keyAsString("binary-name").c_str());
if (conf.keyAsInt("is-go-binary")) {
if (conf.keyAsInt("is-go-binary"))
{
name = fmt("%s/%s",
conf.keyAsString("binary-path").c_str(),
conf.keyAsString("binary-name").c_str());
conf.keyAsString("binary-path").c_str(),
conf.keyAsString("binary-name").c_str());
}

m_fileData = static_cast<uint8_t*>(read_file(&m_fileSize, "%s", name.c_str()));
Expand Down Expand Up @@ -258,40 +264,42 @@ class MachoParser : public IFileParser
}

// Search for the "__go_buildinfo" section in the "__DATA" segment.
bool isGoBinary(const std::string& filename) {
bool isGoBinary(const std::string& filename)
{
size_t read_size = 0;
auto full_file_content = static_cast<uint8_t*>(read_file(&read_size, "%s", filename.c_str()));
auto full_file_content =
static_cast<uint8_t*>(read_file(&read_size, "%s", filename.c_str()));
auto hdr = reinterpret_cast<mach_header_64*>(full_file_content);
auto cmd_ptr = full_file_content + sizeof(mach_header_64);
for (auto i = 0; i < hdr->ncmds; i++)
{
auto cmd = reinterpret_cast<load_command*>(cmd_ptr);
switch (cmd->cmd)
{
case LC_SEGMENT_64:
case LC_SEGMENT_64: {
auto segment = reinterpret_cast<const struct segment_command_64*>(cmd_ptr);
if (strcmp(segment->segname, "__DATA") == 0)
{
auto segment = reinterpret_cast<const struct segment_command_64*>(cmd_ptr);
if (strcmp(segment->segname, "__DATA") == 0) {
auto section_ptr = cmd_ptr + sizeof(struct segment_command_64);
for (auto i = 0; i < segment->nsects; i++)
auto section_ptr = cmd_ptr + sizeof(struct segment_command_64);
for (auto i = 0; i < segment->nsects; i++)
{
auto section = reinterpret_cast<struct section_64*>(section_ptr);
if (strcmp(section->sectname, "__go_buildinfo") == 0)
{
auto section = reinterpret_cast<struct section_64*>(section_ptr);
if (strcmp(section->sectname, "__go_buildinfo") == 0)
{
free((void *) full_file_content);
return true;
}
section_ptr += sizeof(*section);
free((void*)full_file_content);
return true;
}
section_ptr += sizeof(*section);
}
}
break;
}
break;
default:
break;
}
cmd_ptr += cmd->cmdsize;
}
free((void *) full_file_content);
free((void*)full_file_content);
return false;
}

Expand All @@ -308,7 +316,8 @@ class MachoParser : public IFileParser
if (hdr->magic == MH_MAGIC_64)
{
auto& conf = IConfiguration::getInstance();
if (!conf.keyAsInt("is-go-binary") && isGoBinary(filename)) {
if (!conf.keyAsInt("is-go-binary") && isGoBinary(filename))
{
conf.setKey("is-go-binary", 1);
}
return match_perfect;
Expand All @@ -320,11 +329,14 @@ class MachoParser : public IFileParser
void setupParser(IFilter* filter) final
{
auto& conf = IConfiguration::getInstance();
if (conf.keyAsInt("is-go-binary")) {
if (conf.keyAsInt("is-go-binary"))
{
// Do nothing, because the Go linker puts dwarf info in the binary instead of a seperated dSYM file.
// This can be removed if this proposal passed.
// https://github.com/golang/go/issues/62577
} else {
}
else
{
// Run dsymutil to make sure the DWARF info is avaiable
auto dsymutil_command = fmt("dsymutil %s/%s",
conf.keyAsString("binary-path").c_str(),
Expand Down

0 comments on commit 06f6255

Please sign in to comment.