vfsfitvnm / frida-il2cpp-bridge

A Frida module to dump, trace or hijack any Il2Cpp application at runtime, without needing the global-metadata.dat file.
https://github.com/vfsfitvnm/frida-il2cpp-bridge/wiki
MIT License
974 stars 199 forks source link

How to print an detailed error message when an call to a function failed #274

Closed ChaomengCFX closed 1 year ago

ChaomengCFX commented 1 year ago

I used the following program to get Unity's console output:

    function LogHook(): void {
        const UnityEngine_Application = UnityEngineCoreModule.class('UnityEngine.Application');
        const CallLogCallback = UnityEngine_Application.method('CallLogCallback');
        const UnityEngine_LogType = UnityEngineCoreModule.class('UnityEngine.LogType')
        Interceptor.attach(Il2CppUtil.getFunctionByAddress(Il2Cpp.module, CallLogCallback.relativeVirtualAddress), {
            onEnter: args => {
                let name = Il2CppUtil.getEnumName(args[3].toInt32(), UnityEngine_LogType);
                let color = 'm';
                switch (name) {
                    case 'Error':
                        color = '31m';
                        break;
                    case 'Assert':
                        color = '36m';
                        break;
                    case 'Warning':
                        color = '33m';
                        break;
                    case 'Log':
                        color = '34m';
                        break;
                    case 'Exception':
                        color = '31;43m';
                        break;
                }
                Logger.log('[1;' + color + '<' + name + '> from Unity' + ':\n' + Il2CppUtil.readCSharpString(args[1])?.replace(/<\/*color.*?>/g, '') + '\n    ' + '' + Il2CppUtil.readCSharpString(args[2])?.replace(/\n/g, '\n    ') + '');
            }
        });
    }

It works fine when errors are not generated by frida calling C# functions, like this: IMG_20230329_113827.png

But when a function called by frida and produce fault, does not show the error output, such as call DeserializeObject method of the class Newtonsoft.Json.JsonConvert , if a Json deserialization error occurred, The console hook code above is not called, so you can't see the detailed C# layer error message. It looks like:

Screenshot_20230329_121422_edit_192421722805468.jpg

Is there any way to make the frida call function display log information as well?

vfsfitvnm commented 1 year ago

Yes, you need to setup the exception listener:

Il2Cpp.perform(() => {
    Il2Cpp.installExceptionListener("all");

    // ...
});