ProcFileSystem.cpp 13 KB

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