1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef __leaky_h_
6 #define __leaky_h_
7 
8 #include "config.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include "libmalloc.h"
13 #include "strset.h"
14 #include "intcnt.h"
15 
16 typedef unsigned int u_int;
17 
18 struct Symbol;
19 struct leaky;
20 
21 class FunctionCount : public IntCount {
22  public:
23   void printReport(FILE* fp, leaky* lk, int parent, int total);
24 };
25 
26 struct Symbol {
27   char* name;
28   u_long address;
29   int timerHit;
30   FunctionCount cntP, cntC;
31 
regChildSymbol32   int regChild(int id) { return cntC.countAdd(id, 1); }
regParrentSymbol33   int regParrent(int id) { return cntP.countAdd(id, 1); }
regClearSymbol34   void regClear() {
35     cntC.clear();
36     cntP.clear();
37   }
38 
SymbolSymbol39   Symbol() : timerHit(0) {}
InitSymbol40   void Init(const char* aName, u_long aAddress) {
41     name = aName ? strdup(aName) : (char*)"";
42     address = aAddress;
43   }
44 };
45 
46 struct LoadMapEntry {
47   char* name;      // name of .so
48   u_long address;  // base address where it was mapped in
49   LoadMapEntry* next;
50 };
51 
52 struct leaky {
53   leaky();
54   ~leaky();
55 
56   void initialize(int argc, char** argv);
57   void open(char* arg);
58 
59   char* applicationName;
60   int logFileIndex;
61   int numLogFiles;
62   char* progFile;
63   FILE* outputfd;
64 
65   bool quiet;
66   bool showAddress;
67   bool showThreads;
68   bool cleo;
69   u_int stackDepth;
70   int onlyThread;
71   char* output_dir;
72 
73   int mappedLogFile;
74   malloc_log_entry* firstLogEntry;
75   malloc_log_entry* lastLogEntry;
76 
77   int stacks;
78 
79   int sfd;
80   Symbol** externalSymbols;
81   Symbol** lastSymbol;
82   int usefulSymbols;
83   int numExternalSymbols;
84   StrSet exclusions;
85   u_long lowestSymbolAddr;
86   u_long highestSymbolAddr;
87 
88   LoadMapEntry* loadMap;
89 
90   bool collect_last;
91   int collect_start;
92   int collect_end;
93 
94   StrSet roots;
95   StrSet includes;
96 
97   void usageError();
98 
99   void LoadMap();
100 
101   void analyze(int thread);
102 
103   void dumpEntryToLog(malloc_log_entry* lep);
104 
105   void insertAddress(u_long address, malloc_log_entry* lep);
106   void removeAddress(u_long address, malloc_log_entry* lep);
107 
108   void displayStackTrace(FILE* out, malloc_log_entry* lep);
109 
110   Symbol** ExtendSymbols(int num);
111   void ReadSymbols(const char* fileName, u_long aBaseAddress);
112   void ReadSharedLibrarySymbols();
113   void setupSymbols(const char* fileName);
114   Symbol* findSymbol(u_long address);
115   bool excluded(malloc_log_entry* lep);
116   bool included(malloc_log_entry* lep);
indexToNameleaky117   const char* indexToName(int idx) { return externalSymbols[idx]->name; }
118 
119  private:
120   void generateReportHTML(FILE* fp, int* countArray, int count, int thread);
121   int findSymbolIndex(u_long address);
122 };
123 
124 #endif /* __leaky_h_ */
125