Greatly improve /proc/PID/stack by tracing the ebp frame chain.

I also added a generator cache to FileHandle. This way, multiple
reads to a generated file (i.e in a synthfs) can transparently
handle multiple calls to read() without the contents changing
between calls.

The cache is discarded at EOF (or when the FileHandle is destroyed.)
This commit is contained in:
Andreas Kling 2018-10-27 00:14:24 +02:00
parent c928b06218
commit 2716a9e2d7
Notes: sideshowbarker 2024-07-19 18:37:57 +09:00
22 changed files with 210 additions and 116 deletions

View file

@ -24,6 +24,11 @@ public:
m_impl = move(other.m_impl);
return *this;
}
ByteBuffer& operator=(const ByteBuffer& other)
{
m_impl = other.m_impl.copyRef();
return *this;
}
static ByteBuffer createEmpty() { return ByteBuffer(Buffer<byte>::createUninitialized(0)); }
static ByteBuffer createUninitialized(size_t size) { return ByteBuffer(Buffer<byte>::createUninitialized(size)); }

View file

@ -2,6 +2,7 @@
#include "Task.h"
#include <VirtualFileSystem/VirtualFileSystem.h>
#include "system.h"
#include "MemoryManager.h"
static ProcFileSystem* s_the;
@ -25,6 +26,64 @@ ProcFileSystem::~ProcFileSystem()
{
}
ByteBuffer procfs$pid_vm(const Task& task)
{
InterruptDisabler disabler;
char* buffer;
auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer);
memset(buffer, 0, stringImpl->length());
char* ptr = buffer;
ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
for (auto& region : task.regions()) {
ptr += ksprintf(ptr, "%x -- %x %x %s\n",
region->linearAddress.get(),
region->linearAddress.offset(region->size - 1).get(),
region->size,
region->name.characters());
}
*ptr = '\0';
return ByteBuffer::copy((byte*)buffer, ptr - buffer);
}
ByteBuffer procfs$pid_stack(Task& task)
{
InterruptDisabler disabler;
if (current != &task) {
MemoryManager::the().unmapRegionsForTask(*current);
MemoryManager::the().mapRegionsForTask(task);
}
struct RecognizedSymbol {
dword address;
const KSym* ksym;
};
Vector<RecognizedSymbol> recognizedSymbols;
if (auto* eipKsym = ksymbolicate(task.tss().eip))
recognizedSymbols.append({ task.tss().eip, eipKsym });
for (dword* stackPtr = (dword*)task.framePtr(); task.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
dword retaddr = stackPtr[1];
if (auto* ksym = ksymbolicate(retaddr))
recognizedSymbols.append({ retaddr, ksym });
}
size_t bytesNeeded = 0;
for (auto& symbol : recognizedSymbols) {
bytesNeeded += symbol.ksym->name.length() + 8 + 16;
}
auto buffer = ByteBuffer::createUninitialized(bytesNeeded);
char* bufptr = (char*)buffer.pointer();
for (auto& symbol : recognizedSymbols) {
// FIXME: This doesn't actually create a file!
unsigned offset = symbol.address - symbol.ksym->address;
bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
}
buffer.trim(bufptr - (char*)buffer.pointer());
if (current != &task) {
MemoryManager::the().unmapRegionsForTask(task);
MemoryManager::the().mapRegionsForTask(*current);
}
return buffer;
}
void ProcFileSystem::addProcess(Task& task)
{
ASSERT_INTERRUPTS_DISABLED();
@ -32,58 +91,8 @@ void ProcFileSystem::addProcess(Task& task)
ksprintf(buf, "%d", task.pid());
auto dir = addFile(createDirectory(buf));
m_pid2inode.set(task.pid(), dir.index());
addFile(createGeneratedFile("vm", [&task] {
InterruptDisabler disabler;
char* buffer;
auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer);
memset(buffer, 0, stringImpl->length());
char* ptr = buffer;
ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
for (auto& region : task.regions()) {
ptr += ksprintf(ptr, "%x -- %x %x %s\n",
region->linearAddress.get(),
region->linearAddress.offset(region->size - 1).get(),
region->size,
region->name.characters());
}
*ptr = '\0';
return ByteBuffer::copy((byte*)buffer, ptr - buffer);
}), dir.index());
addFile(createGeneratedFile("stack", [&task] {
InterruptDisabler disabler;
auto& syms = ksyms();
dword firstKsymAddress = syms.first().address;
dword lastKsymAddress = syms.last().address;
struct RecognizedSymbol {
dword address;
const char* name;
dword offset;
};
Vector<RecognizedSymbol> recognizedSymbols;
size_t bytesNeeded = 0;
for (dword* stackPtr = (dword*)task.stackPtr(); (dword)stackPtr < task.stackTop(); ++stackPtr) {
if (*stackPtr < firstKsymAddress || *stackPtr > lastKsymAddress)
continue;
const char* name = nullptr;
unsigned offset = 0;
for (unsigned i = 0; i < syms.size(); ++i) {
if (*stackPtr < syms[i+1].address) {
name = syms[i].name.characters();
offset = *stackPtr - syms[i].address;
bytesNeeded += syms[i].name.length() + 8 + 16;
break;
}
}
recognizedSymbols.append({ *stackPtr, name, offset });
}
auto buffer = ByteBuffer::createUninitialized(bytesNeeded);
char* ptr = (char*)buffer.pointer();
for (auto& symbol : recognizedSymbols) {
kprintf("%p %s +%u\n", symbol.address, symbol.name, symbol.offset);
}
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}), dir.index());
addFile(createGeneratedFile("vm", [&task] { return procfs$pid_vm(task); }), dir.index());
addFile(createGeneratedFile("stack", [&task] { return procfs$pid_stack(task); }), dir.index());
}
void ProcFileSystem::removeProcess(Task& task)
@ -97,56 +106,62 @@ void ProcFileSystem::removeProcess(Task& task)
m_pid2inode.remove(pid);
}
ByteBuffer procfs$mounts()
{
InterruptDisabler disabler;
auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80);
char* ptr = (char*)buffer.pointer();
VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) {
auto& fs = mount.fileSystem();
ptr += ksprintf(ptr, "%s @ ", fs.className());
if (!mount.host().isValid())
ptr += ksprintf(ptr, "/\n", fs.className());
else
ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index());
});
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
ByteBuffer procfs$kmalloc()
{
InterruptDisabler disabler;
auto buffer = ByteBuffer::createUninitialized(128);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "alloc: %u\nfree: %u\n", sum_alloc, sum_free);
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
ByteBuffer procfs$summary()
{
InterruptDisabler disabler;
auto tasks = Task::allTasks();
auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n");
for (auto* task : tasks) {
ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n",
task->pid(),
task->uid(),
task->gid(),
task->state(),
task->parentPID(),
task->timesScheduled(),
task->fileHandleCount(),
task->name().characters());
}
*ptr = '\0';
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
bool ProcFileSystem::initialize()
{
SyntheticFileSystem::initialize();
addFile(createGeneratedFile("mounts", [] {
InterruptDisabler disabler;
auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80);
char* ptr = (char*)buffer.pointer();
VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) {
auto& fs = mount.fileSystem();
ptr += ksprintf(ptr, "%s @ ", fs.className());
if (!mount.host().isValid())
ptr += ksprintf(ptr, "/\n", fs.className());
else
ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index());
});
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}));
addFile(createGeneratedFile("kmalloc", [] {
InterruptDisabler disabler;
auto buffer = ByteBuffer::createUninitialized(128);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "alloc: %u\nfree: %u\n", sum_alloc, sum_free);
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}));
addFile(createGeneratedFile("summary", [] {
InterruptDisabler disabler;
auto tasks = Task::allTasks();
auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n");
for (auto* task : tasks) {
ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n",
task->pid(),
task->uid(),
task->gid(),
task->state(),
task->parentPID(),
task->timesScheduled(),
task->fileHandleCount(),
task->name().characters());
}
*ptr = '\0';
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}));
addFile(createGeneratedFile("mounts", procfs$mounts));
addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
addFile(createGeneratedFile("summary", procfs$summary));
return true;
}

