vfs: check userland buffers before reading them.
[haiku.git] / src / bin / debug / strace / TypeHandler.h
blob86e80e59484fd5047f18cef9887912e81b8e1dd9
1 /*
2 * Copyright 2005-2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <bonefish@cs.tu-berlin.de>
7 * Hugo Santos <hugosantos@gmail.com>
8 */
9 #ifndef STRACE_TYPE_HANDLER_H
10 #define STRACE_TYPE_HANDLER_H
12 #include <string>
13 #include <map>
15 #include <arch_config.h>
16 #include <SupportDefs.h>
18 using std::string;
20 class Context;
21 class Parameter;
22 class MemoryReader;
24 typedef FUNCTION_CALL_PARAMETER_ALIGNMENT_TYPE align_t;
26 // TypeHandler
27 class TypeHandler {
28 public:
29 TypeHandler() {}
30 virtual ~TypeHandler() {}
32 virtual string GetParameterValue(Context &, Parameter *,
33 const void *value) = 0;
34 virtual string GetReturnValue(Context &, uint64 value) = 0;
37 class EnumTypeHandler : public TypeHandler {
38 public:
39 typedef std::map<int, const char *> EnumMap;
41 EnumTypeHandler(const EnumMap &);
43 string GetParameterValue(Context &c, Parameter *, const void *);
44 string GetReturnValue(Context &, uint64 value);
46 private:
47 string RenderValue(Context &, unsigned int value) const;
49 const EnumMap &fMap;
52 // currently limited to select ints
53 class TypeHandlerSelector : public TypeHandler {
54 public:
55 typedef std::map<int, TypeHandler *> SelectMap;
57 TypeHandlerSelector(const SelectMap &, int sibling,
58 TypeHandler *def);
60 string GetParameterValue(Context &, Parameter *, const void *);
61 string GetReturnValue(Context &, uint64 value);
63 private:
64 const SelectMap &fMap;
65 int fSibling;
66 TypeHandler *fDefault;
69 // templatized TypeHandler factory class
70 // (I tried a simple function first, but then the compiler complains for
71 // the partial instantiation. Not sure, if I'm missing something or this is
72 // a compiler bug).
73 template<typename Type>
74 struct TypeHandlerFactory {
75 static TypeHandler *Create();
78 extern TypeHandler *create_pointer_type_handler();
79 extern TypeHandler *create_string_type_handler();
81 // specialization for "const char*"
82 template<>
83 struct TypeHandlerFactory<const char*> {
84 static inline TypeHandler *Create()
86 return create_string_type_handler();
90 #define DEFINE_FACTORY(name, type) \
91 template<> \
92 struct TypeHandlerFactory<type> { \
93 static inline TypeHandler *Create() \
94 { \
95 extern TypeHandler *create_##name##_type_handler(); \
96 return create_##name##_type_handler(); \
97 } \
98 } \
100 struct fd_set;
101 struct ifconf;
102 struct ifreq;
103 struct message_args;
104 struct sockaddr_args;
105 struct socket_args;
106 struct sockopt_args;
108 DEFINE_FACTORY(fdset_ptr, fd_set *);
109 DEFINE_FACTORY(ifconf_ptr, ifconf *);
110 DEFINE_FACTORY(ifreq_ptr, ifreq *);
111 DEFINE_FACTORY(message_args_ptr, message_args *);
112 DEFINE_FACTORY(sockaddr_args_ptr, sockaddr_args *);
113 DEFINE_FACTORY(socket_args_ptr, socket_args *);
114 DEFINE_FACTORY(sockopt_args_ptr, sockopt_args *);
116 DEFINE_FACTORY(int_ptr, int *);
117 DEFINE_FACTORY(long_ptr, long *);
118 DEFINE_FACTORY(longlong_ptr, long long *);
119 DEFINE_FACTORY(uint_ptr, unsigned int *);
120 DEFINE_FACTORY(ulong_ptr, unsigned long *);
121 DEFINE_FACTORY(ulonglong_ptr, unsigned long long *);
123 // partial specialization for generic pointers
124 template<typename Type>
125 struct TypeHandlerFactory<Type*> {
126 static inline TypeHandler *Create()
128 return create_pointer_type_handler();
132 template<typename Type>
133 class TypeHandlerImpl : public TypeHandler {
134 public:
135 string GetParameterValue(Context &, Parameter *, const void *);
136 string GetReturnValue(Context &, uint64 value);
139 #endif // STRACE_TYPE_HANDLER_H