summaryrefslogtreecommitdiff
path: root/xen/common/coverage/gcov.c
blob: 327bf8d646c072f33001c11b5243f39470b13d86 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *  This code maintains a list of active profiling data structures.
 *
 *    Copyright IBM Corp. 2009
 *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
 *
 *    Uses gcc-internal data definitions.
 *    Based on the gcov-kernel patch by:
 *       Hubertus Franke <frankeh@us.ibm.com>
 *       Nigel Hinds <nhinds@us.ibm.com>
 *       Rajan Ravindran <rajancr@us.ibm.com>
 *       Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
 *       Paul Larson
 *
 *  Modified for Xen by:
 *    Wei Liu <wei.liu2@citrix.com>
 */

#include <xen/errno.h>
#include <xen/guest_access.h>
#include <xen/types.h>

#include <public/sysctl.h>

#include "coverage.h"
#include "gcov.h"

/**
 * gcov_store_uint32 - store 32 bit number in gcov format to buffer
 * @buffer: target buffer or NULL
 * @off: offset into the buffer
 * @v: value to be stored
 *
 * Number format defined by gcc: numbers are recorded in the 32 bit
 * unsigned binary form of the endianness of the machine generating the
 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
 * store anything.
 */
size_t gcov_store_uint32(void *buffer, size_t off, uint32_t v)
{
    uint32_t *data;

    if ( buffer )
    {
        data = buffer + off;
        *data = v;
    }

    return sizeof(*data);
}

/**
 * gcov_store_uint64 - store 64 bit number in gcov format to buffer
 * @buffer: target buffer or NULL
 * @off: offset into the buffer
 * @v: value to be stored
 *
 * Number format defined by gcc: numbers are recorded in the 32 bit
 * unsigned binary form of the endianness of the machine generating the
 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
 * anything.
 */
size_t gcov_store_uint64(void *buffer, size_t off, uint64_t v)
{
    uint32_t *data;

    if ( buffer )
    {
        data = buffer + off;

        data[0] = (v & 0xffffffffUL);
        data[1] = (v >> 32);
    }

    return sizeof(*data) * 2;
}

static size_t gcov_info_payload_size(const struct gcov_info *info)
{
    return gcov_info_to_gcda(NULL, info);
}

static int gcov_info_dump_payload(const struct gcov_info *info,
                                  XEN_GUEST_HANDLE_PARAM(char) buffer,
                                  uint32_t *off)
{
    char *buf;
    uint32_t buf_size;
    int ret;

    /*
     * Allocate a buffer and dump payload there. This helps us to not
     * have copy_to_guest in other functions and retain their simple
     * semantics.
     */

    buf_size = gcov_info_payload_size(info);
    buf = xmalloc_array(char, buf_size);

    if ( !buf )
    {
        ret = -ENOMEM;
        goto out;
    }

    gcov_info_to_gcda(buf, info);

    if ( copy_to_guest_offset(buffer, *off, buf, buf_size) )
    {
        ret = -EFAULT;
        goto out;
    }
    *off += buf_size;

    ret = 0;
 out:
    xfree(buf);
    return ret;

}

static uint32_t cf_check gcov_get_size(void)
{
    uint32_t total_size = sizeof(uint32_t); /* Magic number XCOV */
    struct gcov_info *info = NULL;

    while ( (info = gcov_info_next(info)) )
    {
        /* File name length, including trailing \0 */
        total_size += strlen(gcov_info_filename(info)) + 1;

        /* Payload size field */
        total_size += sizeof(uint32_t);

        /* Payload itself */
        total_size += gcov_info_payload_size(info);
    }

    return total_size;
}

static void cf_check gcov_reset_all_counters(void)
{
    struct gcov_info *info = NULL;

    while ( (info = gcov_info_next(info)) )
        gcov_info_reset(info);
}

static int gcov_dump_one_record(const struct gcov_info *info,
                                XEN_GUEST_HANDLE_PARAM(char) buffer,
                                uint32_t *off)
{
    uint32_t payload_size;
    uint32_t len;

    /* File name, including trailing \0 */
    len = strlen(gcov_info_filename(info)) + 1;
    if ( copy_to_guest_offset(buffer, *off, gcov_info_filename(info), len) )
        return -EFAULT;
    *off += len;

    payload_size = gcov_info_payload_size(info);
    /* Payload size */
    if ( copy_to_guest_offset(buffer, *off, (char*)&payload_size,
                              sizeof(uint32_t)) )
        return -EFAULT;
    *off += sizeof(uint32_t);

    /* Payload itself */
    return gcov_info_dump_payload(info, buffer, off);
}

static int cf_check gcov_dump_all(
    XEN_GUEST_HANDLE_PARAM(char) buffer, uint32_t *buffer_size)
{
    uint32_t off;
    uint32_t magic = XEN_GCOV_FORMAT_MAGIC;
    struct gcov_info *info = NULL;
    int ret;

    if ( *buffer_size < gcov_get_size() )
    {
        ret = -ENOBUFS;
        goto out;
    }

    off = 0;

    /* Magic number */
    if ( copy_to_guest_offset(buffer, off, (char *)&magic, sizeof(magic)) )
    {
        ret = -EFAULT;
        goto out;
    }
    off += sizeof(magic);

    while ( (info = gcov_info_next(info)) )
    {
        ret = gcov_dump_one_record(info, buffer, &off);
        if ( ret )
            goto out;
    }

    *buffer_size = off;

    ret = 0;
 out:
    return ret;
}

const struct cov_sysctl_ops cov_ops = {
    .get_size = gcov_get_size,
    .reset_counters = gcov_reset_all_counters,
    .dump = gcov_dump_all,
};

/*
 * Local variables:
 * mode: C
 * c-file-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */