HleProcessDebugger.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using ARMeilleure.Memory;
  2. using Ryujinx.HLE.HOS.Diagnostics.Demangler;
  3. using Ryujinx.HLE.HOS.Kernel.Memory;
  4. using Ryujinx.HLE.Loaders.Elf;
  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 long BaseAddress { get; private set; }
  18. public ElfSymbol[] Symbols { get; private set; }
  19. public Image(long 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(long 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. long 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. // TODO: ARM32.
  59. long framePointer = (long)context.GetX(29);
  60. trace.AppendLine($"Process: {_owner.Name}, PID: {_owner.Pid}");
  61. while (framePointer != 0)
  62. {
  63. if ((framePointer & 7) != 0 ||
  64. !_owner.CpuMemory.IsMapped(framePointer) ||
  65. !_owner.CpuMemory.IsMapped(framePointer + 8))
  66. {
  67. break;
  68. }
  69. // Note: This is the return address, we need to subtract one instruction
  70. // worth of bytes to get the branch instruction address.
  71. AppendTrace(_owner.CpuMemory.ReadInt64(framePointer + 8) - 4);
  72. framePointer = _owner.CpuMemory.ReadInt64(framePointer);
  73. }
  74. return trace.ToString();
  75. }
  76. private bool TryGetSubName(Image image, long address, out string name)
  77. {
  78. address -= image.BaseAddress;
  79. int left = 0;
  80. int right = image.Symbols.Length - 1;
  81. while (left <= right)
  82. {
  83. int size = right - left;
  84. int middle = left + (size >> 1);
  85. ElfSymbol symbol = image.Symbols[middle];
  86. long endAddr = symbol.Value + symbol.Size;
  87. if ((ulong)address >= (ulong)symbol.Value && (ulong)address < (ulong)endAddr)
  88. {
  89. name = symbol.Name;
  90. return true;
  91. }
  92. if ((ulong)address < (ulong)symbol.Value)
  93. {
  94. right = middle - 1;
  95. }
  96. else
  97. {
  98. left = middle + 1;
  99. }
  100. }
  101. name = null;
  102. return false;
  103. }
  104. private Image GetImage(long address, out int index)
  105. {
  106. lock (_images)
  107. {
  108. for (index = _images.Count - 1; index >= 0; index--)
  109. {
  110. if ((ulong)address >= (ulong)_images[index].BaseAddress)
  111. {
  112. return _images[index];
  113. }
  114. }
  115. }
  116. return null;
  117. }
  118. private string GetGuessedNsoNameFromIndex(int index)
  119. {
  120. if ((uint)index > 11)
  121. {
  122. return "???";
  123. }
  124. if (index == 0)
  125. {
  126. return "rtld";
  127. }
  128. else if (index == 1)
  129. {
  130. return "main";
  131. }
  132. else if (index == GetImagesCount() - 1)
  133. {
  134. return "sdk";
  135. }
  136. else
  137. {
  138. return "subsdk" + (index - 2);
  139. }
  140. }
  141. private int GetImagesCount()
  142. {
  143. lock (_images)
  144. {
  145. return _images.Count;
  146. }
  147. }
  148. private void EnsureLoaded()
  149. {
  150. if (Interlocked.CompareExchange(ref _loaded, 1, 0) == 0)
  151. {
  152. ScanMemoryForTextSegments();
  153. }
  154. }
  155. private void ScanMemoryForTextSegments()
  156. {
  157. ulong oldAddress = 0;
  158. ulong address = 0;
  159. while (address >= oldAddress)
  160. {
  161. KMemoryInfo info = _owner.MemoryManager.QueryMemory(address);
  162. if (info.State == MemoryState.Reserved)
  163. {
  164. break;
  165. }
  166. if (info.State == MemoryState.CodeStatic && info.Permission == MemoryPermission.ReadAndExecute)
  167. {
  168. LoadMod0Symbols(_owner.CpuMemory, (long)info.Address);
  169. }
  170. oldAddress = address;
  171. address = info.Address + info.Size;
  172. }
  173. }
  174. private void LoadMod0Symbols(MemoryManager memory, long textOffset)
  175. {
  176. long mod0Offset = textOffset + memory.ReadUInt32(textOffset + 4);
  177. if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0)
  178. {
  179. return;
  180. }
  181. Dictionary<ElfDynamicTag, long> dynamic = new Dictionary<ElfDynamicTag, long>();
  182. int mod0Magic = memory.ReadInt32(mod0Offset + 0x0);
  183. if (mod0Magic != Mod0)
  184. {
  185. return;
  186. }
  187. long dynamicOffset = memory.ReadInt32(mod0Offset + 0x4) + mod0Offset;
  188. long bssStartOffset = memory.ReadInt32(mod0Offset + 0x8) + mod0Offset;
  189. long bssEndOffset = memory.ReadInt32(mod0Offset + 0xc) + mod0Offset;
  190. long ehHdrStartOffset = memory.ReadInt32(mod0Offset + 0x10) + mod0Offset;
  191. long ehHdrEndOffset = memory.ReadInt32(mod0Offset + 0x14) + mod0Offset;
  192. long modObjOffset = memory.ReadInt32(mod0Offset + 0x18) + mod0Offset;
  193. // TODO: Elf32.
  194. while (true)
  195. {
  196. long tagVal = memory.ReadInt64(dynamicOffset + 0);
  197. long value = memory.ReadInt64(dynamicOffset + 8);
  198. dynamicOffset += 0x10;
  199. ElfDynamicTag tag = (ElfDynamicTag)tagVal;
  200. if (tag == ElfDynamicTag.DT_NULL)
  201. {
  202. break;
  203. }
  204. dynamic[tag] = value;
  205. }
  206. if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out long strTab) ||
  207. !dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out long symTab) ||
  208. !dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out long symEntSize))
  209. {
  210. return;
  211. }
  212. long strTblAddr = textOffset + strTab;
  213. long symTblAddr = textOffset + symTab;
  214. List<ElfSymbol> symbols = new List<ElfSymbol>();
  215. while ((ulong)symTblAddr < (ulong)strTblAddr)
  216. {
  217. ElfSymbol sym = GetSymbol(memory, symTblAddr, strTblAddr);
  218. symbols.Add(sym);
  219. symTblAddr += symEntSize;
  220. }
  221. lock (_images)
  222. {
  223. _images.Add(new Image(textOffset, symbols.OrderBy(x => x.Value).ToArray()));
  224. }
  225. }
  226. private ElfSymbol GetSymbol(MemoryManager memory, long address, long strTblAddr)
  227. {
  228. int nameIndex = memory.ReadInt32(address + 0);
  229. int info = memory.ReadByte (address + 4);
  230. int other = memory.ReadByte (address + 5);
  231. int shIdx = memory.ReadInt16(address + 6);
  232. long value = memory.ReadInt64(address + 8);
  233. long size = memory.ReadInt64(address + 16);
  234. string name = string.Empty;
  235. for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;)
  236. {
  237. name += (char)chr;
  238. }
  239. return new ElfSymbol(name, info, other, shIdx, value, size);
  240. }
  241. }
  242. }