ProcFileSystem.cpp 10 KB

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