gecko-dev/tools/profiler/lul/AutoObjectMapper.cpp
Benoit Girard daba448963 Bug 1178892 - Split the profiler into Core & Gecko files and break apart monolithic headers. r=mstange
--HG--
rename : tools/profiler/EHABIStackWalk.cpp => tools/profiler/core/EHABIStackWalk.cpp
rename : tools/profiler/EHABIStackWalk.h => tools/profiler/core/EHABIStackWalk.h
rename : tools/profiler/TableTicker.cpp => tools/profiler/core/GeckoSampler.cpp
rename : tools/profiler/TableTicker.h => tools/profiler/core/GeckoSampler.h
rename : tools/profiler/IntelPowerGadget.cpp => tools/profiler/core/IntelPowerGadget.cpp
rename : tools/profiler/IntelPowerGadget.h => tools/profiler/core/IntelPowerGadget.h
rename : tools/profiler/PlatformMacros.h => tools/profiler/core/PlatformMacros.h
rename : tools/profiler/ProfileEntry.cpp => tools/profiler/core/ProfileEntry.cpp
rename : tools/profiler/ProfileEntry.h => tools/profiler/core/ProfileEntry.h
rename : tools/profiler/ProfileJSONWriter.cpp => tools/profiler/core/ProfileJSONWriter.cpp
rename : tools/profiler/ProfileJSONWriter.h => tools/profiler/core/ProfileJSONWriter.h
rename : tools/profiler/ProfilerBacktrace.cpp => tools/profiler/core/ProfilerBacktrace.cpp
rename : tools/profiler/ProfilerMarkers.cpp => tools/profiler/core/ProfilerMarkers.cpp
rename : tools/profiler/SyncProfile.cpp => tools/profiler/core/SyncProfile.cpp
rename : tools/profiler/SyncProfile.h => tools/profiler/core/SyncProfile.h
rename : tools/profiler/platform-linux.cc => tools/profiler/core/platform-linux.cc
rename : tools/profiler/platform-macos.cc => tools/profiler/core/platform-macos.cc
rename : tools/profiler/platform-win32.cc => tools/profiler/core/platform-win32.cc
rename : tools/profiler/platform.cpp => tools/profiler/core/platform.cpp
rename : tools/profiler/platform.h => tools/profiler/core/platform.h
rename : tools/profiler/shared-libraries-linux.cc => tools/profiler/core/shared-libraries-linux.cc
rename : tools/profiler/shared-libraries-macos.cc => tools/profiler/core/shared-libraries-macos.cc
rename : tools/profiler/shared-libraries-win32.cc => tools/profiler/core/shared-libraries-win32.cc
rename : tools/profiler/shim_mac_dump_syms.h => tools/profiler/core/shim_mac_dump_syms.h
rename : tools/profiler/shim_mac_dump_syms.mm => tools/profiler/core/shim_mac_dump_syms.mm
rename : tools/profiler/v8-support.h => tools/profiler/core/v8-support.h
rename : tools/profiler/ProfileGatherer.cpp => tools/profiler/gecko/ProfileGatherer.cpp
rename : tools/profiler/Profiler.jsm => tools/profiler/gecko/Profiler.jsm
rename : tools/profiler/ProfilerIOInterposeObserver.cpp => tools/profiler/gecko/ProfilerIOInterposeObserver.cpp
rename : tools/profiler/ProfilerIOInterposeObserver.h => tools/profiler/gecko/ProfilerIOInterposeObserver.h
rename : tools/profiler/SaveProfileTask.cpp => tools/profiler/gecko/SaveProfileTask.cpp
rename : tools/profiler/SaveProfileTask.h => tools/profiler/gecko/SaveProfileTask.h
rename : tools/profiler/ThreadResponsiveness.cpp => tools/profiler/gecko/ThreadResponsiveness.cpp
rename : tools/profiler/ThreadResponsiveness.h => tools/profiler/gecko/ThreadResponsiveness.h
rename : tools/profiler/local_debug_info_symbolizer.cc => tools/profiler/gecko/local_debug_info_symbolizer.cc
rename : tools/profiler/local_debug_info_symbolizer.h => tools/profiler/gecko/local_debug_info_symbolizer.h
rename : tools/profiler/nsIProfileSaveEvent.idl => tools/profiler/gecko/nsIProfileSaveEvent.idl
rename : tools/profiler/nsIProfiler.idl => tools/profiler/gecko/nsIProfiler.idl
rename : tools/profiler/nsProfiler.cpp => tools/profiler/gecko/nsProfiler.cpp
rename : tools/profiler/nsProfiler.h => tools/profiler/gecko/nsProfiler.h
rename : tools/profiler/nsProfilerCIID.h => tools/profiler/gecko/nsProfilerCIID.h
rename : tools/profiler/nsProfilerFactory.cpp => tools/profiler/gecko/nsProfilerFactory.cpp
rename : tools/profiler/nsProfilerStartParams.cpp => tools/profiler/gecko/nsProfilerStartParams.cpp
rename : tools/profiler/nsProfilerStartParams.h => tools/profiler/gecko/nsProfilerStartParams.h
rename : tools/profiler/AutoObjectMapper.cpp => tools/profiler/lul/AutoObjectMapper.cpp
rename : tools/profiler/AutoObjectMapper.h => tools/profiler/lul/AutoObjectMapper.h
rename : tools/profiler/LulCommon.cpp => tools/profiler/lul/LulCommon.cpp
rename : tools/profiler/LulCommonExt.h => tools/profiler/lul/LulCommonExt.h
rename : tools/profiler/LulDwarf.cpp => tools/profiler/lul/LulDwarf.cpp
rename : tools/profiler/LulDwarfExt.h => tools/profiler/lul/LulDwarfExt.h
rename : tools/profiler/LulDwarfInt.h => tools/profiler/lul/LulDwarfInt.h
rename : tools/profiler/LulDwarfSummariser.cpp => tools/profiler/lul/LulDwarfSummariser.cpp
rename : tools/profiler/LulDwarfSummariser.h => tools/profiler/lul/LulDwarfSummariser.h
rename : tools/profiler/LulElf.cpp => tools/profiler/lul/LulElf.cpp
rename : tools/profiler/LulElfExt.h => tools/profiler/lul/LulElfExt.h
rename : tools/profiler/LulElfInt.h => tools/profiler/lul/LulElfInt.h
rename : tools/profiler/LulMain.cpp => tools/profiler/lul/LulMain.cpp
rename : tools/profiler/LulMain.h => tools/profiler/lul/LulMain.h
rename : tools/profiler/LulMainInt.h => tools/profiler/lul/LulMainInt.h
rename : tools/profiler/LulPlatformMacros.h => tools/profiler/lul/LulPlatformMacros.h
rename : tools/profiler/platform-linux-lul.cpp => tools/profiler/lul/platform-linux-lul.cpp
rename : tools/profiler/platform-linux-lul.h => tools/profiler/lul/platform-linux-lul.h
rename : tools/profiler/GeckoProfiler.h => tools/profiler/public/GeckoProfiler.h
rename : tools/profiler/GeckoProfilerFunc.h => tools/profiler/public/GeckoProfilerFunc.h
rename : tools/profiler/GeckoProfilerImpl.h => tools/profiler/public/GeckoProfilerImpl.h
rename : tools/profiler/ProfileGatherer.h => tools/profiler/public/ProfileGatherer.h
rename : tools/profiler/ProfilerBacktrace.h => tools/profiler/public/ProfilerBacktrace.h
rename : tools/profiler/ProfilerMarkers.h => tools/profiler/public/ProfilerMarkers.h
rename : tools/profiler/PseudoStack.h => tools/profiler/public/PseudoStack.h
rename : tools/profiler/shared-libraries.h => tools/profiler/public/shared-libraries.h
rename : tools/profiler/GeckoTaskTracer.cpp => tools/profiler/tasktracer/GeckoTaskTracer.cpp
rename : tools/profiler/GeckoTaskTracer.h => tools/profiler/tasktracer/GeckoTaskTracer.h
rename : tools/profiler/GeckoTaskTracerImpl.h => tools/profiler/tasktracer/GeckoTaskTracerImpl.h
rename : tools/profiler/SourceEventTypeMap.h => tools/profiler/tasktracer/SourceEventTypeMap.h
rename : tools/profiler/TracedTaskCommon.cpp => tools/profiler/tasktracer/TracedTaskCommon.cpp
rename : tools/profiler/TracedTaskCommon.h => tools/profiler/tasktracer/TracedTaskCommon.h
extra : commitid : EGgqHIgsN6z
2015-06-30 15:03:45 -04:00

