ProcFileSystem.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "ProcFileSystem.h"
  2. #include "Process.h"
  3. #include <VirtualFileSystem/VirtualFileSystem.h>
  4. #include "system.h"
  5. #include "MemoryManager.h"
  6. #include "StdLib.h"
  7. static ProcFileSystem* s_the;
  8. ProcFileSystem& ProcFileSystem::the()
  9. {
  10. ASSERT(s_the);
  11. return *s_the;
  12. }
  13. RetainPtr<ProcFileSystem> ProcFileSystem::create()
  14. {
  15. return adopt(*new ProcFileSystem);
  16. }
  17. ProcFileSystem::ProcFileSystem()
  18. {
  19. s_the = this;
  20. }
  21. ProcFileSystem::~ProcFileSystem()
  22. {
  23. }
  24. ByteBuffer procfs$pid_fds(Process& process)
  25. {
  26. ProcessInspectionScope scope(process);
  27. char* buffer;
  28. auto stringImpl = StringImpl::createUninitialized(process.number_of_open_file_descriptors() * 80, buffer);
  29. memset(buffer, 0, stringImpl->length());
  30. char* ptr = buffer;
  31. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  32. auto* handle = process.file_descriptor(i);
  33. if (!handle)
  34. continue;
  35. ptr += ksprintf(ptr, "% 3u %s\n", i, handle->absolute_path().characters());
  36. }
  37. *ptr = '\0';
  38. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  39. }
  40. ByteBuffer procfs$pid_vm(Process& process)
  41. {
  42. ProcessInspectionScope scope(process);
  43. char* buffer;
  44. auto stringImpl = StringImpl::createUninitialized(80 + process.regionCount() * 80 + 80 + process.subregionCount() * 80, buffer);
  45. memset(buffer, 0, stringImpl->length());
  46. char* ptr = buffer;
  47. ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
  48. for (auto& region : process.regions()) {
  49. ptr += ksprintf(ptr, "%x -- %x %x %s\n",
  50. region->linearAddress.get(),
  51. region->linearAddress.offset(region->size - 1).get(),
  52. region->size,
  53. region->name.characters());
  54. }
  55. if (process.subregionCount()) {
  56. ptr += ksprintf(ptr, "\nREGION OFFSET BEGIN END SIZE NAME\n");
  57. for (auto& subregion : process.subregions()) {
  58. ptr += ksprintf(ptr, "%x %x %x -- %x %x %s\n",
  59. subregion->region->linearAddress.get(),
  60. subregion->offset,
  61. subregion->linearAddress.get(),
  62. subregion->linearAddress.offset(subregion->size - 1).get(),
  63. subregion->size,
  64. subregion->name.characters());
  65. }
  66. }
  67. *ptr = '\0';
  68. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  69. }
  70. ByteBuffer procfs$pid_stack(Process& process)
  71. {
  72. ProcessInspectionScope scope(process);
  73. ProcessPagingScope pagingScope(process);
  74. struct RecognizedSymbol {
  75. dword address;
  76. const KSym* ksym;
  77. };
  78. Vector<RecognizedSymbol> recognizedSymbols;
  79. if (auto* eipKsym = ksymbolicate(process.tss().eip))
  80. recognizedSymbols.append({ process.tss().eip, eipKsym });
  81. for (dword* stackPtr = (dword*)process.framePtr(); process.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  82. dword retaddr = stackPtr[1];
  83. if (auto* ksym = ksymbolicate(retaddr))
  84. recognizedSymbols.append({ retaddr, ksym });
  85. }
  86. size_t bytesNeeded = 0;
  87. for (auto& symbol : recognizedSymbols) {
  88. bytesNeeded += symbol.ksym->name.length() + 8 + 16;
  89. }
  90. auto buffer = ByteBuffer::createUninitialized(bytesNeeded);
  91. char* bufptr = (char*)buffer.pointer();
  92. for (auto& symbol : recognizedSymbols) {
  93. // FIXME: This doesn't actually create a file!
  94. unsigned offset = symbol.address - symbol.ksym->address;
  95. bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
  96. }
  97. buffer.trim(bufptr - (char*)buffer.pointer());
  98. return buffer;
  99. }
  100. ByteBuffer procfs$pid_exe(Process& process)
  101. {
  102. ProcessInspectionScope scope(process);
  103. auto inode = process.executableInode();
  104. return VirtualFileSystem::the().absolutePath(inode).toByteBuffer();
  105. }
  106. void ProcFileSystem::addProcess(Process& process)
  107. {
  108. InterruptDisabler disabler;
  109. char buf[16];
  110. ksprintf(buf, "%d", process.pid());
  111. auto dir = addFile(createDirectory(buf));
  112. m_pid2inode.set(process.pid(), dir.index());
  113. addFile(createGeneratedFile("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
  114. addFile(createGeneratedFile("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
  115. addFile(createGeneratedFile("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
  116. if (process.executableInode().isValid())
  117. addFile(createGeneratedFile("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
  118. }
  119. void ProcFileSystem::removeProcess(Process& process)
  120. {
  121. InterruptDisabler disabler;
  122. auto pid = process.pid();
  123. auto it = m_pid2inode.find(pid);
  124. ASSERT(it != m_pid2inode.end());
  125. bool success = removeFile((*it).value);
  126. ASSERT(success);
  127. m_pid2inode.remove(pid);
  128. }
  129. ByteBuffer procfs$mm()
  130. {
  131. InterruptDisabler disabler;
  132. size_t zonePageCount = 0;
  133. for (auto* zone : MM.m_zones)
  134. zonePageCount += zone->m_pages.size();
  135. auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_zones.size() + zonePageCount * 10);
  136. char* ptr = (char*)buffer.pointer();
  137. for (auto* zone : MM.m_zones) {
  138. ptr += ksprintf(ptr, "Zone %p size: %u\n ", zone, zone->size());
  139. for (auto page : zone->m_pages)
  140. ptr += ksprintf(ptr, "%x ", page);
  141. ptr += ksprintf(ptr, "\n");
  142. }
  143. ptr += ksprintf(ptr, "Zone count: %u\n", MM.m_zones.size());
  144. ptr += ksprintf(ptr, "Free physical pages: %u\n", MM.m_freePages.size());
  145. buffer.trim(ptr - (char*)buffer.pointer());
  146. return buffer;
  147. }
  148. ByteBuffer procfs$mounts()
  149. {
  150. InterruptDisabler disabler;
  151. auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80);
  152. char* ptr = (char*)buffer.pointer();
  153. VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) {
  154. auto& fs = mount.fileSystem();
  155. ptr += ksprintf(ptr, "%s @ ", fs.className());
  156. if (!mount.host().isValid())
  157. ptr += ksprintf(ptr, "/\n", fs.className());
  158. else
  159. ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index());
  160. });
  161. buffer.trim(ptr - (char*)buffer.pointer());
  162. return buffer;
  163. }
  164. ByteBuffer procfs$kmalloc()
  165. {
  166. InterruptDisabler disabler;
  167. auto buffer = ByteBuffer::createUninitialized(256);
  168. char* ptr = (char*)buffer.pointer();
  169. ptr += ksprintf(ptr, "eternal: %u\npage-aligned: %u\nallocated: %u\nfree: %u\n", kmalloc_sum_eternal, sum_alloc, sum_free);
  170. buffer.trim(ptr - (char*)buffer.pointer());
  171. return buffer;
  172. }
  173. static const char* toString(Process::State state)
  174. {
  175. switch (state) {
  176. case Process::Invalid: return "Invalid";
  177. case Process::Runnable: return "Runnable";
  178. case Process::Running: return "Running";
  179. case Process::Terminated: return "Term";
  180. case Process::Crashing: return "Crash";
  181. case Process::Exiting: return "Exit";
  182. case Process::BlockedSleep: return "Sleep";
  183. case Process::BlockedWait: return "Wait";
  184. case Process::BlockedRead: return "Read";
  185. case Process::BeingInspected: return "Inspect";
  186. }
  187. ASSERT_NOT_REACHED();
  188. return nullptr;
  189. }
  190. ByteBuffer procfs$summary()
  191. {
  192. InterruptDisabler disabler;
  193. auto processes = Process::allProcesses();
  194. auto buffer = ByteBuffer::createUninitialized(processes.size() * 256);
  195. char* ptr = (char*)buffer.pointer();
  196. ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  197. for (auto* process : processes) {
  198. ptr += ksprintf(ptr, "% 5u % 4u % 8s % 5u % 10u % 3u % 4s %s\n",
  199. process->pid(),
  200. process->uid(),
  201. toString(process->state()),
  202. process->parentPID(),
  203. process->timesScheduled(),
  204. process->number_of_open_file_descriptors(),
  205. process->tty() ? strrchr(process->tty()->ttyName().characters(), '/') + 1 : "n/a",
  206. process->name().characters());
  207. }
  208. *ptr = '\0';
  209. buffer.trim(ptr - (char*)buffer.pointer());
  210. return buffer;
  211. }
  212. bool ProcFileSystem::initialize()
  213. {
  214. SyntheticFileSystem::initialize();
  215. addFile(createGeneratedFile("mm", procfs$mm));
  216. addFile(createGeneratedFile("mounts", procfs$mounts));
  217. addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
  218. addFile(createGeneratedFile("summary", procfs$summary));
  219. return true;
  220. }
  221. const char* ProcFileSystem::className() const
  222. {
  223. return "procfs";
  224. }