ProcFileSystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "i386.h"
  8. static ProcFileSystem* s_the;
  9. ProcFileSystem& ProcFileSystem::the()
  10. {
  11. ASSERT(s_the);
  12. return *s_the;
  13. }
  14. RetainPtr<ProcFileSystem> ProcFileSystem::create()
  15. {
  16. return adopt(*new ProcFileSystem);
  17. }
  18. ProcFileSystem::ProcFileSystem()
  19. {
  20. s_the = this;
  21. }
  22. ProcFileSystem::~ProcFileSystem()
  23. {
  24. }
  25. ByteBuffer procfs$pid_fds(Process& process)
  26. {
  27. ProcessInspectionScope scope(process);
  28. char* buffer;
  29. auto stringImpl = StringImpl::createUninitialized(process.number_of_open_file_descriptors() * 80, buffer);
  30. memset(buffer, 0, stringImpl->length());
  31. char* ptr = buffer;
  32. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  33. auto* handle = process.file_descriptor(i);
  34. if (!handle)
  35. continue;
  36. ptr += ksprintf(ptr, "% 3u %s\n", i, handle->absolute_path().characters());
  37. }
  38. *ptr = '\0';
  39. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  40. }
  41. ByteBuffer procfs$pid_vm(Process& process)
  42. {
  43. ProcessInspectionScope scope(process);
  44. char* buffer;
  45. auto stringImpl = StringImpl::createUninitialized(80 + process.regionCount() * 80 + 80 + process.subregionCount() * 80, buffer);
  46. memset(buffer, 0, stringImpl->length());
  47. char* ptr = buffer;
  48. ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
  49. for (auto& region : process.regions()) {
  50. ptr += ksprintf(ptr, "%x -- %x %x %s\n",
  51. region->linearAddress.get(),
  52. region->linearAddress.offset(region->size - 1).get(),
  53. region->size,
  54. region->name.characters());
  55. }
  56. if (process.subregionCount()) {
  57. ptr += ksprintf(ptr, "\nREGION OFFSET BEGIN END SIZE NAME\n");
  58. for (auto& subregion : process.subregions()) {
  59. ptr += ksprintf(ptr, "%x %x %x -- %x %x %s\n",
  60. subregion->region->linearAddress.get(),
  61. subregion->offset,
  62. subregion->linearAddress.get(),
  63. subregion->linearAddress.offset(subregion->size - 1).get(),
  64. subregion->size,
  65. subregion->name.characters());
  66. }
  67. }
  68. *ptr = '\0';
  69. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  70. }
  71. ByteBuffer procfs$pid_stack(Process& process)
  72. {
  73. ProcessInspectionScope scope(process);
  74. ProcessPagingScope pagingScope(process);
  75. struct RecognizedSymbol {
  76. dword address;
  77. const KSym* ksym;
  78. };
  79. Vector<RecognizedSymbol> recognizedSymbols;
  80. if (auto* eipKsym = ksymbolicate(process.tss().eip))
  81. recognizedSymbols.append({ process.tss().eip, eipKsym });
  82. for (dword* stackPtr = (dword*)process.framePtr(); process.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  83. dword retaddr = stackPtr[1];
  84. if (auto* ksym = ksymbolicate(retaddr))
  85. recognizedSymbols.append({ retaddr, ksym });
  86. }
  87. size_t bytesNeeded = 0;
  88. for (auto& symbol : recognizedSymbols) {
  89. bytesNeeded += symbol.ksym->name.length() + 8 + 16;
  90. }
  91. auto buffer = ByteBuffer::createUninitialized(bytesNeeded);
  92. char* bufptr = (char*)buffer.pointer();
  93. for (auto& symbol : recognizedSymbols) {
  94. // FIXME: This doesn't actually create a file!
  95. unsigned offset = symbol.address - symbol.ksym->address;
  96. bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
  97. }
  98. buffer.trim(bufptr - (char*)buffer.pointer());
  99. return buffer;
  100. }
  101. ByteBuffer procfs$pid_exe(Process& process)
  102. {
  103. ProcessInspectionScope scope(process);
  104. auto inode = process.executableInode();
  105. return VirtualFileSystem::the().absolutePath(inode).toByteBuffer();
  106. }
  107. void ProcFileSystem::addProcess(Process& process)
  108. {
  109. InterruptDisabler disabler;
  110. char buf[16];
  111. ksprintf(buf, "%d", process.pid());
  112. auto dir = addFile(createDirectory(buf));
  113. m_pid2inode.set(process.pid(), dir.index());
  114. addFile(createGeneratedFile("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
  115. addFile(createGeneratedFile("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
  116. addFile(createGeneratedFile("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
  117. if (process.executableInode().isValid())
  118. addFile(createGeneratedFile("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
  119. }
  120. void ProcFileSystem::removeProcess(Process& process)
  121. {
  122. InterruptDisabler disabler;
  123. auto pid = process.pid();
  124. auto it = m_pid2inode.find(pid);
  125. ASSERT(it != m_pid2inode.end());
  126. bool success = removeFile((*it).value);
  127. ASSERT(success);
  128. m_pid2inode.remove(pid);
  129. }
  130. ByteBuffer procfs$mm()
  131. {
  132. InterruptDisabler disabler;
  133. size_t zonePageCount = 0;
  134. for (auto* zone : MM.m_zones)
  135. zonePageCount += zone->m_pages.size();
  136. auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_zones.size() + zonePageCount * 10);
  137. char* ptr = (char*)buffer.pointer();
  138. for (auto* zone : MM.m_zones) {
  139. ptr += ksprintf(ptr, "Zone %p size: %u\n ", zone, zone->size());
  140. for (auto page : zone->m_pages)
  141. ptr += ksprintf(ptr, "%x ", page);
  142. ptr += ksprintf(ptr, "\n");
  143. }
  144. ptr += ksprintf(ptr, "Zone count: %u\n", MM.m_zones.size());
  145. ptr += ksprintf(ptr, "Free physical pages: %u\n", MM.m_freePages.size());
  146. buffer.trim(ptr - (char*)buffer.pointer());
  147. return buffer;
  148. }
  149. ByteBuffer procfs$mounts()
  150. {
  151. InterruptDisabler disabler;
  152. auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80);
  153. char* ptr = (char*)buffer.pointer();
  154. VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) {
  155. auto& fs = mount.fileSystem();
  156. ptr += ksprintf(ptr, "%s @ ", fs.className());
  157. if (!mount.host().isValid())
  158. ptr += ksprintf(ptr, "/\n", fs.className());
  159. else
  160. ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index());
  161. });
  162. buffer.trim(ptr - (char*)buffer.pointer());
  163. return buffer;
  164. }
  165. ByteBuffer procfs$cpuinfo()
  166. {
  167. auto buffer = ByteBuffer::createUninitialized(256);
  168. char* ptr = (char*)buffer.pointer();
  169. {
  170. CPUID cpuid(0);
  171. ptr += ksprintf(ptr, "cpuid: ");
  172. auto emit_dword = [&] (dword value) {
  173. ptr += ksprintf(ptr, "%c%c%c%c",
  174. value & 0xff,
  175. (value >> 8) & 0xff,
  176. (value >> 16) & 0xff,
  177. (value >> 24) & 0xff);
  178. };
  179. emit_dword(cpuid.ebx());
  180. emit_dword(cpuid.edx());
  181. emit_dword(cpuid.ecx());
  182. ptr += ksprintf(ptr, "\n");
  183. }
  184. {
  185. CPUID cpuid(1);
  186. dword stepping = cpuid.eax() & 0xf;
  187. dword model = (cpuid.eax() >> 4) & 0xf;
  188. dword family = (cpuid.eax() >> 8) & 0xf;
  189. dword type = (cpuid.eax() >> 12) & 0x3;
  190. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  191. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  192. dword display_model;
  193. dword display_family;
  194. if (family == 15) {
  195. display_family = family + extended_family;
  196. display_model = model + (extended_model << 4);
  197. } else if (family == 6) {
  198. display_family = family;
  199. display_model = model + (extended_model << 4);
  200. } else {
  201. display_family = family;
  202. display_model = model;
  203. }
  204. ptr += ksprintf(ptr, "family: %u\n", display_family);
  205. ptr += ksprintf(ptr, "model: %u\n", display_model);
  206. ptr += ksprintf(ptr, "stepping: %u\n", stepping);
  207. ptr += ksprintf(ptr, "type: %u\n", type);
  208. }
  209. {
  210. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  211. // and verifying that the returned eax>=0x80000004.
  212. char buffer[48];
  213. dword* bufptr = reinterpret_cast<dword*>(buffer);
  214. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  215. CPUID cpuid(0x80000002 + i);
  216. *bufptr++ = cpuid.eax();
  217. *bufptr++ = cpuid.ebx();
  218. *bufptr++ = cpuid.ecx();
  219. *bufptr++ = cpuid.edx();
  220. };
  221. copy_brand_string_part_to_buffer(0);
  222. copy_brand_string_part_to_buffer(1);
  223. copy_brand_string_part_to_buffer(2);
  224. ptr += ksprintf(ptr, "brandstr: \"%s\"\n", buffer);
  225. }
  226. buffer.trim(ptr - (char*)buffer.pointer());
  227. return buffer;
  228. }
  229. ByteBuffer procfs$kmalloc()
  230. {
  231. InterruptDisabler disabler;
  232. auto buffer = ByteBuffer::createUninitialized(256);
  233. char* ptr = (char*)buffer.pointer();
  234. ptr += ksprintf(ptr, "eternal: %u\npage-aligned: %u\nallocated: %u\nfree: %u\n", kmalloc_sum_eternal, sum_alloc, sum_free);
  235. buffer.trim(ptr - (char*)buffer.pointer());
  236. return buffer;
  237. }
  238. static const char* toString(Process::State state)
  239. {
  240. switch (state) {
  241. case Process::Invalid: return "Invalid";
  242. case Process::Runnable: return "Runnable";
  243. case Process::Running: return "Running";
  244. case Process::Terminated: return "Term";
  245. case Process::Crashing: return "Crash";
  246. case Process::Exiting: return "Exit";
  247. case Process::BlockedSleep: return "Sleep";
  248. case Process::BlockedWait: return "Wait";
  249. case Process::BlockedRead: return "Read";
  250. case Process::BeingInspected: return "Inspect";
  251. }
  252. ASSERT_NOT_REACHED();
  253. return nullptr;
  254. }
  255. ByteBuffer procfs$summary()
  256. {
  257. InterruptDisabler disabler;
  258. auto processes = Process::allProcesses();
  259. auto buffer = ByteBuffer::createUninitialized(processes.size() * 256);
  260. char* ptr = (char*)buffer.pointer();
  261. ptr += ksprintf(ptr, "PID PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  262. for (auto* process : processes) {
  263. ptr += ksprintf(ptr, "% 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  264. process->pid(),
  265. process->pgid(),
  266. process->sid(),
  267. process->uid(),
  268. toString(process->state()),
  269. process->parentPID(),
  270. process->timesScheduled(),
  271. process->number_of_open_file_descriptors(),
  272. process->tty() ? strrchr(process->tty()->ttyName().characters(), '/') + 1 : "n/a",
  273. process->name().characters());
  274. }
  275. *ptr = '\0';
  276. buffer.trim(ptr - (char*)buffer.pointer());
  277. return buffer;
  278. }
  279. bool ProcFileSystem::initialize()
  280. {
  281. SyntheticFileSystem::initialize();
  282. addFile(createGeneratedFile("mm", procfs$mm));
  283. addFile(createGeneratedFile("mounts", procfs$mounts));
  284. addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
  285. addFile(createGeneratedFile("summary", procfs$summary));
  286. addFile(createGeneratedFile("cpuinfo", procfs$cpuinfo));
  287. return true;
  288. }
  289. const char* ProcFileSystem::className() const
  290. {
  291. return "procfs";
  292. }