View file

@ -2,7 +2,17 @@
#include "types.h"
#if 0
inline void memcpy(void *dest, const void *src, DWORD n)
{
BYTE* bdest = (BYTE*)dest;
const BYTE* bsrc = (const BYTE*)src;
for (; n; --n)
*(bdest++) = *(bsrc++);
}
#else
void memcpy(void*, const void*, DWORD);
#endif
void strcpy(char*, const char*);
int strcmp(char const*, const char*);
DWORD strlen(const char*);

View file

@ -885,3 +885,23 @@ Task::Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n)
Task::Region::~Region()
{
}
bool Task::isValidAddressForKernel(LinearAddress laddr) const
{
InterruptDisabler disabler;
if (laddr.get() >= ksyms().first().address && laddr.get() <= ksyms().last().address)
return true;
if (is_kmalloc_address((void*)laddr.get()))
return true;
return isValidAddressForUser(laddr);
}
bool Task::isValidAddressForUser(LinearAddress laddr) const
{
InterruptDisabler disabler;
for (auto& region: m_regions) {
if (laddr >= region->linearAddress && laddr < region->linearAddress.offset(region->size))
return true;
}
return false;
}

View file

@ -122,9 +122,13 @@ public:
size_t fileHandleCount() const { return m_fileHandles.size(); }
dword framePtr() const { return m_tss.ebp; }
dword stackPtr() const { return m_tss.esp; }
dword stackTop() const { return m_tss.ss == 0x10 ? m_stackTop0 : m_stackTop3; }
bool isValidAddressForKernel(LinearAddress) const;
bool isValidAddressForUser(LinearAddress) const;
private:
friend class MemoryManager;
friend bool scheduleNewTask();

