xref: /haiku/src/add-ons/kernel/debugger/demangle/demangle.cpp (revision b55a57da7173b9af0432bd3e148d03f06161d036)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "demangle.h"
7 
8 #include <string.h>
9 
10 #include <debug.h>
11 
12 
13 static inline bool
14 looks_like_gcc3_symbol(const char* symbol)
15 {
16 	return strncmp(symbol, "_Z", 2) == 0;
17 }
18 
19 
20 const char*
21 demangle_symbol(const char* mangledName, char* buffer, size_t bufferSize,
22 	bool* _isObjectMethod)
23 {
24 	// try the gcc3 demangler, if it looks like a gcc3 symbol
25 	const char* demangled = NULL;
26 	if (looks_like_gcc3_symbol(mangledName)) {
27 		demangled = demangle_symbol_gcc3(mangledName, buffer, bufferSize,
28 			_isObjectMethod);
29 		if (demangled != NULL)
30 			return demangled;
31 	}
32 
33 	// fallback is gcc2
34 	return demangle_symbol_gcc2(mangledName, buffer, bufferSize,
35 		_isObjectMethod);
36 }
37 
38 
39 status_t
40 get_next_argument(uint32* _cookie, const char* mangledName, char* name,
41 	size_t nameSize, int32* _type, size_t* _argumentLength)
42 {
43 	// try the gcc3 demangler, if it looks like a gcc3 symbol
44 	if (looks_like_gcc3_symbol(mangledName)) {
45 		status_t error = get_next_argument_gcc3(_cookie, mangledName, name,
46 			nameSize, _type, _argumentLength);
47 		if (error == B_OK)
48 			return B_OK;
49 	}
50 
51 	// fallback is gcc2
52 	return get_next_argument_gcc2(_cookie, mangledName, name, nameSize, _type,
53 		_argumentLength);
54 }
55 
56 
57 #ifdef _KERNEL_MODE
58 
59 static status_t
60 std_ops(int32 op, ...)
61 {
62 	switch (op) {
63 		case B_MODULE_INIT:
64 		case B_MODULE_UNINIT:
65 			return B_OK;
66 	}
67 
68 	return B_BAD_VALUE;
69 }
70 
71 
72 static struct debugger_demangle_module_info sModuleInfo = {
73 	{
74 		"debugger/demangle/v1",
75 		0,
76 		std_ops
77 	},
78 
79 	demangle_symbol,
80 	get_next_argument,
81 };
82 
83 module_info* modules[] = {
84 	(module_info*)&sModuleInfo,
85 	NULL
86 };
87 
88 #endif // _KERNEL_MODE
89