aboutsummaryrefslogtreecommitdiff
path: root/acid/trump
blob: 4921bfce620d39cdbcdd46822dd3de18877effa0 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// trace user malloc pool - trace malloc, realloc, and free calls
// if trumpsbrk is set, we trace sbrkalloc and sbrkmerge too.

_stoprunning = 0;
trumphexaddrs = 0;
trumpsbrk = 0;

defn stopped(pid) {
	local l;
	local pc;
	pc = *PC;
	if notes then {
		if (notes[0]!="sys: breakpoint") then
		{
			print(pid,": ",reason(*TRAP),"\t");
			print(fmt(pc,97),"\t",fmt(pc,105),"\n");
			print("Notes pending:\n");
			l = notes;
			while l do
			{
				print("\t",head l,"\n");
				l = tail l;
			}
			_stoprunning = 1;
		}
	}
}

defn printstack() {
	local frame, stk, pcs, lst, x;

	pcs = {*PC};
	stk = strace(*PC,*SP,0);
	while stk do {
		pcs = append pcs, stk[0][1];
		stk = tail stk;
	}

	print(" #");
	lst = pcs;
	while lst do {
		if trumphexaddrs != 0 then
			x = lst[0]\X;
		else
			x = lst[0]\a;
		print(" src(", x, ");");
		lst = tail lst;
	}
	print("\n");
}

defn setuptrump() {
	mallocPC = malloc;
	malloczPC = mallocz;
	freePC = free;
	reallocPC = realloc;
	sbrkallocPC = sbrkalloc;
	sbrkmergePC = sbrkmerge;

	// linker might fill delay slot with first instruction
	if objtype == "mips" then {
		mallocPC = mallocPC+4;
		malloczPC = malloczPC+4;
		freePC = freePC+4;
		reallocPC = reallocPC+4;
		sbrkallocPC = sbrkallocPC+4;
		sbrkmergePC = sbrkmergePC+4;
	}

	bpset(mallocPC);
	bpset(malloczPC);
	bpset(freePC);
	bpset(reallocPC);
	if trumpsbrk then {
		bpset(sbrkallocPC);
		bpset(sbrkmergePC);
	}
}

defn cleantrump() {
	stop(pid);

	bpdel(mallocPC);
	bpdel(malloczPC);
	bpdel(freePC);
	bpdel(reallocPC);
	bpdel(sbrkallocPC);
	bpdel(sbrkmergePC);
}

defn trumpflush() {
	stop(pid);		// already stopped, but flushes output
}

defn new() {
	bplist = {};
	newproc(progargs);
	bpset(follow(main)[0]);
	cont();
	bpdel(*PC);
	// clear the hang bit, which is left set by newproc, so programs we fork/exec don't hang
	printto("/proc/"+itoa(pid)+"/ctl", "nohang");
}

defn trumpfninfo() {
	local arg0, arg1, stk, retpc, params;

	stk = strace(*PC, *SP, 0);
	retpc = stk[0][1];
	params = stk[0][2];
	arg0 = params[0][1];
	arg1 = 0;
	if tail params != {} then
		arg1 = params[1][1];
	return {arg0, arg1, retpc};
}

defn trumpretval() {
	if objtype=="386" then
		return *AX;
	if objtype=="mips" then
		return *R1;
	if objtype=="power" || objtype=="alpha" then
		return *R0;
}

defn trump() {
	local arg0, arg1, pc, ret, x;

	stop(pid);
	_stoprunning = 0;
	setuptrump();
	while !_stoprunning do {
		cont();
		if notes[0]!="sys: breakpoint" then {
			cleantrump();
			return {};
		}

		pc = *PC;
		x = trumpfninfo();
		arg0 = x[0];
		if pc == reallocPC || pc == sbrkmergePC then 
			arg1 = x[1];
		bpset(x[2]);
		cont();
		bpdel(x[2]);
		ret = trumpretval();
		if pc == mallocPC then
			print(ret\X, " malloc ", arg0\D);
		if pc == malloczPC then
			print(ret\X, " mallocz ", arg0\D);
		if pc == freePC then
			print(arg0\X, " free");
		if pc == reallocPC then
			print(ret\X, " realloc ", arg0\X, " ", arg1\D);
		if pc == sbrkallocPC then
			print(ret\X, " sbrkalloc ", arg0\D);
		if pc == sbrkmergePC then
			print("sbrkmerge ", arg0\X, " ", arg1\X, " = ", ret\D);
		printstack();
		trumpflush();
	}
}

defn untrump() {
	cleantrump();
	start(pid);
}

print(acidfile);