Binary file not shown.

View file

@ -196,10 +196,10 @@ void exception_14_handler()
asm ("movl %%cr2, %%eax":"=a"(faultAddress));
auto& regs = *reinterpret_cast<RegisterDump*>(exception_state_dump);
kprintf("%s page fault: %u(%s), %s laddr=%p\n",
current->isRing0() ? "Kernel" : "User",
current->pid(),
kprintf("Ring%u page fault in %s(%u), %s laddr=%p\n",
regs.cs & 3,
current->name().characters(),
current->pid(),
exception_code & 2 ? "write" : "read",
faultAddress);

View file

@ -65,6 +65,17 @@ Vector<KSym>& ksyms()
return *s_ksyms;
}
const KSym* ksymbolicate(dword address)
{
if (address < ksyms().first().address || address > ksyms().last().address)
return nullptr;
for (unsigned i = 0; i < ksyms().size(); ++i) {
if (address < ksyms()[i + 1].address)
return &ksyms()[i];
}
return nullptr;
}
static void loadKernelMap(const ByteBuffer& buffer)
{
s_ksyms = new Vector<KSym>;

View file

@ -29,6 +29,11 @@ PRIVATE BYTE alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
volatile DWORD sum_alloc = 0;
volatile DWORD sum_free = POOL_SIZE;
bool is_kmalloc_address(void* ptr)
{
return ptr >= (void*)BASE_PHYS && ptr <= ((void*)BASE_PHYS + POOL_SIZE);
}
PUBLIC void
kmalloc_init()
{

View file

@ -4,6 +4,8 @@ void kmalloc_init();
void *kmalloc(DWORD size) __attribute__ ((malloc));
void kfree(void*);
bool is_kmalloc_address(void*);
extern volatile DWORD sum_alloc;
extern volatile DWORD sum_free;

View file

@ -10,6 +10,7 @@ struct KSym {
};
Vector<KSym>& ksyms() PURE;
const KSym* ksymbolicate(dword address) PURE;
struct system_t
{

View file

@ -74,6 +74,10 @@ public:
void set(dword address) { m_address = address; }
void mask(dword m) { m_address &= m; }
bool operator<=(const LinearAddress& other) const { return m_address <= other.m_address; }
bool operator>=(const LinearAddress& other) const { return m_address >= other.m_address; }
bool operator>(const LinearAddress& other) const { return m_address > other.m_address; }
bool operator<(const LinearAddress& other) const { return m_address < other.m_address; }
bool operator==(const LinearAddress& other) const { return m_address == other.m_address; }
byte* asPtr() { return reinterpret_cast<byte*>(m_address); }

View file

@ -15,7 +15,7 @@ int main(int argc, char** argv)
return 1;
}
for (;;) {
char buf[4096];
char buf[1024];
ssize_t nread = read(fd, buf, sizeof(buf));
if (nread == 0)
break;

View file

@ -268,7 +268,7 @@ Vector<unsigned> Ext2FileSystem::blockListForInode(const ext2_inode& e2inode) co
return list;
}
Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer) const
Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const
{
ASSERT(offset >= 0);
ASSERT(inode.fileSystemID() == id());
@ -293,6 +293,7 @@ Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t
static const unsigned maxInlineSymlinkLength = 60;
if (isSymbolicLink(e2inode->i_mode) && e2inode->i_size < maxInlineSymlinkLength) {
Unix::ssize_t nread = min((Unix::off_t)e2inode->i_size - offset, static_cast<Unix::off_t>(count));
kprintf("nread = %d\n", nread);
memcpy(buffer, e2inode->i_block + offset, nread);
return nread;
}

View file

@ -44,7 +44,7 @@ private:
virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
virtual bool setModificationTime(InodeIdentifier, dword timestamp) override;
virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override;
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override;
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const override;
virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
bool isDirectoryInode(unsigned) const;

View file

@ -102,7 +102,7 @@ Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count)
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->read(buffer, count);
}
Unix::ssize_t nread = m_vnode->fileSystem()->readInodeBytes(m_vnode->inode, m_currentOffset, count, buffer);
Unix::ssize_t nread = m_vnode->fileSystem()->readInodeBytes(m_vnode->inode, m_currentOffset, count, buffer, this);
m_currentOffset += nread;
return nread;
}
@ -125,7 +125,7 @@ ByteBuffer FileHandle::readEntireFile()
return buffer;
}
return m_vnode->fileSystem()->readEntireInode(m_vnode->inode);
return m_vnode->fileSystem()->readEntireInode(m_vnode->inode, this);
}
bool FileHandle::isDirectory() const

