ProcFileSystem.cpp 14 KB

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