HleProcessDebugger.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using Ryujinx.HLE.HOS.Diagnostics.Demangler;
  2. using Ryujinx.HLE.HOS.Kernel.Memory;
  3. using Ryujinx.HLE.Loaders.Elf;
  4. using Ryujinx.Memory;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. namespace Ryujinx.HLE.HOS.Kernel.Process
  10. {
  11. class HleProcessDebugger
  12. {
  13. private const int Mod0 = 'M' << 0 | 'O' << 8 | 'D' << 16 | '0' << 24;
  14. private KProcess _owner;
  15. private class Image
  16. {
  17. public ulong BaseAddress { get; }
  18. public ElfSymbol[] Symbols { get; }
  19. public Image(ulong baseAddress, ElfSymbol[] symbols)
  20. {
  21. BaseAddress = baseAddress;
  22. Symbols = symbols;
  23. }
  24. }
  25. private List<Image> _images;
  26. private int _loaded;
  27. public HleProcessDebugger(KProcess owner)
  28. {
  29. _owner = owner;
  30. _images = new List<Image>();
  31. }
  32. public string GetGuestStackTrace(ARMeilleure.State.ExecutionContext context)
  33. {
  34. EnsureLoaded();
  35. StringBuilder trace = new StringBuilder();
  36. void AppendTrace(ulong address)
  37. {
  38. Image image = GetImage(address, out int imageIndex);
  39. if (image == null || !TryGetSubName(image, address, out string subName))
  40. {
  41. subName = $"Sub{address:x16}";
  42. }
  43. else if (subName.StartsWith("_Z"))
  44. {
  45. subName = Demangler.Parse(subName);
  46. }
  47. if (image != null)
  48. {
  49. ulong offset = address - image.BaseAddress;
  50. string imageName = GetGuessedNsoNameFromIndex(imageIndex);
  51. trace.AppendLine($" {imageName}:0x{offset:x8} {subName}");
  52. }
  53. else
  54. {
  55. trace.AppendLine($" ??? {subName}");
  56. }
  57. }
  58. trace.AppendLine($"Process: {_owner.Name}, PID: {_owner.Pid}");
  59. if (context.IsAarch32)
  60. {
  61. ulong framePointer = context.GetX(11);
  62. while (framePointer != 0)
  63. {
  64. if ((framePointer & 3) != 0 ||
  65. !_owner.CpuMemory.IsMapped(framePointer) ||
  66. !_owner.CpuMemory.IsMapped(framePointer + 4))
  67. {
  68. break;
  69. }
  70. AppendTrace(_owner.CpuMemory.Read<uint>(framePointer + 4));
  71. framePointer = _owner.CpuMemory.Read<uint>(framePointer);
  72. }
  73. }
  74. else
  75. {
  76. ulong framePointer = context.GetX(29);
  77. while (framePointer != 0)
  78. {
  79. if ((framePointer & 7) != 0 ||
  80. !_owner.CpuMemory.IsMapped(framePointer) ||
  81. !_owner.CpuMemory.IsMapped(framePointer + 8))
  82. {
  83. break;
  84. }
  85. AppendTrace(_owner.CpuMemory.Read<ulong>(framePointer + 8));
  86. framePointer = _owner.CpuMemory.Read<ulong>(framePointer);
  87. }
  88. }
  89. return trace.ToString();
  90. }
  91. private bool TryGetSubName(Image image, ulong address, out string name)
  92. {
  93. address -= image.BaseAddress;
  94. int left = 0;
  95. int right = image.Symbols.Length - 1;
  96. while (left <= right)
  97. {
  98. int size = right - left;
  99. int middle = left + (size >> 1);
  100. ElfSymbol symbol = image.Symbols[middle];
  101. ulong endAddr = symbol.Value + symbol.Size;
  102. if ((ulong)address >= symbol.Value && (ulong)address < endAddr)
  103. {
  104. name = symbol.Name;
  105. return true;
  106. }
  107. if ((ulong)address < (ulong)symbol.Value)
  108. {
  109. right = middle - 1;
  110. }
  111. else
  112. {
  113. left = middle + 1;
  114. }
  115. }
  116. name = null;
  117. return false;
  118. }
  119. private Image GetImage(ulong address, out int index)
  120. {
  121. lock (_images)
  122. {
  123. for (index = _images.Count - 1; index >= 0; index--)
  124. {
  125. if (address >= _images[index].BaseAddress)
  126. {
  127. return _images[index];
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. private string GetGuessedNsoNameFromIndex(int index)
  134. {
  135. if ((uint)index > 11)
  136. {
  137. return "???";
  138. }
  139. if (index == 0)
  140. {
  141. return "rtld";
  142. }
  143. else if (index == 1)
  144. {
  145. return "main";
  146. }
  147. else if (index == GetImagesCount() - 1)
  148. {
  149. return "sdk";
  150. }
  151. else
  152. {
  153. return "subsdk" + (index - 2);
  154. }
  155. }
  156. private int GetImagesCount()
  157. {
  158. lock (_images)
  159. {
  160. return _images.Count;
  161. }
  162. }
  163. private void EnsureLoaded()
  164. {
  165. if (Interlocked.CompareExchange(ref _loaded, 1, 0) == 0)
  166. {
  167. ScanMemoryForTextSegments();
  168. }
  169. }
  170. private void ScanMemoryForTextSegments()
  171. {
  172. ulong oldAddress = 0;
  173. ulong address = 0;
  174. while (address >= oldAddress)
  175. {
  176. KMemoryInfo info = _owner.MemoryManager.QueryMemory(address);
  177. if (info.State == MemoryState.Reserved)
  178. {
  179. break;
  180. }
  181. if (info.State == MemoryState.CodeStatic && info.Permission == KMemoryPermission.ReadAndExecute)
  182. {
  183. LoadMod0Symbols(_owner.CpuMemory, info.Address);
  184. }
  185. oldAddress = address;
  186. address = info.Address + info.Size;
  187. }
  188. }
  189. private void LoadMod0Symbols(IVirtualMemoryManager memory, ulong textOffset)
  190. {
  191. ulong mod0Offset = textOffset + memory.Read<uint>(textOffset + 4);
  192. if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0)
  193. {
  194. return;
  195. }
  196. Dictionary<ElfDynamicTag, ulong> dynamic = new Dictionary<ElfDynamicTag, ulong>();
  197. int mod0Magic = memory.Read<int>(mod0Offset + 0x0);
  198. if (mod0Magic != Mod0)
  199. {
  200. return;
  201. }
  202. ulong dynamicOffset = memory.Read<uint>(mod0Offset + 0x4) + mod0Offset;
  203. ulong bssStartOffset = memory.Read<uint>(mod0Offset + 0x8) + mod0Offset;
  204. ulong bssEndOffset = memory.Read<uint>(mod0Offset + 0xc) + mod0Offset;
  205. ulong ehHdrStartOffset = memory.Read<uint>(mod0Offset + 0x10) + mod0Offset;
  206. ulong ehHdrEndOffset = memory.Read<uint>(mod0Offset + 0x14) + mod0Offset;
  207. ulong modObjOffset = memory.Read<uint>(mod0Offset + 0x18) + mod0Offset;
  208. bool isAArch32 = memory.Read<ulong>(dynamicOffset) > 0xFFFFFFFF || memory.Read<ulong>(dynamicOffset + 0x10) > 0xFFFFFFFF;
  209. while (true)
  210. {
  211. ulong tagVal;
  212. ulong value;
  213. if (isAArch32)
  214. {
  215. tagVal = memory.Read<uint>(dynamicOffset + 0);
  216. value = memory.Read<uint>(dynamicOffset + 4);
  217. dynamicOffset += 0x8;
  218. }
  219. else
  220. {
  221. tagVal = memory.Read<ulong>(dynamicOffset + 0);
  222. value = memory.Read<ulong>(dynamicOffset + 8);
  223. dynamicOffset += 0x10;
  224. }
  225. ElfDynamicTag tag = (ElfDynamicTag)tagVal;
  226. if (tag == ElfDynamicTag.DT_NULL)
  227. {
  228. break;
  229. }
  230. dynamic[tag] = value;
  231. }
  232. if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out ulong strTab) ||
  233. !dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out ulong symTab) ||
  234. !dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out ulong symEntSize))
  235. {
  236. return;
  237. }
  238. ulong strTblAddr = textOffset + strTab;
  239. ulong symTblAddr = textOffset + symTab;
  240. List<ElfSymbol> symbols = new List<ElfSymbol>();
  241. while (symTblAddr < strTblAddr)
  242. {
  243. ElfSymbol sym = isAArch32 ? GetSymbol32(memory, symTblAddr, strTblAddr) : GetSymbol64(memory, symTblAddr, strTblAddr);
  244. symbols.Add(sym);
  245. symTblAddr += symEntSize;
  246. }
  247. lock (_images)
  248. {
  249. _images.Add(new Image(textOffset, symbols.OrderBy(x => x.Value).ToArray()));
  250. }
  251. }
  252. private ElfSymbol GetSymbol64(IVirtualMemoryManager memory, ulong address, ulong strTblAddr)
  253. {
  254. ElfSymbol64 sym = memory.Read<ElfSymbol64>(address);
  255. uint nameIndex = sym.NameOffset;
  256. string name = string.Empty;
  257. for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
  258. {
  259. name += (char)chr;
  260. }
  261. return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
  262. }
  263. private ElfSymbol GetSymbol32(IVirtualMemoryManager memory, ulong address, ulong strTblAddr)
  264. {
  265. ElfSymbol32 sym = memory.Read<ElfSymbol32>(address);
  266. uint nameIndex = sym.NameOffset;
  267. string name = string.Empty;
  268. for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
  269. {
  270. name += (char)chr;
  271. }
  272. return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
  273. }
  274. }
  275. }