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;
|
2023-06-13 02:12:06 +02:00
|
|
|
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-06-13 02:12:06 +02:00
|
|
|
SpanReader reader = new(message);
|
|
|
|
|
|
|
|
if (!reader.TryRead(out LogPacketHeader header))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-12 07:42:05 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-06-13 02:12:06 +02:00
|
|
|
if (!TryReadUleb128(ref reader, out int type) || !TryReadUleb128(ref reader, out int size))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-04 23:15:45 +01:00
|
|
|
|
2023-01-12 07:42:05 +01:00
|
|
|
LogDataChunkKey key = (LogDataChunkKey)type;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
2023-07-01 12:42:10 +02:00
|
|
|
switch (key)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-07-01 12:42:10 +02:00
|
|
|
case LogDataChunkKey.Start:
|
|
|
|
reader.Skip(size);
|
|
|
|
continue;
|
|
|
|
case LogDataChunkKey.Stop:
|
|
|
|
break;
|
|
|
|
case LogDataChunkKey.Line when !reader.TryRead(out _logPacket.Line):
|
|
|
|
case LogDataChunkKey.DropCount when !reader.TryRead(out _logPacket.DropCount):
|
|
|
|
case LogDataChunkKey.Time when !reader.TryRead(out _logPacket.Time):
|
2023-06-13 02:12:06 +02:00
|
|
|
return true;
|
2023-07-01 12:42:10 +02:00
|
|
|
case LogDataChunkKey.Message:
|
2023-01-12 07:42:05 +01:00
|
|
|
{
|
2023-07-01 12:42:10 +02:00
|
|
|
string text = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd();
|
|
|
|
|
|
|
|
if (isHeadPacket && isTailPacket)
|
|
|
|
{
|
|
|
|
_logPacket.Message = text;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logPacket.Message += text;
|
|
|
|
|
|
|
|
if (_logPacket.Message.Length >= MessageLengthLimit)
|
|
|
|
{
|
|
|
|
isTailPacket = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2023-01-12 07:42:05 +01:00
|
|
|
}
|
2023-07-01 12:42:10 +02:00
|
|
|
case LogDataChunkKey.Filename:
|
|
|
|
_logPacket.Filename = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd();
|
|
|
|
break;
|
|
|
|
case LogDataChunkKey.Function:
|
|
|
|
_logPacket.Function = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd();
|
|
|
|
break;
|
|
|
|
case LogDataChunkKey.Module:
|
|
|
|
_logPacket.Module = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd();
|
|
|
|
break;
|
|
|
|
case LogDataChunkKey.Thread:
|
|
|
|
_logPacket.Thread = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd();
|
|
|
|
break;
|
|
|
|
case LogDataChunkKey.ProgramName:
|
|
|
|
_logPacket.ProgramName = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd();
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2023-06-13 02:12:06 +02:00
|
|
|
private static bool TryReadUleb128(ref SpanReader reader, out int result)
|
2023-01-04 23:15:45 +01:00
|
|
|
{
|
2023-06-13 02:12:06 +02:00
|
|
|
result = 0;
|
|
|
|
int count = 0;
|
2023-01-04 23:15:45 +01:00
|
|
|
byte encoded;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-07-01 12:42:10 +02:00
|
|
|
if (!reader.TryRead(out encoded))
|
2023-06-13 02:12:06 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
result += (encoded & 0x7F) << (7 * count);
|
|
|
|
|
|
|
|
count++;
|
|
|
|
} while ((encoded & 0x80) != 0);
|
|
|
|
|
2023-06-13 02:12:06 +02:00
|
|
|
return true;
|
2023-01-04 23:15:45 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-01 12:42:10 +02:00
|
|
|
}
|