HleProcessDebugger.cs 11 KB

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