Skip to content

Commit

Permalink
Merge pull request #448 from SimonKagstrom/collect-only-for-bash
Browse files Browse the repository at this point in the history
Issue #437: Fixes for --collect-only/--report-only
  • Loading branch information
SimonKagstrom authored Jul 9, 2024
2 parents fe0643b + 06f6255 commit 9133fec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion doc/vscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension when the coverage changes.

To produce this output, just run

```
```sh
kcov --cobertura-only [other options] /path/to/your/folder/.vscode /path/to/the/binary
```

Expand Down
23 changes: 18 additions & 5 deletions src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,26 @@ class Configuration : public IConfiguration
setKey("binary-path", tmp.first);
binaryName = tmp.second;

setKey("binary-name", binaryName);

if (!keyAsInt("cobertura-only"))
{
setKey("target-directory",
fmt("%s/%s.%08zx", outDirectory.c_str(), binaryName.c_str(),
(size_t)hash_file(get_real_path(binaryPath))));
IFileParser *parser = IParserManager::getInstance().matchParser(binaryPath);

if (parser && (parser->getParserType() == "bash" ||
parser->getParserType() == "python"))
{
// Use the path as the hash for non-compiled languages
setKey("target-directory",
fmt("%s/%s.%08zx", keyAsString("out-directory").c_str(), keyAsString("binary-name").c_str(),
std::hash<std::string>()(keyAsString("binary-name").c_str())));
}
else
{
setKey("target-directory",
fmt("%s/%s.%08zx", outDirectory.c_str(), binaryName.c_str(),
(size_t)hash_file(get_real_path(binaryPath))));
}
}
else
{
Expand All @@ -511,8 +526,6 @@ class Configuration : public IConfiguration
return usage();
}

setKey("binary-name", binaryName);

if (keyAsString("command-name") == "")
setKey("command-name", binaryName);
}
Expand Down
10 changes: 0 additions & 10 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,6 @@ static int runKcov(IConfiguration::RunMode_t runningMode)
return 1;
}

if ((parser->getParserType() == "bash" ||
parser->getParserType() == "python") &&
!conf.keyAsInt("cobertura-only"))
{
// Use the path as the hash for non-compiled languages
conf.setKey("target-directory",
fmt("%s/%s.%08zx", conf.keyAsString("out-directory").c_str(), conf.keyAsString("binary-name").c_str(),
std::hash<std::string>()(conf.keyAsString("binary-name").c_str())));
}

// Match and create an engine
IEngineFactory::IEngineCreator &engineCreator =
IEngineFactory::getInstance().matchEngine(file);
Expand Down
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 9133fec

Please sign in to comment.