207 lines
5.9 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "mozilla/Assertions.h"
#include "PlatformMacros.h"
#include "AutoObjectMapper.h"
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
# include <dlfcn.h>
# include "mozilla/Types.h"
// FIXME move these out of mozglue/linker/ElfLoader.h into their
// own header, so as to avoid conflicts arising from two definitions
// of Array
extern "C" {
MFBT_API size_t
__dl_get_mappable_length(void *handle);
MFBT_API void *
__dl_mmap(void *handle, void *addr, size_t length, off_t offset);
MFBT_API void
__dl_munmap(void *handle, void *addr, size_t length);
}
// The following are for get_installation_lib_dir()
# include "nsString.h"
# include "nsDirectoryServiceUtils.h"
# include "nsDirectoryServiceDefs.h"
#endif
// A helper function for creating failure error messages in
// AutoObjectMapper*::Map.
static void
failedToMessage(void(*aLog)(const char*),
const char* aHowFailed, std::string aFileName)
{
char buf[300];
snprintf(buf, sizeof(buf), "AutoObjectMapper::Map: Failed to %s \'%s\'",
aHowFailed, aFileName.c_str());
buf[sizeof(buf)-1] = 0;
aLog(buf);
}
AutoObjectMapperPOSIX::AutoObjectMapperPOSIX(void(*aLog)(const char*))
: mImage(nullptr)
, mSize(0)
, mLog(aLog)
, mIsMapped(false)
{}
AutoObjectMapperPOSIX::~AutoObjectMapperPOSIX() {
if (!mIsMapped) {
// There's nothing to do.
MOZ_ASSERT(!mImage);
MOZ_ASSERT(mSize == 0);
return;
}
MOZ_ASSERT(mSize > 0);
// The following assertion doesn't necessarily have to be true,
// but we assume (reasonably enough) that no mmap facility would
// be crazy enough to map anything at page zero.
MOZ_ASSERT(mImage);
munmap(mImage, mSize);
}
bool AutoObjectMapperPOSIX::Map(/*OUT*/void** start, /*OUT*/size_t* length,
std::string fileName)
{
MOZ_ASSERT(!mIsMapped);
int fd = open(fileName.c_str(), O_RDONLY);
if (fd == -1) {
failedToMessage(mLog, "open", fileName);
return false;
}
struct stat st;
int err = fstat(fd, &st);
size_t sz = (err == 0) ? st.st_size : 0;
if (err != 0 || sz == 0) {
failedToMessage(mLog, "fstat", fileName);
close(fd);
return false;
}
void* image = mmap(nullptr, sz, PROT_READ, MAP_SHARED, fd, 0);
if (image == MAP_FAILED) {
failedToMessage(mLog, "mmap", fileName);
close(fd);
return false;
}
close(fd);
mIsMapped = true;
mImage = *start = image;
mSize = *length = sz;
return true;
}
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
// A helper function for AutoObjectMapperFaultyLib::Map. Finds out
// where the installation's lib directory is, since we'll have to look
// in there to get hold of libmozglue.so. Returned C string is heap
// allocated and the caller must deallocate it.
static char*
get_installation_lib_dir()
{
nsCOMPtr<nsIProperties>
directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
if (!directoryService) {
return nullptr;
}
nsCOMPtr<nsIFile> greDir;
nsresult rv = directoryService->Get(NS_GRE_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(greDir));
if (NS_FAILED(rv)) return nullptr;
nsCString path;
rv = greDir->GetNativePath(path);
if (NS_FAILED(rv)) {
return nullptr;
}
return strdup(path.get());
}
AutoObjectMapperFaultyLib::AutoObjectMapperFaultyLib(void(*aLog)(const char*))
: AutoObjectMapperPOSIX(aLog)
, mHdl(nullptr)
{}
AutoObjectMapperFaultyLib::~AutoObjectMapperFaultyLib() {
if (mHdl) {
// We've got an object mapped by faulty.lib. Unmap it via faulty.lib.
MOZ_ASSERT(mSize > 0);
// Assert on the basis that no valid mapping would start at page zero.
MOZ_ASSERT(mImage);
__dl_munmap(mHdl, mImage, mSize);
dlclose(mHdl);
// Stop assertions in ~AutoObjectMapperPOSIX from failing.
mImage = nullptr;
mSize = 0;
}
// At this point the parent class destructor, ~AutoObjectMapperPOSIX,
// gets called. If that has something mapped in the normal way, it
// will unmap it in the normal way. Unfortunately there's no
// obvious way to enforce the requirement that the object is mapped
// either by faulty.lib or by the parent class, but not by both.
}
bool AutoObjectMapperFaultyLib::Map(/*OUT*/void** start, /*OUT*/size_t* length,
std::string fileName)
{
MOZ_ASSERT(!mHdl);
if (fileName == "libmozglue.so") {
// Do (2) in the comment above.
char* libdir = get_installation_lib_dir();
if (libdir) {
fileName = std::string(libdir) + "/lib/" + fileName;
free(libdir);
}
// Hand the problem off to the standard mapper.
return AutoObjectMapperPOSIX::Map(start, length, fileName);
} else {
// Do cases (1) and (3) in the comment above. We have to
// grapple with faulty.lib directly.
void* hdl = dlopen(fileName.c_str(), RTLD_GLOBAL | RTLD_LAZY);
if (!hdl) {
failedToMessage(mLog, "get handle for ELF file", fileName);
return false;
}
size_t sz = __dl_get_mappable_length(hdl);
if (sz == 0) {
dlclose(hdl);
failedToMessage(mLog, "get size for ELF file", fileName);
return false;
}
void* image = __dl_mmap(hdl, nullptr, sz, 0);
if (image == MAP_FAILED) {
dlclose(hdl);
failedToMessage(mLog, "mmap ELF file", fileName);
return false;
}
mHdl = hdl;
mImage = *start = image;
mSize = *length = sz;
return true;
}
}
#endif // defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)