1 //===-- sanitizer_common.cc -----------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between sanitizers' run-time libraries.
10 //===----------------------------------------------------------------------===//
11 #include "sanitizer_stacktrace_printer.h"
13 namespace __sanitizer
{
15 static const char *StripFunctionName(const char *function
, const char *prefix
) {
16 if (function
== 0) return 0;
17 if (prefix
== 0) return function
;
18 uptr prefix_len
= internal_strlen(prefix
);
19 if (0 == internal_strncmp(function
, prefix
, prefix_len
))
20 return function
+ prefix_len
;
24 static const char kDefaultFormat
[] = " #%n %p %F %L";
26 void RenderFrame(InternalScopedString
*buffer
, const char *format
, int frame_no
,
27 const AddressInfo
&info
, const char *strip_path_prefix
,
28 const char *strip_func_prefix
) {
29 if (0 == internal_strcmp(format
, "DEFAULT"))
30 format
= kDefaultFormat
;
31 for (const char *p
= format
; *p
!= '\0'; p
++) {
33 buffer
->append("%c", *p
);
41 // Frame number and all fields of AddressInfo structure.
43 buffer
->append("%zu", frame_no
);
46 buffer
->append("0x%zx", info
.address
);
49 buffer
->append("%s", StripPathPrefix(info
.module
, strip_path_prefix
));
52 buffer
->append("0x%zx", info
.module_offset
);
55 buffer
->append("%s", StripFunctionName(info
.function
, strip_func_prefix
));
58 buffer
->append("0x%zx", info
.function_offset
!= AddressInfo::kUnknown
59 ? info
.function_offset
63 buffer
->append("%s", StripPathPrefix(info
.file
, strip_path_prefix
));
66 buffer
->append("%d", info
.line
);
69 buffer
->append("%d", info
.column
);
71 // Smarter special cases.
73 // Function name and offset, if file is unknown.
75 buffer
->append("in %s",
76 StripFunctionName(info
.function
, strip_func_prefix
));
77 if (!info
.file
&& info
.function_offset
!= AddressInfo::kUnknown
)
78 buffer
->append("+0x%zx", info
.function_offset
);
82 // File/line information.
83 RenderSourceLocation(buffer
, info
.file
, info
.line
, info
.column
,
87 // Source location, or module location.
89 RenderSourceLocation(buffer
, info
.file
, info
.line
, info
.column
,
91 } else if (info
.module
) {
92 RenderModuleLocation(buffer
, info
.module
, info
.module_offset
,
95 buffer
->append("(<unknown module>)");
99 // Module basename and offset, or PC.
101 buffer
->append("(%s+%p)", StripModuleName(info
.module
),
102 (void *)info
.module_offset
);
104 buffer
->append("(%p)", (void *)info
.address
);
107 Report("Unsupported specifier in stack frame format: %c (0x%zx)!\n",
114 void RenderSourceLocation(InternalScopedString
*buffer
, const char *file
,
115 int line
, int column
, const char *strip_path_prefix
) {
116 buffer
->append("%s", StripPathPrefix(file
, strip_path_prefix
));
118 buffer
->append(":%d", line
);
120 buffer
->append(":%d", column
);
124 void RenderModuleLocation(InternalScopedString
*buffer
, const char *module
,
125 uptr offset
, const char *strip_path_prefix
) {
126 buffer
->append("(%s+0x%zx)", StripPathPrefix(module
, strip_path_prefix
),
130 } // namespace __sanitizer