2023-01-04 23:15:45 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using Ryujinx.Common.Memory;
|
|
|
|
using Ryujinx.Horizon.Common;
|
2023-01-12 07:42:05 +01:00
|
|
|
using Ryujinx.Horizon.LogManager.Types;
|
2023-01-04 23:15:45 +01:00
|
|
|
using Ryujinx.Horizon.Sdk.Lm;
|
|
|
|
using Ryujinx.Horizon.Sdk.Sf;
|
|
|
|
using Ryujinx.Horizon.Sdk.Sf.Hipc;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Text;
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
namespace Ryujinx.Horizon.LogManager.Ipc
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-08 13:13:39 +01:00
|
|
|
partial class LmLogger : ILmLogger
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
private const int MessageLengthLimit = 5000;
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
private readonly LogService _log;
|
|
|
|
private readonly ulong _pid;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
private LogPacket _logPacket;
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
public LmLogger(LogService log, ulong pid)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
|
|
|
_log = log;
|
2023-01-08 13:13:39 +01:00
|
|
|
_pid = pid;
|
2023-01-12 07:42:05 +01:00
|
|
|
|
|
|
|
_logPacket = new LogPacket();
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[CmifCommand(0)]
|
|
|
|
public Result Log([Buffer(HipcBufferFlags.In | HipcBufferFlags.AutoSelect)] Span<byte> message)
|
|
|
|
{
|
2023-01-08 13:13:39 +01:00
|
|
|
if (!SetProcessId(message, _pid))
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
|
|
|
return Result.Success;
|
|
|
|
}
|
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
if (LogImpl(message))
|
|
|
|
{
|
|
|
|
Logger.Guest?.Print(LogClass.ServiceLm, _logPacket.ToString());
|
|
|
|
|
|
|
|
_logPacket = new LogPacket();
|
|
|
|
}
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
}
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
[CmifCommand(1)] // 3.0.0+
|
2023-01-04 23:15:45 +01:00
|
|
|
public Result SetDestination(LogDestination destination)
|
|
|
|
{
|
|
|
|
_log.LogDestination = destination;
|
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool SetProcessId(Span<byte> message, ulong processId)
|
|
|
|
{
|
|
|
|
ref LogPacketHeader header = ref MemoryMarshal.Cast<byte, LogPacketHeader>(message)[0];
|
|
|
|
|
|
|
|
uint expectedMessageSize = (uint)Unsafe.SizeOf<LogPacketHeader>() + header.PayloadSize;
|
|
|
|
if (expectedMessageSize != (uint)message.Length)
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceLm, $"Invalid message size (expected 0x{expectedMessageSize:X} but got 0x{message.Length:X}).");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
header.ProcessId = processId;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
private bool LogImpl(ReadOnlySpan<byte> message)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
SpanReader reader = new(message);
|
|
|
|
LogPacketHeader header = reader.Read<LogPacketHeader>();
|
|
|
|
|
|
|
|
bool isHeadPacket = (header.Flags & LogPacketFlags.IsHead) != 0;
|
|
|
|
bool isTailPacket = (header.Flags & LogPacketFlags.IsTail) != 0;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
_logPacket.Severity = header.Severity;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
while (reader.Length > 0)
|
|
|
|
{
|
|
|
|
int type = ReadUleb128(ref reader);
|
|
|
|
int size = ReadUleb128(ref reader);
|
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
LogDataChunkKey key = (LogDataChunkKey)type;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
if (key == LogDataChunkKey.Start)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
|
|
|
reader.Skip(size);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
else if (key == LogDataChunkKey.Stop)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
else if (key == LogDataChunkKey.Line)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
_logPacket.Line = reader.Read<int>();
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
else if (key == LogDataChunkKey.DropCount)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
_logPacket.DropCount = reader.Read<long>();
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
else if (key == LogDataChunkKey.Time)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
_logPacket.Time = reader.Read<long>();
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
else if (key == LogDataChunkKey.Message)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
string text = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd();
|
|
|
|
|
|
|
|
if (isHeadPacket && isTailPacket)
|
|
|
|
{
|
|
|
|
_logPacket.Message = text;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logPacket.Message += text;
|
|
|
|
|
|
|
|
if (_logPacket.Message.Length >= MessageLengthLimit)
|
|
|
|
{
|
|
|
|
isTailPacket = true;
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
else if (key == LogDataChunkKey.Filename)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-01-12 07:42:05 +01:00
|
|
|
_logPacket.Filename = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd();
|
|
|
|
}
|
|
|
|
else if (key == LogDataChunkKey.Function)
|
|
|
|
{
|
|
|
|
_logPacket.Function = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd();
|
|
|
|
}
|
|
|
|
else if (key == LogDataChunkKey.Module)
|
|
|
|
{
|
|
|
|
_logPacket.Module = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd();
|
|
|
|
}
|
|
|
|
else if (key == LogDataChunkKey.Thread)
|
|
|
|
{
|
|
|
|
_logPacket.Thread = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd();
|
|
|
|
}
|
|
|
|
else if (key == LogDataChunkKey.ProgramName)
|
|
|
|
{
|
|
|
|
_logPacket.ProgramName = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd();
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
return isTailPacket;
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int ReadUleb128(ref SpanReader reader)
|
|
|
|
{
|
|
|
|
int result = 0;
|
2023-01-08 13:13:39 +01:00
|
|
|
int count = 0;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
byte encoded;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
encoded = reader.Read<byte>();
|
|
|
|
|
|
|
|
result += (encoded & 0x7F) << (7 * count);
|
|
|
|
|
|
|
|
count++;
|
|
|
|
} while ((encoded & 0x80) != 0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 13:13:39 +01:00
|
|
|
}
|