View file

@ -32,6 +32,8 @@ public:
void setBlocking(bool b) { m_isBlocking = b; }
#endif
ByteBuffer& generatorCache() { return m_generatorCache; }
private:
friend class VirtualFileSystem;
@ -39,6 +41,8 @@ private:
Unix::off_t m_currentOffset { 0 };
ByteBuffer m_generatorCache;
#ifdef SERENITY
int m_fd { -1 };
bool m_isBlocking { true };

View file

@ -50,7 +50,7 @@ InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode,
return foundInode;
}
ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const
ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode, FileHandle* handle) const
{
ASSERT(inode.fileSystemID() == id());
@ -67,7 +67,7 @@ ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const
byte* out = contents.pointer();
Unix::off_t offset = 0;
for (;;) {
nread = readInodeBytes(inode, offset, sizeof(buffer), buffer);
nread = readInodeBytes(inode, offset, sizeof(buffer), buffer, handle);
if (nread <= 0)
break;
memcpy(out, buffer, nread);

View file

@ -16,6 +16,8 @@
static const dword mepoch = 476763780;
class FileHandle;
class FileSystem : public Retainable<FileSystem> {
public:
static void initializeGlobals();
@ -30,7 +32,7 @@ public:
virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0;
virtual InodeMetadata inodeMetadata(InodeIdentifier) const = 0;
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const = 0;
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const = 0;
struct DirectoryEntry {
String name;
@ -44,7 +46,7 @@ public:
virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) = 0;
InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name) const;
ByteBuffer readEntireInode(InodeIdentifier) const;
ByteBuffer readEntireInode(InodeIdentifier, FileHandle* = nullptr) const;
protected:
FileSystem();

View file

@ -5,5 +5,5 @@ ByteBuffer InodeIdentifier::readEntireFile() const
{
if (!fileSystem())
return { };
return fileSystem()->readEntireInode(*this);
return fileSystem()->readEntireInode(*this, nullptr);
}

View file

@ -1,4 +1,5 @@
#include "SyntheticFileSystem.h"
#include "FileHandle.h"
#include <AK/StdLib.h>
//#define SYNTHFS_DEBUG
@ -188,7 +189,7 @@ bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&)
return false;
}
Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer) const
Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle* handle) const
{
InterruptDisabler disabler;
@ -204,12 +205,21 @@ Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::o
return false;
const File& file = *(*it).value;
ByteBuffer generatedData;
if (file.generator)
generatedData = file.generator();
if (file.generator) {
if (!handle) {
generatedData = file.generator();
} else {
if (!handle->generatorCache())
handle->generatorCache() = file.generator();
generatedData = handle->generatorCache();
}
}
auto* data = generatedData ? &generatedData : &file.data;
Unix::ssize_t nread = min(static_cast<Unix::off_t>(data->size() - offset), static_cast<Unix::off_t>(count));
memcpy(buffer, data->pointer() + offset, nread);
if (nread == 0 && handle && handle->generatorCache())
handle->generatorCache().clear();
return nread;
}

View file

@ -17,7 +17,7 @@ public:
virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
virtual bool setModificationTime(InodeIdentifier, dword timestamp) override;
virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override;
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override;
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const override;
virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
protected: