ProcFileSystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. ProcessInspectionHandle handle(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* descriptor = process.file_descriptor(i);
  34. if (!descriptor)
  35. continue;
  36. ptr += ksprintf(ptr, "% 3u %s\n", i, descriptor->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. ProcessInspectionHandle handle(process);
  44. char* buffer;
  45. auto stringImpl = StringImpl::createUninitialized(80 + process.regionCount() * 160 + 4096, 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. ptr += ksprintf(ptr, "VMO: %s \"%s\" @ %x(%u)\n",
  56. region->vmo().is_anonymous() ? "anonymous" : "file-backed",
  57. region->vmo().name().characters(),
  58. &region->vmo(),
  59. region->vmo().retainCount());
  60. for (size_t i = 0; i < region->vmo().page_count(); ++i) {
  61. auto& physical_page = region->vmo().physical_pages()[i];
  62. ptr += ksprintf(ptr, "P%x%s(%u) ",
  63. physical_page ? physical_page->paddr().get() : 0,
  64. region->cow_map.get(i) ? "!" : "",
  65. physical_page ? physical_page->retain_count() : 0
  66. );
  67. }
  68. ptr += ksprintf(ptr, "\n");
  69. }
  70. *ptr = '\0';
  71. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  72. }
  73. ByteBuffer procfs$pid_stack(Process& process)
  74. {
  75. ProcessInspectionHandle handle(process);
  76. ProcessPagingScope pagingScope(process);
  77. struct RecognizedSymbol {
  78. dword address;
  79. const KSym* ksym;
  80. };
  81. Vector<RecognizedSymbol> recognizedSymbols;
  82. if (auto* eipKsym = ksymbolicate(process.tss().eip))
  83. recognizedSymbols.append({ process.tss().eip, eipKsym });
  84. for (dword* stackPtr = (dword*)process.framePtr(); process.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  85. dword retaddr = stackPtr[1];
  86. if (auto* ksym = ksymbolicate(retaddr))
  87. recognizedSymbols.append({ retaddr, ksym });
  88. }
  89. size_t bytesNeeded = 0;
  90. for (auto& symbol : recognizedSymbols) {
  91. bytesNeeded += symbol.ksym->name.length() + 8 + 16;
  92. }
  93. auto buffer = ByteBuffer::createUninitialized(bytesNeeded);
  94. char* bufptr = (char*)buffer.pointer();
  95. for (auto& symbol : recognizedSymbols) {
  96. // FIXME: This doesn't actually create a file!
  97. unsigned offset = symbol.address - symbol.ksym->address;
  98. bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
  99. }
  100. buffer.trim(bufptr - (char*)buffer.pointer());
  101. return buffer;
  102. }
  103. ByteBuffer procfs$pid_regs(Process& process)
  104. {
  105. ProcessInspectionHandle handle(process);
  106. auto& tss = process.tss();
  107. auto buffer = ByteBuffer::createUninitialized(1024);
  108. char* ptr = (char*)buffer.pointer();
  109. ptr += ksprintf(ptr, "eax: %x\n", tss.eax);
  110. ptr += ksprintf(ptr, "ebx: %x\n", tss.ebx);
  111. ptr += ksprintf(ptr, "ecx: %x\n", tss.ecx);
  112. ptr += ksprintf(ptr, "edx: %x\n", tss.edx);
  113. ptr += ksprintf(ptr, "esi: %x\n", tss.esi);
  114. ptr += ksprintf(ptr, "edi: %x\n", tss.edi);
  115. ptr += ksprintf(ptr, "ebp: %x\n", tss.ebp);
  116. ptr += ksprintf(ptr, "cr3: %x\n", tss.cr3);
  117. ptr += ksprintf(ptr, "flg: %x\n", tss.eflags);
  118. ptr += ksprintf(ptr, "sp: %w:%x\n", tss.ss, tss.esp);
  119. ptr += ksprintf(ptr, "pc: %w:%x\n", tss.cs, tss.eip);
  120. buffer.trim(ptr - (char*)buffer.pointer());
  121. return buffer;
  122. }
  123. ByteBuffer procfs$pid_exe(Process& process)
  124. {
  125. ProcessInspectionHandle handle(process);
  126. auto inode = process.executableInode();
  127. return VirtualFileSystem::the().absolutePath(inode).toByteBuffer();
  128. }
  129. ByteBuffer procfs$pid_cwd(Process& process)
  130. {
  131. ProcessInspectionHandle handle(process);
  132. auto inode = process.cwdInode();
  133. return VirtualFileSystem::the().absolutePath(inode).toByteBuffer();
  134. }
  135. void ProcFileSystem::addProcess(Process& process)
  136. {
  137. InterruptDisabler disabler;
  138. char buf[16];
  139. ksprintf(buf, "%d", process.pid());
  140. auto dir = addFile(createDirectory(buf));
  141. m_pid2inode.set(process.pid(), dir.index());
  142. addFile(createGeneratedFile("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
  143. addFile(createGeneratedFile("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
  144. addFile(createGeneratedFile("regs", [&process] { return procfs$pid_regs(process); }), dir.index());
  145. addFile(createGeneratedFile("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
  146. if (process.executableInode().isValid())
  147. addFile(createGeneratedFile("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
  148. addFile(createGeneratedFile("cwd", [&process] { return procfs$pid_cwd(process); }, 00120777), dir.index());
  149. }
  150. void ProcFileSystem::removeProcess(Process& process)
  151. {
  152. InterruptDisabler disabler;
  153. auto pid = process.pid();
  154. auto it = m_pid2inode.find(pid);
  155. ASSERT(it != m_pid2inode.end());
  156. bool success = removeFile((*it).value);
  157. ASSERT(success);
  158. m_pid2inode.remove(pid);
  159. }
  160. ByteBuffer procfs$mm()
  161. {
  162. // FIXME: Implement
  163. InterruptDisabler disabler;
  164. auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_vmos.size());
  165. char* ptr = (char*)buffer.pointer();
  166. for (auto* vmo : MM.m_vmos) {
  167. ptr += ksprintf(ptr, "VMO: %p %s(%u): p:%4u %s\n",
  168. vmo,
  169. vmo->is_anonymous() ? "anon" : "file",
  170. vmo->retainCount(),
  171. vmo->page_count(),
  172. vmo->name().characters());
  173. }
  174. ptr += ksprintf(ptr, "VMO count: %u\n", MM.m_vmos.size());
  175. ptr += ksprintf(ptr, "Free physical pages: %u\n", MM.m_free_physical_pages.size());
  176. buffer.trim(ptr - (char*)buffer.pointer());
  177. return buffer;
  178. }
  179. ByteBuffer procfs$regions()
  180. {
  181. // FIXME: Implement
  182. InterruptDisabler disabler;
  183. auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_regions.size());
  184. char* ptr = (char*)buffer.pointer();
  185. for (auto* region : MM.m_regions) {
  186. ptr += ksprintf(ptr, "Region: %p VMO=%p %s\n",
  187. region,
  188. &region->vmo(),
  189. region->name.characters());
  190. }
  191. ptr += ksprintf(ptr, "Region count: %u\n", MM.m_regions.size());
  192. buffer.trim(ptr - (char*)buffer.pointer());
  193. return buffer;
  194. }
  195. ByteBuffer procfs$mounts()
  196. {
  197. InterruptDisabler disabler;
  198. auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80);
  199. char* ptr = (char*)buffer.pointer();
  200. VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) {
  201. auto& fs = mount.fileSystem();
  202. ptr += ksprintf(ptr, "%s @ ", fs.className());
  203. if (!mount.host().isValid())
  204. ptr += ksprintf(ptr, "/\n", fs.className());
  205. else
  206. ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index());
  207. });
  208. buffer.trim(ptr - (char*)buffer.pointer());
  209. return buffer;
  210. }
  211. ByteBuffer procfs$cpuinfo()
  212. {
  213. auto buffer = ByteBuffer::createUninitialized(256);
  214. char* ptr = (char*)buffer.pointer();
  215. {
  216. CPUID cpuid(0);
  217. ptr += ksprintf(ptr, "cpuid: ");
  218. auto emit_dword = [&] (dword value) {
  219. ptr += ksprintf(ptr, "%c%c%c%c",
  220. value & 0xff,
  221. (value >> 8) & 0xff,
  222. (value >> 16) & 0xff,
  223. (value >> 24) & 0xff);
  224. };
  225. emit_dword(cpuid.ebx());
  226. emit_dword(cpuid.edx());
  227. emit_dword(cpuid.ecx());
  228. ptr += ksprintf(ptr, "\n");
  229. }
  230. {
  231. CPUID cpuid(1);
  232. dword stepping = cpuid.eax() & 0xf;
  233. dword model = (cpuid.eax() >> 4) & 0xf;
  234. dword family = (cpuid.eax() >> 8) & 0xf;
  235. dword type = (cpuid.eax() >> 12) & 0x3;
  236. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  237. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  238. dword display_model;
  239. dword display_family;
  240. if (family == 15) {
  241. display_family = family + extended_family;
  242. display_model = model + (extended_model << 4);
  243. } else if (family == 6) {
  244. display_family = family;
  245. display_model = model + (extended_model << 4);
  246. } else {
  247. display_family = family;
  248. display_model = model;
  249. }
  250. ptr += ksprintf(ptr, "family: %u\n", display_family);
  251. ptr += ksprintf(ptr, "model: %u\n", display_model);
  252. ptr += ksprintf(ptr, "stepping: %u\n", stepping);
  253. ptr += ksprintf(ptr, "type: %u\n", type);
  254. }
  255. {
  256. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  257. // and verifying that the returned eax>=0x80000004.
  258. char buffer[48];
  259. dword* bufptr = reinterpret_cast<dword*>(buffer);
  260. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  261. CPUID cpuid(0x80000002 + i);
  262. *bufptr++ = cpuid.eax();
  263. *bufptr++ = cpuid.ebx();
  264. *bufptr++ = cpuid.ecx();
  265. *bufptr++ = cpuid.edx();
  266. };
  267. copy_brand_string_part_to_buffer(0);
  268. copy_brand_string_part_to_buffer(1);
  269. copy_brand_string_part_to_buffer(2);
  270. ptr += ksprintf(ptr, "brandstr: \"%s\"\n", buffer);
  271. }
  272. buffer.trim(ptr - (char*)buffer.pointer());
  273. return buffer;
  274. }
  275. ByteBuffer procfs$kmalloc()
  276. {
  277. auto buffer = ByteBuffer::createUninitialized(256);
  278. char* ptr = (char*)buffer.pointer();
  279. ptr += ksprintf(ptr, "eternal: %u\npage-aligned: %u\nallocated: %u\nfree: %u\n", kmalloc_sum_eternal, sum_alloc, sum_free);
  280. buffer.trim(ptr - (char*)buffer.pointer());
  281. return buffer;
  282. }
  283. ByteBuffer procfs$summary()
  284. {
  285. InterruptDisabler disabler;
  286. auto processes = Process::allProcesses();
  287. auto buffer = ByteBuffer::createUninitialized(processes.size() * 256);
  288. char* ptr = (char*)buffer.pointer();
  289. ptr += ksprintf(ptr, "PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  290. for (auto* process : processes) {
  291. ptr += ksprintf(ptr, "% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  292. process->pid(),
  293. process->tty() ? process->tty()->pgid() : 0,
  294. process->pgid(),
  295. process->sid(),
  296. process->uid(),
  297. toString(process->state()),
  298. process->ppid(),
  299. process->timesScheduled(),
  300. process->number_of_open_file_descriptors(),
  301. process->tty() ? strrchr(process->tty()->ttyName().characters(), '/') + 1 : "n/a",
  302. process->name().characters());
  303. }
  304. *ptr = '\0';
  305. buffer.trim(ptr - (char*)buffer.pointer());
  306. return buffer;
  307. }
  308. ByteBuffer procfs$vnodes()
  309. {
  310. auto& vfs = VirtualFileSystem::the();
  311. auto buffer = ByteBuffer::createUninitialized(vfs.m_maxNodeCount * 256);
  312. char* ptr = (char*)buffer.pointer();
  313. for (size_t i = 0; i < vfs.m_maxNodeCount; ++i) {
  314. auto& vnode = vfs.m_nodes[i];
  315. // FIXME: Retain the vnode while inspecting it.
  316. if (!vnode.inUse())
  317. continue;
  318. auto path = vfs.absolutePath(vnode.inode);
  319. if (path.isEmpty()) {
  320. if (auto* dev = vnode.characterDevice()) {
  321. if (dev->isTTY())
  322. path = static_cast<const TTY*>(dev)->ttyName();
  323. }
  324. }
  325. ptr += ksprintf(ptr, "vnode %03u: %02u:%08u (%u) %s\n", i, vnode.inode.fileSystemID(), vnode.inode.index(), vnode.retain_count(), path.characters());
  326. }
  327. *ptr = '\0';
  328. buffer.trim(ptr - (char*)buffer.pointer());
  329. return buffer;
  330. }
  331. bool ProcFileSystem::initialize()
  332. {
  333. SyntheticFileSystem::initialize();
  334. addFile(createGeneratedFile("mm", procfs$mm));
  335. addFile(createGeneratedFile("regions", procfs$regions));
  336. addFile(createGeneratedFile("mounts", procfs$mounts));
  337. addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
  338. addFile(createGeneratedFile("summary", procfs$summary));
  339. addFile(createGeneratedFile("cpuinfo", procfs$cpuinfo));
  340. addFile(createGeneratedFile("vnodes", procfs$vnodes));
  341. return true;
  342. }
  343. const char* ProcFileSystem::className() const
  344. {
  345. return "procfs";
  346. }