File: test_extra_flag_strings.c

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (288 lines) | stat: -rw-r--r-- 7,886 bytes parent folder | download
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*  Copyright (c) 2019-2019, David Anderson
    All rights reserved.

    Redistribution and use in source and binary forms, with
    or without modification, are permitted provided that the
    following conditions are met:

    Redistributions of source code must retain the above
    copyright notice, this list of conditions and the following
    disclaimer.

    Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials
    provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "libdwarfdefs.h"
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#ifdef HAVE_STDINT_H
#include <stdint.h> /* For uintptr_t */
#endif /* HAVE_STDINT_H */
#include "pro_incl.h"
#include "dwarf.h"
#include "libdwarf.h"
#include "pro_opaque.h"
#include "dwarfstring.h"


static int errcount;

/* int
_dwarf_log_extra_flagstrings(Dwarf_P_Debug dbg,
  const char *extra,
  int *err)
*/



static void
resetdbg(Dwarf_P_Debug dbg)
{
    memset(dbg,0,sizeof(struct Dwarf_P_Debug_s));
}

#if 0
"default_is_stmt"
"minimum_instruction_length"
"maximum_operations_per_instruction"
"opcode_base"
"line_base"
"line_range"
"linetable_version"
"segment_selector_size"
"segment_size"
dbg->de_line_inits.pi_default_is_stmt = (unsigned)v;
dbg->de_line_inits.pi_minimum_instruction_length = (unsigned)v;
dbg->de_line_inits.pi_maximum_operations_per_instruction = (unsigned)v;
dbg->de_line_inits.pi_opcode_base = (unsigned)v;
dbg->de_line_inits.pi_line_base = (int)v;
dbg->de_line_inits.pi_line_range = (int)v;
dbg->de_line_inits.pi_linetable_version = (unsigned)v;
dbg->de_line_inits.pi_segment_selector_size = (unsigned)v;
dbg->de_line_inits.pi_segment_size = (unsigned)v;
#endif /* 0 */

const char *
returnvalstr(int res)
{
    if (res == DW_DLV_OK) return "DW_DLV_OK";
    if (res == DW_DLV_NO_ENTRY) return "DW_DLV_NO_ENTRY";
    if (res == DW_DLV_ERROR) return "DW_DLV_ERROR";
    return "Unknown";
}


static void
check_expected(int exp, int got, int err_exp, int err_got,
    Dwarf_Signed exp_val, Dwarf_Signed got_val, int line)
{
    if ( exp != got) {
        ++errcount;
        printf("Expected res %s, got %s line %d\n",
            returnvalstr(exp), returnvalstr(got),line);
    }
    if ( err_exp != err_got) {
        ++errcount;
        printf("Expected err code %d, got %d line %d\n",
            err_exp,err_got,line);
    }
    if ( exp_val != got_val) {
        ++errcount;
        printf("Expected value %" DW_PR_DSd ", got %" DW_PR_DSd " line %d\n",
            exp_val,got_val,line);
    }
}

static void
test1(Dwarf_P_Debug dbg)
{
    int res = 0;
    int err = 0;

    res = _dwarf_log_extra_flagstrings(dbg,"default_is_stmt=1",&err);
    check_expected(DW_DLV_OK,res,0,err,1,dbg->de_line_inits.pi_default_is_stmt,
        __LINE__);

    err = 0;
    res = _dwarf_log_extra_flagstrings(dbg,0,&err);
    check_expected(DW_DLV_NO_ENTRY,res,0,err,0,0,
        __LINE__);
    err = 0;
    res = _dwarf_log_extra_flagstrings(dbg,"",&err);
    check_expected(DW_DLV_NO_ENTRY,res,0,err,0,0,
        __LINE__);

    err = 0;
    resetdbg(dbg);
    res = _dwarf_log_extra_flagstrings(dbg,",line_base=-1,",&err);
    check_expected(DW_DLV_OK,res,0,err,-1,dbg->de_line_inits.pi_line_base,
        __LINE__);

    err = 0;
    resetdbg(dbg);
    res = _dwarf_log_extra_flagstrings(dbg,",,line_base=-1,,",&err);
    check_expected(DW_DLV_OK,res,0,err,-1,dbg->de_line_inits.pi_line_base,
        __LINE__);

    resetdbg(dbg);
    err = 0;
    res = _dwarf_log_extra_flagstrings(dbg,",,line_base =-1,,",&err);
    check_expected(DW_DLV_ERROR,res,err,DW_DLE_PRO_INIT_EXTRAS_ERR,dbg->de_line_inits.pi_line_base,0,
        __LINE__);

}
static void
test2(Dwarf_P_Debug dbg)
{
    int res = 0;
    int err = 0;

    resetdbg(dbg);
    err = 0;

    res = _dwarf_log_extra_flagstrings(dbg,
        "default_is_stmt=1,line_base=2",&err);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_default_is_stmt,1,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_line_base,2,
        __LINE__);

    resetdbg(dbg);
    err = 0;
    /* intentional typo here */
    res = _dwarf_log_extra_flagstrings(dbg,
        "minimum_instruction_lengty=3,"
        "maximum_operations_per_instruction=4",&err);
    check_expected(DW_DLV_ERROR,res,
        err,DW_DLE_PRO_INIT_EXTRAS_UNKNOWN,
        dbg->de_line_inits.pi_minimum_instruction_length,
        0,
        __LINE__);
    check_expected(DW_DLV_ERROR,res,
        err,DW_DLE_PRO_INIT_EXTRAS_UNKNOWN,
        dbg->de_line_inits.pi_maximum_operations_per_instruction,
        0,
        __LINE__);

    resetdbg(dbg);
    err = 0;

    res = _dwarf_log_extra_flagstrings(dbg,
        "default_is_stmt=1,"
        "minimum_instruction_length=2,"
        "maximum_operations_per_instruction=3,"
        "opcode_base=4,"
        "line_base=5,"
        "line_range=6,"
        "linetable_version=7,"
        "segment_selector_size=8,"
        "segment_size=9",
        &err);

    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_default_is_stmt,1,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_minimum_instruction_length,2,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_maximum_operations_per_instruction,3,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_opcode_base,4,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_line_base,5,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_line_range,6,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_linetable_version,7,
        __LINE__);

    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_segment_selector_size,8,
        __LINE__);

    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_segment_size,9,
        __LINE__);






}

static void
test3(Dwarf_P_Debug dbg)
{
    int res = 0;
    int err = 0;

    /*  The following identical to a current dwarfgen.cc use. */
    res = _dwarf_log_extra_flagstrings(dbg,
        "opcode_base=13,"
        "minimum_instruction_length=1,"
        "line_base=-1,"
        "line_range=4",&err);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_opcode_base,13,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_minimum_instruction_length,1,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_line_base,-1,
        __LINE__);
    check_expected(DW_DLV_OK,res,err,0,
        dbg->de_line_inits.pi_line_range,4,
        __LINE__);

}


int main()
{

    struct Dwarf_P_Debug_s sdbg;
    Dwarf_P_Debug dbg =  &sdbg;

    resetdbg(dbg);

    test1(dbg);

    resetdbg(dbg);

    test2(dbg);
    resetdbg(dbg);
    test3(dbg);

    if (errcount) {
        return 1;
    }
    return 0;
}