2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/public-api/common/dali-common.h>
32 #include <dali/integration-api/debug.h>
37 #if defined(BACKTRACE_ENABLED)
41 const int32_t MAX_NUM_STACK_FRAMES = 25;
44 std::string Demangle(const char* symbol)
48 // backtrace ::= <filename>'('['_'<mangled-symbol>'_']['+'<offset>]')'
49 // Only want <mangled-symbol>:
50 const char* openParen = strchr(symbol, '(');
53 const char* startOfToken = openParen + 1;
54 const char* plus = strchr(startOfToken, '+');
55 const char* closeParen = strchr(startOfToken, ')');
56 const char* endOfToken = NULL;
61 else if(closeParen != NULL)
63 endOfToken = closeParen;
65 if(endOfToken != NULL)
67 size_t tokenLength = endOfToken - startOfToken;
69 // Allocate space for symbol
70 char *mangledSymbol = reinterpret_cast< char* >( malloc( tokenLength + 1u ) );
71 if(mangledSymbol != NULL)
73 strncpy(mangledSymbol, startOfToken, tokenLength);
74 mangledSymbol[tokenLength] = '\0';
79 demangled = abi::__cxa_demangle( mangledSymbol, NULL, &size, &status );
80 if( demangled != NULL )
83 free(demangled); // demangle() allocates returned string, so free it
97 DALI_CORE_API DaliException::DaliException( const char* location, const char* condition )
98 : location( location ), condition( condition )
100 // Note, if a memory error has occured, then the backtrace won't work - backtrace_symbols relies on
101 // allocating memory.
103 // Initial dlog error message is output in DALI_ASSERT_ALWAYS macro
104 // Also output on stderr
105 #if defined(DEBUG_ENABLED)
106 fprintf(stderr, "Exception: \n%s\n thrown at %s\nSee dlog for backtrace\n", condition, location);
108 fprintf(stderr, "Exception: \n%s\n thrown\nSee dlog for backtrace\n", condition );
111 DALI_LOG_ERROR_NOFN("Backtrace:\n");
113 void* frameArray[MAX_NUM_STACK_FRAMES];
114 int32_t nSize = backtrace(frameArray, MAX_NUM_STACK_FRAMES);
115 char** symbols = backtrace_symbols(frameArray, nSize);
116 for(int32_t i=1; i< nSize; i++)
118 std::string demangled_symbol = Demangle(symbols[i]);
119 DALI_LOG_ERROR_NOFN("[%02d] %s\n", i, demangled_symbol.c_str() );
125 #else // BACKTRACE_ENABLED
127 DALI_CORE_API DaliException::DaliException( const char* location, const char* condition )
128 : location( location ), condition( condition )
130 #if defined(DEBUG_ENABLED)
131 printf("Exception: \n%s\n thrown at %s\n", condition, location );
133 printf("Exception: \n%s\n thrown\n", condition );
138 #endif // BACKTRACE_ENABLED
140 DALI_CORE_API void DaliAssertMessage( const char* location, const char* condition )
142 #if defined(DEBUG_ENABLED)
143 DALI_LOG_ERROR_NOFN( "Assert (%s) failed in: %s\n", condition, location );
145 DALI_LOG_ERROR_NOFN( "Assert (%s) failed\n", condition );