xref: /haiku/src/add-ons/kernel/debugger/demangle/demangle.cpp (revision a1163de83ea633463a79de234b8742ee106531b2)
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 static 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 static 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 static status_t
58 std_ops(int32 op, ...)
59 {
60 	switch (op) {
61 		case B_MODULE_INIT:
62 		case B_MODULE_UNINIT:
63 			return B_OK;
64 	}
65 
66 	return B_BAD_VALUE;
67 }
68 
69 
70 static struct debugger_demangle_module_info sModuleInfo = {
71 	{
72 		"debugger/demangle/v1",
73 		0,
74 		std_ops
75 	},
76 
77 	demangle_symbol,
78 	get_next_argument,
79 };
80 
81 module_info* modules[] = {
82 	(module_info*)&sModuleInfo,
83 	NULL
84 };
85