2018-11-28 23:18:09 +01:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using Ryujinx.HLE.HOS.Diagnostics.Demangler;
|
|
|
|
using Ryujinx.HLE.Loaders.Elf;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class HleProcessDebugger
|
|
|
|
{
|
|
|
|
private const int Mod0 = 'M' << 0 | 'O' << 8 | 'D' << 16 | '0' << 24;
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private KProcess _owner;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
private class Image
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
public long BaseAddress { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public ElfSymbol[] Symbols { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public Image(long baseAddress, ElfSymbol[] symbols)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
BaseAddress = baseAddress;
|
|
|
|
Symbols = symbols;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private List<Image> _images;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private int _loaded;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public HleProcessDebugger(KProcess owner)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
_owner = owner;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
_images = new List<Image>();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public void PrintGuestStackTrace(CpuThreadState threadState)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
EnsureLoaded();
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
StringBuilder trace = new StringBuilder();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
trace.AppendLine("Guest stack trace:");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
void AppendTrace(long address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
Image image = GetImage(address, out int imageIndex);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (image == null || !TryGetSubName(image, address, out string subName))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
subName = $"Sub{address:x16}";
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-04 21:23:37 +01:00
|
|
|
else if (subName.StartsWith("_Z"))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
subName = Demangler.Parse(subName);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (image != null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
long offset = address - image.BaseAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
string imageName = GetGuessedNsoNameFromIndex(imageIndex);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
string imageNameAndOffset = $"[{_owner.Name}] {imageName}:0x{offset:x8}";
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
trace.AppendLine($" {imageNameAndOffset} {subName}");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
trace.AppendLine($" [{_owner.Name}] ??? {subName}");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
long framePointer = (long)threadState.X29;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
while (framePointer != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
if ((framePointer & 7) != 0 ||
|
|
|
|
!_owner.CpuMemory.IsMapped(framePointer) ||
|
|
|
|
!_owner.CpuMemory.IsMapped(framePointer + 8))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Note: This is the return address, we need to subtract one instruction
|
|
|
|
//worth of bytes to get the branch instruction address.
|
2018-12-04 21:23:37 +01:00
|
|
|
AppendTrace(_owner.CpuMemory.ReadInt64(framePointer + 8) - 4);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
framePointer = _owner.CpuMemory.ReadInt64(framePointer);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
Logger.PrintInfo(LogClass.Cpu, trace.ToString());
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private bool TryGetSubName(Image image, long address, out string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
address -= image.BaseAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
int left = 0;
|
|
|
|
int right = image.Symbols.Length - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
while (left <= right)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
int size = right - left;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
int middle = left + (size >> 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
ElfSymbol symbol = image.Symbols[middle];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
long endAddr = symbol.Value + symbol.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if ((ulong)address >= (ulong)symbol.Value && (ulong)address < (ulong)endAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
name = symbol.Name;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if ((ulong)address < (ulong)symbol.Value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
right = middle - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
left = middle + 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
name = null;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private Image GetImage(long address, out int index)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
lock (_images)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
for (index = _images.Count - 1; index >= 0; index--)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
if ((ulong)address >= (ulong)_images[index].BaseAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
return _images[index];
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private string GetGuessedNsoNameFromIndex(int index)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
if ((uint)index > 11)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (index == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "rtld";
|
|
|
|
}
|
2018-12-04 21:23:37 +01:00
|
|
|
else if (index == 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "main";
|
|
|
|
}
|
2018-12-04 21:23:37 +01:00
|
|
|
else if (index == GetImagesCount() - 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "sdk";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
return "subsdk" + (index - 2);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetImagesCount()
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
lock (_images)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
return _images.Count;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureLoaded()
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
if (Interlocked.CompareExchange(ref _loaded, 1, 0) == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
ScanMemoryForTextSegments();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ScanMemoryForTextSegments()
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
ulong oldAddress = 0;
|
|
|
|
ulong address = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
while (address >= oldAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
KMemoryInfo info = _owner.MemoryManager.QueryMemory(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (info.State == MemoryState.Reserved)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (info.State == MemoryState.CodeStatic && info.Permission == MemoryPermission.ReadAndExecute)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
LoadMod0Symbols(_owner.CpuMemory, (long)info.Address);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
oldAddress = address;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
address = info.Address + info.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private void LoadMod0Symbols(MemoryManager memory, long textOffset)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
long mod0Offset = textOffset + memory.ReadUInt32(textOffset + 4);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
Dictionary<ElfDynamicTag, long> dynamic = new Dictionary<ElfDynamicTag, long>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
int mod0Magic = memory.ReadInt32(mod0Offset + 0x0);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (mod0Magic != Mod0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
long dynamicOffset = memory.ReadInt32(mod0Offset + 0x4) + mod0Offset;
|
|
|
|
long bssStartOffset = memory.ReadInt32(mod0Offset + 0x8) + mod0Offset;
|
|
|
|
long bssEndOffset = memory.ReadInt32(mod0Offset + 0xc) + mod0Offset;
|
|
|
|
long ehHdrStartOffset = memory.ReadInt32(mod0Offset + 0x10) + mod0Offset;
|
|
|
|
long ehHdrEndOffset = memory.ReadInt32(mod0Offset + 0x14) + mod0Offset;
|
|
|
|
long modObjOffset = memory.ReadInt32(mod0Offset + 0x18) + mod0Offset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
long tagVal = memory.ReadInt64(dynamicOffset + 0);
|
|
|
|
long value = memory.ReadInt64(dynamicOffset + 8);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
dynamicOffset += 0x10;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
ElfDynamicTag tag = (ElfDynamicTag)tagVal;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (tag == ElfDynamicTag.DT_NULL)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
dynamic[tag] = value;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out long strTab) ||
|
|
|
|
!dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out long symTab) ||
|
|
|
|
!dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out long symEntSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
long strTblAddr = textOffset + strTab;
|
|
|
|
long symTblAddr = textOffset + symTab;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
List<ElfSymbol> symbols = new List<ElfSymbol>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
while ((ulong)symTblAddr < (ulong)strTblAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
ElfSymbol sym = GetSymbol(memory, symTblAddr, strTblAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
symbols.Add(sym);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
symTblAddr += symEntSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
lock (_images)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
_images.Add(new Image(textOffset, symbols.OrderBy(x => x.Value).ToArray()));
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private ElfSymbol GetSymbol(MemoryManager memory, long address, long strTblAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
int nameIndex = memory.ReadInt32(address + 0);
|
|
|
|
int info = memory.ReadByte (address + 4);
|
|
|
|
int other = memory.ReadByte (address + 5);
|
|
|
|
int shIdx = memory.ReadInt16(address + 6);
|
|
|
|
long value = memory.ReadInt64(address + 8);
|
|
|
|
long size = memory.ReadInt64(address + 16);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
string name = string.Empty;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
name += (char)chr;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
return new ElfSymbol(name, info, other, shIdx, value, size);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|