HleProcessDebugger.cs 8.9 KB

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