Skip to content

Commit

Permalink
Greatly improve /proc/PID/stack by tracing the ebp frame chain.
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
awesomekling committed Oct 26, 2018
1 parent c928b06 commit 2716a9e
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 115 deletions.
5 changes: 5 additions & 0 deletions AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class ByteBuffer {
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)); }
Expand Down
211 changes: 113 additions & 98 deletions Kernel/ProcFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Task.h"
#include <VirtualFileSystem/VirtualFileSystem.h>
#include "system.h"
#include "MemoryManager.h"

static ProcFileSystem* s_the;

Expand All @@ -25,65 +26,73 @@ 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();
char buf[16];
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)
Expand All @@ -97,56 +106,62 @@ void ProcFileSystem::removeProcess(Task& task)
m_pid2inode.remove(pid);
}

bool ProcFileSystem::initialize()
ByteBuffer procfs$mounts()
{
SyntheticFileSystem::initialize();
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("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;
}

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;
}));
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;
}

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;
}));
bool ProcFileSystem::initialize()
{
SyntheticFileSystem::initialize();
addFile(createGeneratedFile("mounts", procfs$mounts));
addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
addFile(createGeneratedFile("summary", procfs$summary));
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions Kernel/StdLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
Expand Down
20 changes: 20 additions & 0 deletions Kernel/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions Kernel/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ class Task : public InlineLinkedListNode<Task> {

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();
Expand Down
Binary file modified Kernel/_fs_contents
Binary file not shown.
6 changes: 3 additions & 3 deletions Kernel/i386.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
11 changes: 11 additions & 0 deletions Kernel/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
5 changes: 5 additions & 0 deletions Kernel/kmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 2 additions & 0 deletions Kernel/kmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions Kernel/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct KSym {
};

Vector<KSym>& ksyms() PURE;
const KSym* ksymbolicate(dword address) PURE;

struct system_t
{
Expand Down
4 changes: 4 additions & 0 deletions Kernel/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class LinearAddress {
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); }
Expand Down
2 changes: 1 addition & 1 deletion Userland/cat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 2716a9e

Please sign in to comment.