1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_ResultFailureHandler.cpp
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 29262 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/Result.h>
17 #include <nn/dbg.h>
18 #include <nn/svc.h>
19 
20 using namespace nn::dbg;
21 
22 namespace {
nnosGetProcessId(bit32 * pOut)23     void nnosGetProcessId(bit32* pOut)
24     {
25 #if     defined(NN_PROCESSOR_ARM11MPCORE)
26         nn::svc::GetProcessId(pOut, nn::PSEUDO_HANDLE_CURRENT_PROCESS);
27 #elif   defined(NN_PROCESSOR_ARM946ES)
28         *pOut = 0;
29 #endif
30     }
31 
32 #ifndef NN_SWITCH_DISABLE_DEBUG_PRINT
nnosResultFailureHandlerImplDefault(nnResult result,const char * filename,int lineno,const char * fmt,va_list vlist)33     void nnosResultFailureHandlerImplDefault(nnResult result, const char* filename, int lineno, const char* fmt, va_list vlist)
34     {
35         bit32 processId;
36         nnosGetProcessId(&processId);
37         nn::dbg::detail::Printf("RESULT FAILURE [%x]: ", nn::Result(result).GetPrintableBits());
38         nn::dbg::detail::VPrintf(fmt, vlist);
39         nn::dbg::detail::Printf(" (process_id:%d) (%s:%d)\n", processId, filename, lineno);
40         PrintResult(result);
41 
42         Break(BREAK_REASON_ASSERT);
43     }
nnosResultTFailureHandlerImplDefault(nnResult result,const char * filename,int lineno,const char * fmt,va_list vlist)44     void nnosResultTFailureHandlerImplDefault(nnResult result, const char* filename, int lineno, const char* fmt, va_list vlist)
45     {
46         bit32 processId;
47         nnosGetProcessId(&processId);
48         nn::dbg::detail::TPrintf("RESULT FAILURE [%x]: ", nn::Result(result).GetPrintableBits());
49         nn::dbg::detail::TVPrintf(fmt, vlist);
50         nn::dbg::detail::TPrintf(" (process_id:%d) (%s:%d)\n", processId, filename, lineno);
51         TPrintResult(result);
52 
53         Break(BREAK_REASON_ASSERT);
54     }
55 #else
nnosResultFailureHandlerImplDefault(nnResult,const char *,int,const char *,va_list)56     void nnosResultFailureHandlerImplDefault(nnResult, const char*, int, const char*, va_list)
57     {
58         Break(BREAK_REASON_ASSERT);
59     }
nnosResultTFailureHandlerImplDefault(nnResult,const char *,int,const char *,va_list)60     void nnosResultTFailureHandlerImplDefault(nnResult, const char*, int, const char*, va_list)
61     {
62         Break(BREAK_REASON_ASSERT);
63     }
64 #endif
65 
nnosResultPanicHandlerImplDefault(nnResult result,const char * filename,int lineno,const char * fmt,va_list vlist)66     void nnosResultPanicHandlerImplDefault(nnResult result, const char* filename, int lineno, const char* fmt, va_list vlist)
67     {
68 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT
69         NN_UNUSED_VAR(filename);
70         NN_UNUSED_VAR(lineno);
71 #endif //NN_SWITCH_DISABLE_DEBUG_PRINT
72 
73         bit32 processId;
74         nnosGetProcessId(&processId);
75         NN_LOG("RESULT [%x]:", nn::Result(result).GetPrintableBits());
76         //nn::dbg::detail::VPrintf(fmt, vlist);
77         NN_UNUSED_VAR(fmt);
78         NN_UNUSED_VAR(vlist);
79         NN_LOG(" (process_id:%d) (%s:%d)\n", processId, filename, lineno);
80         PrintResult(result);
81 
82         //Break(BREAK_REASON_PANIC);
83     }
nnosResultTPanicHandlerImplDefault(nnResult result,const char * filename,int lineno,const char * fmt,va_list vlist)84     void nnosResultTPanicHandlerImplDefault(nnResult result, const char* filename, int lineno, const char* fmt, va_list vlist)
85     {
86 #ifdef NN_SWITCH_DISABLE_DEBUG_PRINT_FOR_SDK
87         NN_UNUSED_VAR(filename);
88         NN_UNUSED_VAR(lineno);
89 #endif //NN_SWITCH_DISABLE_DEBUG_PRINT_FOR_SDK
90 
91         bit32 processId;
92         nnosGetProcessId(&processId);
93         NN_TLOG_("RESULT [%x]:", nn::Result(result).GetPrintableBits());
94         //nn::dbg::detail::TVPrintf(fmt, vlist);
95         NN_UNUSED_VAR(fmt);
96         NN_UNUSED_VAR(vlist);
97         NN_TLOG_(" (process_id:%d) (%s:%d)\n", processId, filename, lineno);
98         TPrintResult(result);
99 
100         //Break(BREAK_REASON_PANIC);
101     }
102 }
103 
104 extern "C" {
105 
106 // アプリは弱シンボルを上書き、ライブラリは関数ポインタを上書き
107 nnResultHandlerImpl nnResultFailureHandlerImpl   = nnosResultFailureHandlerImplDefault;
108 nnResultHandlerImpl nnResultTFailureHandlerImpl  = nnosResultTFailureHandlerImplDefault;
109 nnResultHandlerImpl nnResultPanicHandlerImpl     = nnosResultPanicHandlerImplDefault;
110 nnResultHandlerImpl nnResultTPanicHandlerImpl    = nnosResultTPanicHandlerImplDefault;
111 
112 // nnResultFailureHandler のデフォルト実装
nnResultFailureHandler(nnResult result,const char * filename,int lineno,const char * fmt,...)113 NN_WEAK_SYMBOL int nnResultFailureHandler(nnResult result, const char* filename, int lineno, const char* fmt, ...)
114 {
115     va_list vlist;
116     va_start(vlist, fmt);
117     nnResultFailureHandlerImpl(result, filename, lineno, fmt, vlist);
118     va_end(vlist);
119     return 0;
120 }
121 // nnResultTFailureHandler のデフォルト実装
nnResultTFailureHandler(nnResult result,const char * filename,int lineno,const char * fmt,...)122 NN_WEAK_SYMBOL int nnResultTFailureHandler(nnResult result, const char* filename, int lineno, const char* fmt, ...)
123 {
124     va_list vlist;
125     va_start(vlist, fmt);
126     nnResultTFailureHandlerImpl(result, filename, lineno, fmt, vlist);
127     va_end(vlist);
128     return 0;
129 }
130 
131 // nnResultPanicHandler のデフォルト実装
nnResultPanicHandler(nnResult result,const char * filename,int lineno,const char * fmt,...)132 NN_WEAK_SYMBOL int nnResultPanicHandler(nnResult result, const char* filename, int lineno, const char* fmt, ...)
133 {
134     va_list vlist;
135     va_start(vlist, fmt);
136     nnResultPanicHandlerImpl(result, filename, lineno, fmt, vlist);
137     va_end(vlist);
138     return 0;
139 }
140 // nnResultTPanicHandler のデフォルト実装
nnResultTPanicHandler(nnResult result,const char * filename,int lineno,const char * fmt,...)141 NN_WEAK_SYMBOL int nnResultTPanicHandler(nnResult result, const char* filename, int lineno, const char* fmt, ...)
142 {
143     va_list vlist;
144     va_start(vlist, fmt);
145     nnResultTPanicHandlerImpl(result, filename, lineno, fmt, vlist);
146     va_end(vlist);
147     return 0;
148 }
149 
nnMakeInvalidResult()150 nnResult nnMakeInvalidResult()
151 {
152     nn::Result result = nn::MakePermanentResult(
153                             nn::Result::SUMMARY_INVALID_RESULT_VALUE,
154                             nn::Result::MODULE_INVALID_RESULT_VALUE,
155                             nn::Result::DESCRIPTION_INVALID_RESULT_VALUE);
156     return static_cast<nnResult>(result);
157 }
158 
159 }
160