xbmc
dll_tracker.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "threads/CriticalSection.h"
12 #include "PlatformDefs.h"
13 #ifdef TARGET_WINDOWS
14 #if defined(TARGET_WINDOWS_STORE)
15 #include <WinSock2.h>
16 #endif
17 #endif
18 
19 #include <list>
20 #include <map>
21 
22 class DllLoader;
23 
25 {
26  size_t size;
27  uintptr_t calleraddr;
28 };
29 
30 enum TrackedFileType
31 {
32  FILE_XBMC_OPEN,
33  FILE_XBMC_FOPEN,
34  FILE_OPEN,
35  FILE_FOPEN
36 };
37 
38 typedef struct _TrackedFile
39 {
40  TrackedFileType type;
41  uintptr_t handle;
42  char* name;
43 } TrackedFile;
44 
45 typedef std::map<uintptr_t, AllocLenCaller> DataList;
46 typedef std::map<uintptr_t, AllocLenCaller>::iterator DataListIter;
47 
48 typedef std::list<TrackedFile*> FileList;
49 typedef std::list<TrackedFile*>::iterator FileListIter;
50 
51 typedef std::list<HMODULE> DllList;
52 typedef std::list<HMODULE>::iterator DllListIter;
53 
54 typedef std::list<uintptr_t> DummyList;
55 typedef std::list<uintptr_t>::iterator DummyListIter;
56 
57 typedef std::list<SOCKET> SocketList;
58 typedef std::list<SOCKET>::iterator SocketListIter;
59 
60 typedef std::list<HANDLE> HeapObjectList;
61 typedef std::list<HANDLE>::iterator HeapObjectListIter;
62 
63 typedef std::map<uintptr_t, AllocLenCaller> VAllocList;
64 typedef std::map<uintptr_t, AllocLenCaller>::iterator VAllocListIter;
65 
66 typedef struct _DllTrackInfo
67 {
68  DllLoader* pDll;
69  uintptr_t lMinAddr;
70  uintptr_t lMaxAddr;
71 
72  DataList dataList;
73 
74  // list with dll's that are loaded by this dll
75  DllList dllList;
76 
77  // for dummy functions that are created if no exported function could be found
78  DummyList dummyList;
79 
80  FileList fileList;
81  SocketList socketList;
82 
83  HeapObjectList heapobjectList;
84 
85  VAllocList virtualList;
86 } DllTrackInfo;
87 
88 class TrackedDllList : public std::list<DllTrackInfo*>, public CCriticalSection {};
89 typedef std::list<DllTrackInfo*>::iterator TrackedDllsIter;
90 
91 #ifdef _cplusplus
92 extern "C"
93 {
94 #endif
95 
96 extern CCriticalSection g_trackerLock;
97 extern TrackedDllList g_trackedDlls;
98 
99 // add a dll for tracking
100 void tracker_dll_add(DllLoader* pDll);
101 
102 // remove a dll, and free all its resources
103 void tracker_dll_free(DllLoader* pDll);
104 
105 // sets the dll base address and size
106 void tracker_dll_set_addr(const DllLoader* pDll, uintptr_t min, uintptr_t max);
107 
108 // returns the name from the dll that contains this address or "" if not found
109 const char* tracker_getdllname(uintptr_t caller);
110 
111 // returns a function pointer if there is one available for it, or NULL if not ofund
112 void* tracker_dll_get_function(DllLoader* pDll, char* sFunctionName);
113 
114 DllTrackInfo* tracker_get_dlltrackinfo_byobject(const DllLoader* pDll);
115 
116 DllTrackInfo* tracker_get_dlltrackinfo(uintptr_t caller);
117 
118 void tracker_dll_data_track(const DllLoader* pDll, uintptr_t addr);
119 
120 #ifdef TARGET_POSIX
121 #define _ReturnAddress() __builtin_return_address(0)
122 #endif
123 
124 #ifdef _cplusplus
125 }
126 #endif
127 
128 #ifndef TARGET_POSIX
129 extern "C" void * _ReturnAddress(void);
130 #pragma intrinsic(_ReturnAddress)
131 #endif
132 
Definition: dll_tracker.h:88
Definition: dll_tracker.h:24
Definition: dll_tracker.h:38
Definition: DllLoader.h:61
Definition: dll_tracker.h:66