-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
92 lines (83 loc) · 2.85 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* DynProf, an Dyninst-based dynamic profiler.
* Copyright (C) 2015-16 Peter Foley
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <unistd.h> // for F_OK
#include "dynprof.h" // for DynProf
static std::unique_ptr<std::string> get_path(const char* exe) {
std::unique_ptr<std::string> path(new std::string);
path->assign(getenv("PATH"));
std::unique_ptr<std::string> fullpath(new std::string);
size_t current = 0;
size_t offset;
do {
offset = path->find_first_of(":", current);
fullpath->assign(path->substr(current, offset - current));
// TODO(peter): support windows paths?
fullpath->append("/");
fullpath->append(exe);
if (access(fullpath->c_str(), F_OK) == 0) {
return fullpath;
}
current = offset + 1;
} while (offset != std::string::npos);
fullpath->assign(resolve_path(exe));
return fullpath;
}
static const char** get_params(std::vector<std::string> args) {
const char** params = static_cast<const char**>(calloc(args.size() + 1, sizeof(char*)));
// Skip the exe, that's resolved by get_path
for (size_t i = 1; i < args.size(); i++) {
params[i] = strdup(args[i].c_str());
}
return params;
}
static void usage() {
std::cerr << "Usage: ./dynprof (--write) [program] arg1,arg2,arg3..." << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
usage();
return 1;
}
std::vector<std::string> args(argv, argv + argc);
bool binary_edit = false;
if (args[1] == "--write") {
if (argc < 3) {
usage();
return 1;
}
binary_edit = true;
args.erase(args.begin());
}
std::unique_ptr<std::string> path = get_path(argv[0]);
if (!path) {
std::cerr << args[1] << " not found in path" << std::endl;
return 1;
}
args.erase(args.begin());
const char** params = get_params(args);
// set argv[0] to the original path.
params[0] = strdup(path->c_str());
DynProf prof(std::move(path), params);
if (binary_edit) {
prof.setupBinary();
return prof.writeOutput();
}
prof.start();
return prof.waitForExit();
}