Skip to content

Commit

Permalink
Merge pull request FEX-Emu#1421 from lioncash/symbols
Browse files Browse the repository at this point in the history
JitSymbols: Make use of fmt
  • Loading branch information
Sonicadvance1 authored Dec 3, 2021
2 parents 8c38f93 + ec8f077 commit db0740b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
45 changes: 16 additions & 29 deletions External/FEXCore/Source/Common/JitSymbols.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,53 @@
#include "Common/JitSymbols.h"

#include <string>
#include <sstream>
#include <unistd.h>

#include <fmt/format.h>

namespace FEXCore {
JITSymbols::JITSymbols() {
std::stringstream PerfMap;
PerfMap << "/tmp/perf-" << getpid() << ".map";
JITSymbols::JITSymbols() : fp{nullptr, std::fclose} {
const auto PerfMap = fmt::format("/tmp/perf-{}.map", getpid());

fp = fopen(PerfMap.str().c_str(), "wb");
fp.reset(fopen(PerfMap.c_str(), "wb"));
if (fp) {
// Disable buffering on this file
setvbuf(fp, nullptr, _IONBF, 0);
setvbuf(fp.get(), nullptr, _IONBF, 0);
}
}

JITSymbols::~JITSymbols() {
if (fp) {
fclose(fp);
}
}
JITSymbols::~JITSymbols() = default;

void JITSymbols::Register(void *HostAddr, uint64_t GuestAddr, uint32_t CodeSize) {
void JITSymbols::Register(const void *HostAddr, uint64_t GuestAddr, uint32_t CodeSize) {
if (!fp) return;

// Linux perf format is very straightforward
// `<HostPtr> <Size> <Name>\n`
std::stringstream String;
String << std::hex << HostAddr << " " << CodeSize << " " << "JIT_0x" << GuestAddr << "_" << HostAddr << std::endl;
fwrite(String.str().c_str(), 1, String.str().size(), fp);
fmt::print(fp.get(), "{:x} {:x} JIT_0x{:x}_{:x}\n", HostAddr, CodeSize, GuestAddr, HostAddr);
}

void JITSymbols::Register(void *HostAddr, uint32_t CodeSize, std::string const &Name) {
void JITSymbols::Register(const void *HostAddr, uint32_t CodeSize, std::string_view Name) {
if (!fp) return;

// Linux perf format is very straightforward
// `<HostPtr> <Size> <Name>\n`
std::stringstream String;
String << std::hex << HostAddr << " " << CodeSize << " " << Name << "_" << HostAddr << std::endl;
fwrite(String.str().c_str(), 1, String.str().size(), fp);
fmt::print(fp.get(), "{:x} {:x} {}_{:x}\n", HostAddr, CodeSize, Name, HostAddr);
}

void JITSymbols::RegisterNamedRegion(void *HostAddr, uint32_t CodeSize, std::string const &Name) {
void JITSymbols::RegisterNamedRegion(const void *HostAddr, uint32_t CodeSize, std::string_view Name) {
if (!fp) return;

// Linux perf format is very straightforward
// `<HostPtr> <Size> <Name>\n`
std::stringstream String;
String << std::hex << HostAddr << " " << CodeSize << " " << Name << std::endl;
fwrite(String.str().c_str(), 1, String.str().size(), fp);
fmt::print(fp.get(), "{:x} {:x} {}\n", HostAddr, CodeSize, Name);
}

void JITSymbols::RegisterJITSpace(void *HostAddr, uint32_t CodeSize) {
void JITSymbols::RegisterJITSpace(const void *HostAddr, uint32_t CodeSize) {
if (!fp) return;

// Linux perf format is very straightforward
// `<HostPtr> <Size> <Name>\n`
std::stringstream String;
String << std::hex << HostAddr << " " << CodeSize << " FEXJIT" << std::endl;
fwrite(String.str().c_str(), 1, String.str().size(), fp);
fmt::print(fp.get(), "{:x} {:x} FEXJIT\n", HostAddr, CodeSize);
}

}

} // namespace FEXCore
17 changes: 11 additions & 6 deletions External/FEXCore/Source/Common/JitSymbols.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#pragma once

#include <cstdint>
#include <cstdio>
#include <string>
#include <memory>
#include <string_view>

namespace FEXCore {
class JITSymbols final {
public:
JITSymbols();
~JITSymbols();
void Register(void *HostAddr, uint64_t GuestAddr, uint32_t CodeSize);
void Register(void *HostAddr, uint32_t CodeSize, std::string const &Name);
void RegisterNamedRegion(void *HostAddr, uint32_t CodeSize, std::string const &Name);
void RegisterJITSpace(void *HostAddr, uint32_t CodeSize);

void Register(const void *HostAddr, uint64_t GuestAddr, uint32_t CodeSize);
void Register(const void *HostAddr, uint32_t CodeSize, std::string_view Name);
void RegisterNamedRegion(const void *HostAddr, uint32_t CodeSize, std::string_view Name);
void RegisterJITSpace(const void *HostAddr, uint32_t CodeSize);

private:
FILE* fp{};
using FILEPtr = std::unique_ptr<FILE, decltype(&std::fclose)>;

FILEPtr fp;
};
}

0 comments on commit db0740b

Please sign in to comment.