ProcFileSystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.executable_inode();
  127. ASSERT(inode);
  128. return VirtualFileSystem::the().absolute_path(*inode).toByteBuffer();
  129. }
  130. ByteBuffer procfs$pid_cwd(Process& process)
  131. {
  132. ProcessInspectionHandle handle(process);
  133. auto inode = process.cwd_inode();
  134. ASSERT(inode);
  135. return VirtualFileSystem::the().absolute_path(*inode).toByteBuffer();
  136. }
  137. void ProcFileSystem::addProcess(Process& process)
  138. {
  139. InterruptDisabler disabler;
  140. char buf[16];
  141. ksprintf(buf, "%d", process.pid());
  142. auto dir = addFile(create_directory(buf));
  143. m_pid2inode.set(process.pid(), dir.index());
  144. addFile(create_generated_file("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
  145. addFile(create_generated_file("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
  146. addFile(create_generated_file("regs", [&process] { return procfs$pid_regs(process); }), dir.index());
  147. addFile(create_generated_file("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
  148. if (process.executable_inode())
  149. addFile(create_generated_file("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
  150. addFile(create_generated_file("cwd", [&process] { return procfs$pid_cwd(process); }, 00120777), dir.index());
  151. }
  152. void ProcFileSystem::removeProcess(Process& process)
  153. {
  154. InterruptDisabler disabler;
  155. auto pid = process.pid();
  156. auto it = m_pid2inode.find(pid);
  157. ASSERT(it != m_pid2inode.end());
  158. bool success = removeFile((*it).value);
  159. ASSERT(success);
  160. m_pid2inode.remove(pid);
  161. }
  162. ByteBuffer procfs$mm()
  163. {
  164. // FIXME: Implement
  165. InterruptDisabler disabler;
  166. auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_vmos.size());
  167. char* ptr = (char*)buffer.pointer();
  168. for (auto* vmo : MM.m_vmos) {
  169. ptr += ksprintf(ptr, "VMO: %p %s(%u): p:%4u %s\n",
  170. vmo,
  171. vmo->is_anonymous() ? "anon" : "file",
  172. vmo->retainCount(),
  173. vmo->page_count(),
  174. vmo->name().characters());
  175. }
  176. ptr += ksprintf(ptr, "VMO count: %u\n", MM.m_vmos.size());
  177. ptr += ksprintf(ptr, "Free physical pages: %u\n", MM.m_free_physical_pages.size());
  178. buffer.trim(ptr - (char*)buffer.pointer());
  179. return buffer;
  180. }
  181. ByteBuffer procfs$regions()
  182. {
  183. // FIXME: Implement
  184. InterruptDisabler disabler;
  185. auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_regions.size());
  186. char* ptr = (char*)buffer.pointer();
  187. for (auto* region : MM.m_regions) {
  188. ptr += ksprintf(ptr, "Region: %p VMO=%p %s\n",
  189. region,
  190. &region->vmo(),
  191. region->name.characters());
  192. }
  193. ptr += ksprintf(ptr, "Region count: %u\n", MM.m_regions.size());
  194. buffer.trim(ptr - (char*)buffer.pointer());
  195. return buffer;
  196. }
  197. ByteBuffer procfs$mounts()
  198. {
  199. InterruptDisabler disabler;
  200. auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80);
  201. char* ptr = (char*)buffer.pointer();
  202. VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) {
  203. auto& fs = mount.fileSystem();
  204. ptr += ksprintf(ptr, "%s @ ", fs.className());
  205. if (!mount.host().isValid())
  206. ptr += ksprintf(ptr, "/\n", fs.className());
  207. else
  208. ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index());
  209. });
  210. buffer.trim(ptr - (char*)buffer.pointer());
  211. return buffer;
  212. }
  213. ByteBuffer procfs$cpuinfo()
  214. {
  215. auto buffer = ByteBuffer::createUninitialized(256);
  216. char* ptr = (char*)buffer.pointer();
  217. {
  218. CPUID cpuid(0);
  219. ptr += ksprintf(ptr, "cpuid: ");
  220. auto emit_dword = [&] (dword value) {
  221. ptr += ksprintf(ptr, "%c%c%c%c",
  222. value & 0xff,
  223. (value >> 8) & 0xff,
  224. (value >> 16) & 0xff,
  225. (value >> 24) & 0xff);
  226. };
  227. emit_dword(cpuid.ebx());
  228. emit_dword(cpuid.edx());
  229. emit_dword(cpuid.ecx());
  230. ptr += ksprintf(ptr, "\n");
  231. }
  232. {
  233. CPUID cpuid(1);
  234. dword stepping = cpuid.eax() & 0xf;
  235. dword model = (cpuid.eax() >> 4) & 0xf;
  236. dword family = (cpuid.eax() >> 8) & 0xf;
  237. dword type = (cpuid.eax() >> 12) & 0x3;
  238. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  239. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  240. dword display_model;
  241. dword display_family;
  242. if (family == 15) {
  243. display_family = family + extended_family;
  244. display_model = model + (extended_model << 4);
  245. } else if (family == 6) {
  246. display_family = family;
  247. display_model = model + (extended_model << 4);
  248. } else {
  249. display_family = family;
  250. display_model = model;
  251. }
  252. ptr += ksprintf(ptr, "family: %u\n", display_family);
  253. ptr += ksprintf(ptr, "model: %u\n", display_model);
  254. ptr += ksprintf(ptr, "stepping: %u\n", stepping);
  255. ptr += ksprintf(ptr, "type: %u\n", type);
  256. }
  257. {
  258. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  259. // and verifying that the returned eax>=0x80000004.
  260. char buffer[48];
  261. dword* bufptr = reinterpret_cast<dword*>(buffer);
  262. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  263. CPUID cpuid(0x80000002 + i);
  264. *bufptr++ = cpuid.eax();
  265. *bufptr++ = cpuid.ebx();
  266. *bufptr++ = cpuid.ecx();
  267. *bufptr++ = cpuid.edx();
  268. };
  269. copy_brand_string_part_to_buffer(0);
  270. copy_brand_string_part_to_buffer(1);
  271. copy_brand_string_part_to_buffer(2);
  272. ptr += ksprintf(ptr, "brandstr: \"%s\"\n", buffer);
  273. }
  274. buffer.trim(ptr - (char*)buffer.pointer());
  275. return buffer;
  276. }
  277. ByteBuffer procfs$kmalloc()
  278. {
  279. auto buffer = ByteBuffer::createUninitialized(256);
  280. char* ptr = (char*)buffer.pointer();
  281. ptr += ksprintf(ptr, "eternal: %u\npage-aligned: %u\nallocated: %u\nfree: %u\n", kmalloc_sum_eternal, sum_alloc, sum_free);
  282. buffer.trim(ptr - (char*)buffer.pointer());
  283. return buffer;
  284. }
  285. ByteBuffer procfs$summary()
  286. {
  287. InterruptDisabler disabler;
  288. auto processes = Process::allProcesses();
  289. auto buffer = ByteBuffer::createUninitialized(processes.size() * 256);
  290. char* ptr = (char*)buffer.pointer();
  291. ptr += ksprintf(ptr, "PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  292. for (auto* process : processes) {
  293. ptr += ksprintf(ptr, "% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  294. process->pid(),
  295. process->tty() ? process->tty()->pgid() : 0,
  296. process->pgid(),
  297. process->sid(),
  298. process->uid(),
  299. toString(process->state()),
  300. process->ppid(),
  301. process->timesScheduled(),
  302. process->number_of_open_file_descriptors(),
  303. process->tty() ? strrchr(process->tty()->ttyName().characters(), '/') + 1 : "n/a",
  304. process->name().characters());
  305. }
  306. *ptr = '\0';
  307. buffer.trim(ptr - (char*)buffer.pointer());
  308. return buffer;
  309. }
  310. ByteBuffer procfs$vnodes()
  311. {
  312. auto& vfs = VirtualFileSystem::the();
  313. auto buffer = ByteBuffer::createUninitialized(vfs.m_maxNodeCount * 256);
  314. char* ptr = (char*)buffer.pointer();
  315. for (size_t i = 0; i < vfs.m_maxNodeCount; ++i) {
  316. auto& vnode = vfs.m_nodes[i];
  317. // FIXME: Retain the vnode while inspecting it.
  318. if (!vnode.inUse())
  319. continue;
  320. String path;
  321. if (vnode.core_inode())
  322. path = vfs.absolute_path(*vnode.core_inode());
  323. if (path.isEmpty()) {
  324. if (auto* dev = vnode.characterDevice()) {
  325. if (dev->isTTY())
  326. path = static_cast<const TTY*>(dev)->ttyName();
  327. }
  328. }
  329. ptr += ksprintf(ptr, "vnode %03u: %02u:%08u (%u) %s\n", i, vnode.inode.fileSystemID(), vnode.inode.index(), vnode.retain_count(), path.characters());
  330. }
  331. *ptr = '\0';
  332. buffer.trim(ptr - (char*)buffer.pointer());
  333. return buffer;
  334. }
  335. bool ProcFileSystem::initialize()
  336. {
  337. SyntheticFileSystem::initialize();
  338. addFile(create_generated_file("mm", procfs$mm));
  339. addFile(create_generated_file("regions", procfs$regions));
  340. addFile(create_generated_file("mounts", procfs$mounts));
  341. addFile(create_generated_file("kmalloc", procfs$kmalloc));
  342. addFile(create_generated_file("summary", procfs$summary));
  343. addFile(create_generated_file("cpuinfo", procfs$cpuinfo));
  344. addFile(create_generated_file("vnodes", procfs$vnodes));
  345. return true;
  346. }
  347. const char* ProcFileSystem::className() const
  348. {
  349. return "procfs";
  350. }