[dali_1.0.23] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-common.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/common/dali-common.h>
20
21 // EXTERNAL INCLUDES
22 #include <stdlib.h>
23 #include <string>
24 #include <cstdio>
25
26 #ifndef EMSCRIPTEN // cxxabi not supported
27 # include <execinfo.h>
28 # include <cxxabi.h>
29 #endif
30
31 #include <cstring>
32
33 // INTERNAL INCLUDES
34 #include <dali/integration-api/debug.h>
35
36 namespace
37 {
38 const int MAX_NUM_STACK_FRAMES = 25;
39 const size_t C_SYMBOL_LENGTH = 4096;
40 }
41
42 namespace Dali
43 {
44
45 #if defined(BACKTRACE_ENABLED)
46
47 std::string Demangle(const char* symbol)
48 {
49   std::string result;
50
51   // backtrace ::=  <filename>'('['_'<mangled-symbol>'_']['+'<offset>]')'
52   // Only want <mangled-symbol>:
53   const char* openParen = strchr(symbol, '(');
54   if(openParen != NULL)
55   {
56     const char* startOfToken = openParen + 1;
57     const char* plus = strchr(startOfToken, '+');
58     const char* closeParen = strchr(startOfToken, ')');
59     const char* endOfToken = NULL;
60     if(plus != NULL)
61     {
62       endOfToken = plus;
63     }
64     else if(closeParen != NULL)
65     {
66       endOfToken = closeParen;
67     }
68     if(endOfToken != NULL)
69     {
70       size_t tokenLength = endOfToken - startOfToken;
71
72       // Allocate space for symbol
73       char *mangledSymbol = (char*)malloc(tokenLength+1u);
74       if(mangledSymbol != NULL)
75       {
76         strncpy(mangledSymbol, startOfToken, tokenLength);
77         mangledSymbol[tokenLength] = '\0';
78
79         size_t size;
80         int    status;
81         char*  demangled=NULL;
82         demangled = abi::__cxa_demangle( mangledSymbol, NULL, &size, &status );
83         if( demangled != NULL )
84         {
85           result = demangled;
86           free(demangled); // demangle() allocates returned string, so free it
87         }
88         else
89         {
90           result = symbol;
91         }
92         free(mangledSymbol);
93       }
94     }
95   }
96
97   return result;
98 }
99
100 DALI_EXPORT_API DaliException::DaliException( const char* location, const char* condition )
101 : location( location ), condition( condition )
102 {
103   // Note, if a memory error has occured, then the backtrace won't work - backtrace_symbols relies on
104   // allocating memory.
105
106   // Initial dlog error message is output in DALI_ASSERT_ALWAYS macro
107   // Also output on stderr
108 #if defined(DEBUG_ENABLED)
109   fprintf(stderr, "Exception: \n%s\n thrown at %s\nSee dlog for backtrace\n", condition, location);
110 #else
111   fprintf(stderr, "Exception: \n%s\n thrown\nSee dlog for backtrace\n", condition );
112 #endif
113
114   DALI_LOG_ERROR_NOFN("Backtrace:\n");
115
116   void* frameArray[MAX_NUM_STACK_FRAMES];
117   int nSize = backtrace(frameArray, MAX_NUM_STACK_FRAMES);
118   char** symbols = backtrace_symbols(frameArray, nSize);
119   for(int i=1; i< nSize; i++)
120   {
121     std::string demangled_symbol = Demangle(symbols[i]);
122     DALI_LOG_ERROR_NOFN("[%02d]   %s\n", i, demangled_symbol.c_str() );
123   }
124   free(symbols);
125 }
126
127
128 #else // BACKTRACE_ENABLED
129
130 DALI_EXPORT_API DaliException::DaliException( const char* location, const char* condition )
131 : location( location ), condition( condition )
132 {
133 #if defined(DEBUG_ENABLED)
134   printf("Exception: \n%s\n thrown at %s\n", condition, location );
135 #else
136   printf("Exception: \n%s\n thrown\n", condition );
137 #endif
138 }
139
140
141 #endif // BACKTRACE_ENABLED
142
143 DALI_EXPORT_API void DaliAssertMessage( const char* location, const char* condition )
144 {
145 #if defined(DEBUG_ENABLED)
146   DALI_LOG_ERROR_NOFN( "Assert (%s) failed in: %s\n", condition, location );
147 #else
148   DALI_LOG_ERROR_NOFN( "Assert (%s) failed\n", condition );
149 #endif
150 }
151
152 } // Dali