ProcFileSystem.cpp 14 KB

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