1/* 2 * Copyright 2005-2007, Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 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 11 12#include <list> 13#include <map> 14#include <string> 15 16#include <arch_config.h> 17#include <SupportDefs.h> 18 19using std::string; 20 21class Context; 22class Parameter; 23class MemoryReader; 24 25typedef FUNCTION_CALL_PARAMETER_ALIGNMENT_TYPE align_t; 26 27// TypeHandler 28class TypeHandler { 29public: 30 TypeHandler() {} 31 virtual ~TypeHandler() {} 32 33 virtual string GetParameterValue(Context &, Parameter *, 34 const void *value) = 0; 35 virtual string GetReturnValue(Context &, uint64 value) = 0; 36}; 37 38class EnumTypeHandler : public TypeHandler { 39public: 40 typedef std::map<int, const char *> EnumMap; 41 42 EnumTypeHandler(const EnumMap &); 43 44 string GetParameterValue(Context &c, Parameter *, const void *); 45 string GetReturnValue(Context &, uint64 value); 46 47private: 48 string RenderValue(Context &, unsigned int value) const; 49 50 const EnumMap &fMap; 51}; 52 53class FlagsTypeHandler : public TypeHandler { 54public: 55 struct FlagInfo { 56 unsigned int value; 57 const char* name; 58 }; 59 typedef std::list<FlagInfo> FlagsList; 60 61 FlagsTypeHandler(const FlagsList &); 62 63 string GetParameterValue(Context &c, Parameter *, const void *); 64 string GetReturnValue(Context &, uint64 value); 65 66protected: 67 string RenderValue(Context &, unsigned int value) const; 68 69private: 70 const FlagsList &fList; 71}; 72 73// currently limited to select ints 74class TypeHandlerSelector : public TypeHandler { 75public: 76 typedef std::map<int, TypeHandler *> SelectMap; 77 78 TypeHandlerSelector(const SelectMap &, int sibling, 79 TypeHandler *def); 80 81 string GetParameterValue(Context &, Parameter *, const void *); 82 string GetReturnValue(Context &, uint64 value); 83 84private: 85 const SelectMap &fMap; 86 int fSibling; 87 TypeHandler *fDefault; 88}; 89 90// templatized TypeHandler factory class 91// (I tried a simple function first, but then the compiler complains for 92// the partial instantiation. Not sure, if I'm missing something or this is 93// a compiler bug). 94template<typename Type> 95struct TypeHandlerFactory { 96 static TypeHandler *Create(); 97}; 98 99extern TypeHandler *create_pointer_type_handler(); 100extern TypeHandler *create_string_type_handler(); 101extern TypeHandler *create_status_t_type_handler(); 102 103// specialization for "const char*" 104template<> 105struct TypeHandlerFactory<const char*> { 106 static inline TypeHandler *Create() 107 { 108 return create_string_type_handler(); 109 } 110}; 111 112#define DEFINE_FACTORY(name, type) \ 113 template<> \ 114 struct TypeHandlerFactory<type> { \ 115 static inline TypeHandler *Create() \ 116 { \ 117 extern TypeHandler *create_##name##_type_handler(); \ 118 return create_##name##_type_handler(); \ 119 } \ 120 } \ 121 122struct fd_set; 123struct flock; 124struct ifconf; 125struct ifreq; 126struct msghdr; 127struct message_args; 128struct pollfd; 129struct sockaddr; 130struct sockaddr_args; 131struct socket_args; 132struct sockopt_args; 133 134DEFINE_FACTORY(fdset_ptr, fd_set *); 135DEFINE_FACTORY(flock_ptr, flock *); 136DEFINE_FACTORY(ifconf_ptr, ifconf *); 137DEFINE_FACTORY(ifreq_ptr, ifreq *); 138DEFINE_FACTORY(msghdr_ptr, msghdr *); 139DEFINE_FACTORY(msghdr_ptr, const msghdr *); 140DEFINE_FACTORY(message_args_ptr, message_args *); 141DEFINE_FACTORY(pollfd_ptr, pollfd *); 142DEFINE_FACTORY(siginfo_t_ptr, siginfo_t *); 143DEFINE_FACTORY(sockaddr_ptr, sockaddr *); 144DEFINE_FACTORY(sockaddr_ptr, const sockaddr *); 145DEFINE_FACTORY(sockaddr_args_ptr, sockaddr_args *); 146DEFINE_FACTORY(socket_args_ptr, socket_args *); 147DEFINE_FACTORY(sockopt_args_ptr, sockopt_args *); 148 149DEFINE_FACTORY(int_ptr, int *); 150DEFINE_FACTORY(long_ptr, long *); 151DEFINE_FACTORY(longlong_ptr, long long *); 152DEFINE_FACTORY(uint_ptr, unsigned int *); 153DEFINE_FACTORY(ulong_ptr, unsigned long *); 154DEFINE_FACTORY(ulonglong_ptr, unsigned long long *); 155 156template<> 157struct TypeHandlerFactory<void**> { 158 static inline TypeHandler *Create() 159 { 160 return TypeHandlerFactory<addr_t*>::Create(); 161 } 162}; 163 164// partial specialization for generic pointers 165template<typename Type> 166struct TypeHandlerFactory<Type*> { 167 static inline TypeHandler *Create() 168 { 169 return create_pointer_type_handler(); 170 } 171}; 172 173template<typename Type> 174class TypeHandlerImpl : public TypeHandler { 175public: 176 string GetParameterValue(Context &, Parameter *, const void *); 177 string GetReturnValue(Context &, uint64 value); 178}; 179 180#endif // STRACE_TYPE_HANDLER_H 181