Skip to content

Commit

Permalink
try to support go executable
Browse files Browse the repository at this point in the history
  • Loading branch information
myzhan committed Aug 24, 2023
1 parent 1d567a1 commit 89ddc61
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/engines/mach-engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <engine.hh>
#include <mach-o/loader.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach/mach_init.h>
#include <mach/mach_types.h>
#include <mach/port.h>
Expand All @@ -35,6 +36,7 @@
#include <unistd.h>
#include <unordered_map>
#include <utils.hh>
#include <iostream>

using namespace kcov;
extern char** environ;
Expand Down Expand Up @@ -493,12 +495,13 @@ class MachEngine : public IEngine
size_t size = 0;

// FIXME! Is this really true?
auto patch_addr = m_imageBase + (aligned_addr & 0xffffffff);
auto patch_addr = (aligned_addr & 0xffffffff);
// std::cout << std::hex << "m_imageBase: 0x" << m_imageBase << " patch_addr: 0x" << patch_addr << std::endl;

auto kr = vm_read_overwrite(m_task, patch_addr, sizeof(val), (vm_offset_t)&val, &size);
if (kr != KERN_SUCCESS)
{
panic("vm_read_overwrite failed for peekWord for addr 0x%llx", patch_addr);
panic("vm_read_overwrite failed for peekWord for addr 0x%llx, msg: %s", patch_addr, mach_error_string(kr));
return 0;
}

Expand All @@ -510,7 +513,7 @@ class MachEngine : public IEngine
assert((aligned_addr & 3) == 0 && "The poked address must be aligned");

// FIXME! Is this really true?
auto patch_addr = m_imageBase + (aligned_addr & 0xffffffff);
auto patch_addr = (aligned_addr & 0xffffffff);

// VM_PROT_COPY forces COW, probably, see vm_map_protect in vm_map.c
kern_return_t kr;
Expand All @@ -521,6 +524,7 @@ class MachEngine : public IEngine
VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
if (kr != KERN_SUCCESS)
{
printf("%s ", mach_error_string(kr));
panic("vm_protect failed\n");
}

Expand All @@ -534,6 +538,7 @@ class MachEngine : public IEngine
m_task, trunc_page(patch_addr), vm_page_size, false, VM_PROT_READ | VM_PROT_EXECUTE);
if (kr != KERN_SUCCESS)
{
printf("%s ", mach_error_string(kr));
panic("vm_protect failed\n");
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/parsers/macho-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <unistd.h>
#include <utils.hh>
#include <vector>
#include <iostream>

using namespace kcov;

Expand Down Expand Up @@ -71,9 +72,8 @@ class MachoParser : public IFileParser
bool parse() final
{
auto& conf = IConfiguration::getInstance();
auto name = fmt("%s/%s.dSYM/Contents/Resources/DWARF/%s",
auto name = fmt("%s/%s",
conf.keyAsString("binary-path").c_str(),
conf.keyAsString("binary-name").c_str(),
conf.keyAsString("binary-name").c_str());

m_fileData = static_cast<uint8_t*>(read_file(&m_fileSize, "%s", name.c_str()));
Expand All @@ -90,7 +90,8 @@ class MachoParser : public IFileParser
return false;
}

if (hdr->filetype != MH_DSYM)
// std::cout << std::hex << "filetype: " << hdr->filetype << std::endl;
if (hdr->filetype != MH_DSYM && hdr->filetype != MH_EXECUTE)
{
error("Not a debug file");
return false;
Expand Down Expand Up @@ -271,15 +272,15 @@ class MachoParser : public IFileParser

void setupParser(IFilter* filter) final
{
auto& conf = IConfiguration::getInstance();
// auto& conf = IConfiguration::getInstance();

// Run dsymutil to make sure the DWARF info is avaiable
auto dsymutil_command = fmt("dsymutil %s/%s",
conf.keyAsString("binary-path").c_str(),
conf.keyAsString("binary-name").c_str());
kcov_debug(ELF_MSG, "running %s\n", dsymutil_command.c_str());
// auto dsymutil_command = fmt("dsymutil %s/%s",
// conf.keyAsString("binary-path").c_str(),
// conf.keyAsString("binary-name").c_str());
// kcov_debug(ELF_MSG, "running %s\n", dsymutil_command.c_str());

system(dsymutil_command.c_str());
// system(dsymutil_command.c_str());
}

// Mach-O command handlers
Expand Down
45 changes: 45 additions & 0 deletions tools/parse-go-macho.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"debug/dwarf"
"debug/macho"
"fmt"
"io"
"os"
)

func panicIfErr(err error) {
if err != nil {
panic(err)
}
}

func main() {
executable := os.Args[1]
exe, err := macho.Open(executable)
panicIfErr(err)
dw, err := exe.DWARF()
panicIfErr(err)
reader := dw.Reader()
for index := 0; ; index++ {
entry, err := reader.Next()
panicIfErr(err)
if entry == nil {
break
}
if entry.Tag != dwarf.TagCompileUnit {
continue
}
lrd, err := dw.LineReader(entry)
panicIfErr(err)
for {
var e dwarf.LineEntry
err := lrd.Next(&e)
if err == io.EOF {
break
}
panicIfErr(err)
fmt.Printf("%s:%d %x\n", e.File.Name, e.Line, e.Address)
}
}
}

0 comments on commit 89ddc61

Please sign in to comment.