2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
private KProcess Owner;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
private class Image
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
public long BaseAddress { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public ElfSymbol[] Symbols { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public Image(long BaseAddress, ElfSymbol[] Symbols)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
this.BaseAddress = BaseAddress;
|
|
|
|
this.Symbols = Symbols;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private List<Image> Images;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private int Loaded;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public HleProcessDebugger(KProcess Owner)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
this.Owner = Owner;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Images = new List<Image>();
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public void PrintGuestStackTrace(CpuThreadState ThreadState)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
EnsureLoaded();
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
StringBuilder Trace = new StringBuilder();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Trace.AppendLine("Guest stack trace:");
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
void AppendTrace(long Address)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Image Image = GetImage(Address, out int ImageIndex);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Image == null || !TryGetSubName(Image, Address, out string SubName))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
SubName = $"Sub{Address:x16}";
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (SubName.StartsWith("_Z"))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
SubName = Demangler.Parse(SubName);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Image != null)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
long Offset = Address - Image.BaseAddress;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
string ImageName = GetGuessedNsoNameFromIndex(ImageIndex);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
string ImageNameAndOffset = $"[{Owner.Name}] {ImageName}:0x{Offset:x8}";
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Trace.AppendLine($" {ImageNameAndOffset} {SubName}");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Trace.AppendLine($" [{Owner.Name}] ??? {SubName}");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
long FramePointer = (long)ThreadState.X29;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (FramePointer != 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((FramePointer & 7) != 0 ||
|
|
|
|
!Owner.CpuMemory.IsMapped(FramePointer) ||
|
|
|
|
!Owner.CpuMemory.IsMapped(FramePointer + 8))
|
2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
AppendTrace(Owner.CpuMemory.ReadInt64(FramePointer + 8) - 4);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
FramePointer = Owner.CpuMemory.ReadInt64(FramePointer);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Logger.PrintInfo(LogClass.Cpu, Trace.ToString());
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private bool TryGetSubName(Image Image, long Address, out string Name)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Address -= Image.BaseAddress;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
int Left = 0;
|
|
|
|
int Right = Image.Symbols.Length - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Left <= Right)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
int Size = Right - Left;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
int Middle = Left + (Size >> 1);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ElfSymbol Symbol = Image.Symbols[Middle];
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
long EndAddr = Symbol.Value + Symbol.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((ulong)Address >= (ulong)Symbol.Value && (ulong)Address < (ulong)EndAddr)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Name = Symbol.Name;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((ulong)Address < (ulong)Symbol.Value)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Right = Middle - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Left = Middle + 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Name = null;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private Image GetImage(long Address, out int Index)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Images)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
for (Index = Images.Count - 1; Index >= 0; Index--)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((ulong)Address >= (ulong)Images[Index].BaseAddress)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Images[Index];
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private string GetGuessedNsoNameFromIndex(int Index)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((uint)Index > 11)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Index == 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return "rtld";
|
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (Index == 1)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return "main";
|
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (Index == GetImagesCount() - 1)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return "sdk";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return "subsdk" + (Index - 2);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetImagesCount()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Images)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Images.Count;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureLoaded()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Interlocked.CompareExchange(ref Loaded, 1, 0) == 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
ScanMemoryForTextSegments();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ScanMemoryForTextSegments()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong OldAddress = 0;
|
|
|
|
ulong Address = 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Address >= OldAddress)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info = Owner.MemoryManager.QueryMemory(Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State == MemoryState.Reserved)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State == MemoryState.CodeStatic && Info.Permission == MemoryPermission.ReadAndExecute)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
LoadMod0Symbols(Owner.CpuMemory, (long)Info.Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
OldAddress = Address;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = Info.Address + Info.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private void LoadMod0Symbols(MemoryManager Memory, long TextOffset)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
long Mod0Offset = TextOffset + Memory.ReadUInt32(TextOffset + 4);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Mod0Offset < TextOffset || !Memory.IsMapped(Mod0Offset) || (Mod0Offset & 3) != 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Dictionary<ElfDynamicTag, long> Dynamic = new Dictionary<ElfDynamicTag, long>();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
int Mod0Magic = Memory.ReadInt32(Mod0Offset + 0x0);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Mod0Magic != Mod0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
long TagVal = Memory.ReadInt64(DynamicOffset + 0);
|
|
|
|
long Value = Memory.ReadInt64(DynamicOffset + 8);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
DynamicOffset += 0x10;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ElfDynamicTag Tag = (ElfDynamicTag)TagVal;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Tag == ElfDynamicTag.DT_NULL)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Dynamic[Tag] = Value;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
long StrTblAddr = TextOffset + StrTab;
|
|
|
|
long SymTblAddr = TextOffset + SymTab;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
List<ElfSymbol> Symbols = new List<ElfSymbol>();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while ((ulong)SymTblAddr < (ulong)StrTblAddr)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ElfSymbol Sym = GetSymbol(Memory, SymTblAddr, StrTblAddr);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Symbols.Add(Sym);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
SymTblAddr += SymEntSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Images)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Images.Add(new Image(TextOffset, Symbols.OrderBy(x => x.Value).ToArray()));
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private ElfSymbol GetSymbol(MemoryManager Memory, long Address, long StrTblAddr)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
string Name = string.Empty;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
for (int Chr; (Chr = Memory.ReadByte(StrTblAddr + NameIndex++)) != 0;)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Name += (char)Chr;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return new ElfSymbol(Name, Info, Other, SHIdx, Value, Size);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|