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
|
/*
Copyright (C) 2008, 2009, 2010 Jiri Olsa <olsajiri@gmail.com>
This file is part of the latrace.
The latrace 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 3 of the License, or
(at your option) any later version.
The latrace 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 the latrace (file COPYING). If not, see
<http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "stack.h"
static int process_struct(struct lt_config_shared *cfg, struct lt_arg *arg,
void *pval, struct lt_args_data *data)
{
struct lt_arg *a;
int i = 0;
lt_args_cb_struct(cfg, LT_ARGS_STRUCT_ITSELF, arg, pval, data, 0);
if (arg->pointer)
pval = *((void**) pval);
lt_list_for_each_entry(a, arg->args_head, args_list) {
int last = (i + 1) == arg->mmbcnt;
/* For the type size up to word (4), the offset of the member
is aligned to its size. For the type size over the word,
the offset of the member is aligned to word. */
int nsize = a->type_len > 4 ? 4 : a->type_len;
int naligned = (u_int) pval % nsize;
if (naligned)
pval += nsize - naligned;
lt_args_cb_struct(cfg, LT_ARGS_STRUCT_ARG, a, pval, data, last);
pval += a->type_len;
i++;
}
return 0;
}
/*
the traced program stack (in regs)
should look like this:
...
esp + 12 2nd argument
esp + 8 1st argument
esp + 4 possible return structure/union address
esp return function address
*/
int lt_stack_process(struct lt_config_shared *cfg, struct lt_args_sym *asym,
La_regs *regs, struct lt_args_data *data)
{
int i;
void *pval;
struct lt_arg *argret;
/* get the esp reg and skip the return address */
pval = (void*) regs->lr_esp;
pval += sizeof(void*);
/* if the function returns structure by value,
there's a hidden first argument we need to skip */
argret = asym->args[LT_ARGS_RET];
if ((!argret->pointer) &&
(argret->dtype == LT_ARGS_DTYPE_STRUCT))
pval += sizeof(void*);
for(i = 1; i < asym->argcnt; i++) {
struct lt_arg *arg = asym->args[i];
int last = (i + 1) == asym->argcnt;
lt_args_cb_arg(cfg, arg, pval, data, last, 1);
if ((cfg->args_detailed) &&
(LT_ARGS_DTYPE_STRUCT == arg->dtype))
process_struct(cfg, arg, pval, data);
pval += arg->pointer ? sizeof(void*) : LT_STACK_ALIGN(arg->type_len);
}
return 0;
}
/* x86 is easy, everything is in eax register */
int lt_stack_process_ret(struct lt_config_shared *cfg, struct lt_args_sym *asym,
La_retval *regs, struct lt_args_data *data)
{
struct lt_arg *arg;
void *pval;
pval = (void*) &(regs->lrv_eax);
arg = asym->args[LT_ARGS_RET];
lt_args_cb_arg(cfg, arg, pval, data, 1, 0);
if ((cfg->args_detailed) &&
(LT_ARGS_DTYPE_STRUCT == arg->dtype)) {
/* The process_struct function does its own
dereference of pval value in case of pointer
argument, so we need to prepare pval correctly. */
if (!arg->pointer)
pval = (void*) regs->lrv_eax;
process_struct(cfg, arg, pval, data);
}
return 0;
}
|