Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d64a8e10dbfd2b272da8ad8ea69917291af20aba (plain) (blame)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*******************************************************************************
 * Copyright (c) 2013 Ericsson
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Matthew Khouzam - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.ctf.core.tests.trace;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
import org.eclipse.linuxtools.internal.ctf.core.event.metadata.IOStructGen;
import org.junit.Test;

/**
 * Unit tests for {@link IOStructGen}
 *
 * @author Matthew Khouzam
 */
public class IOstructgenTest {

    private static final String metadataDecs = "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
            + "typealias integer { size = 16; align = 8; signed = false; } := uint16_t;\n"
            + "typealias integer { size = 32; align = 8; signed = false; } := uint32_t;\n"
            + "typealias integer { size = 64; align = 8; signed = false; } := uint64_t;\n"
            + "typealias integer { size = 64; align = 8; signed = false; } := unsigned long;\n"
            + "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
            + "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
            + "typealias integer { size = 32; align = 1; signed = true; base = decimal; } := int32_t;\n"
            + "typealias integer { size = 31; align = 1; signed = true; base = dec; } := int31_t;\n"
            + "typealias integer { size = 30; align = 1; signed = true; base = d; } := int30_t;\n"
            + "typealias integer { size = 29; align = 1; signed = true; base = i; } := int29_t;\n"
            + "typealias integer { size = 28; align = 1; signed = true; base = u; } := int28_t;\n"
            + "typealias integer { size = 27; align = 1; signed = true; base = hexadecimal; } := int27_t;\n"
            + "typealias integer { size = 26; align = 1; signed = true; base = hex; } := int26_t;\n"
            + "typealias integer { size = 25; align = 1; signed = true; base = x; } := int25_t;\n"
            + "typealias integer { size = 24; align = 1; signed = true; base = X; } := int24_t;\n"
            + "typealias integer { size = 23; align = 1; signed = true; base = p; } := int23_t;\n"
            + "typealias integer { size = 22; align = 1; signed = true; base = 16; } := int22_t;\n"
            + "typealias integer { size = 21; align = 1; signed = true; base = oct; } := int21_t;\n"
            + "typealias integer { size = 20; align = 1; signed = true; base = b; } := int20_t;\n"
            + "typealias integer { size = 19; align = 1; signed = true; base = octal; } := int19_t;\n"
            + "typealias integer { size = 18; align = 1; signed = true; base = o; } := int18_t;\n"
            + "typealias integer { size = 17; align = 1; signed = true; base = binary; } := int17_t;\n"
            + "\n"
            + "trace {\n"
            + "    major = 1;\n"
            + "    minor = 8;\n"
            + "    uuid = \"b04d391b-e736-44c1-8d89-4bb438857f8d\";\n"
            + "    byte_order = le;\n"
            + "    packet.header := struct {\n"
            + "        uint32_t magic;\n"
            + "        uint8_t  uuid[16];\n"
            + "        uint32_t stream_id;\n" + "    };\n" + "};\n" + "\n";
    private static final String environmentMD = "env {\n"
            + "    hostname = \"DemoSystem\";\n"
            + "    vpid = 1337;\n"
            + "    procname = \"demo\";\n"
            + "    domain = \"autogenerated\";\n"
            + "    tracer_name = \"tmf\";\n"
            + "    tracer_major = 2;\n"
            + "    tracer_minor = 0x01;\n"
            + "    tracer_patchlevel = 0;\n"
            + "};\n" + "\n";
    private static final String clockMD = "clock {\n" + "    name = monotonic;\n"
            + "    uuid = \"cbf9f42e-9be7-4798-a96f-11db556e2ebb\";\n"
            + "    description = \"Monotonic Clock\";\n"
            + "    freq = 1000000000; /* Frequency, in Hz */\n"
            + "    /* clock value offset from Epoch is: offset * (1/freq) */\n"
            + "    offset = 1350310657466295832;\n" + "};\n"
            + "\n";

    private static final String ctfStart =
            "typealias integer {\n"
                    + "    size = 27; align = 1; signed = false;\n"
                    + "    map = clock.monotonic.value;\n"
                    + "} := uint27_clock_monotonic_t;\n"
                    + "\n"
                    + "typealias integer {\n"
                    + "    size = 32; align = 8; signed = false;\n"
                    + "    map = clock.monotonic.value;\n"
                    + "} := uint32_clock_monotonic_t;\n"
                    + "\n"
                    + "typealias integer {\n"
                    + "    size = 64; align = 8; signed = false;\n"
                    + "    map = clock.monotonic.value;\n"
                    + "} := uint64_clock_monotonic_t;\n"
                    + "\n";

    private static final String ctfHeaders =
            "struct packet_context {\n"
                    + "    uint64_clock_monotonic_t timestamp_begin;\n"
                    + "    uint64_clock_monotonic_t timestamp_end;\n"
                    + "    uint64_t content_size;\n"
                    + "    uint64_t packet_size;\n"
                    + "    unsigned long events_discarded;\n"
                    + "    uint32_t cpu_id;\n"
                    + "};\n"
                    + "\n"
                    + "struct event_header_compact {\n"
                    + "    enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
                    + "    variant <id> {\n"
                    + "        struct {\n"
                    + "            uint27_clock_monotonic_t timestamp;\n"
                    + "        } compact;\n"
                    + "        struct {\n"
                    + "            uint32_t id;\n"
                    + "            uint64_clock_monotonic_t timestamp;\n"
                    + "        } extended;\n"
                    + "    } v;\n"
                    + "} align(8);\n"
                    + "\n"
                    + "struct event_header_large {\n"
                    + "    enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
                    + "    variant <id> {\n" + "        struct {\n"
                    + "            uint32_clock_monotonic_t timestamp;\n"
                    + "        } compact;\n" + "        struct {\n"
                    + "            uint32_t id;\n"
                    + "            uint64_clock_monotonic_t timestamp;\n"
                    + "        } extended;\n" + "    } v;\n" + "} align(8);\n" + "\n";

    private static final String ctfBody = "stream {\n"
            + "    id = 0;\n"
            + "    event.header := struct event_header_compact;\n"
            + "    packet.context := struct packet_context;\n"
            + "    event.context := struct {\n"
            + "        integer { size = 64; align = 8; signed = 0; encoding = none; base = 16; } _ip;\n"
            + "    };\n"
            + "};\n"
            + "\n"
            + "event {\n"
            + "    name = \"ust_tests_demo3:done\";\n"
            + "    id = 0;\n"
            + "    stream_id = 0;\n"
            + "    loglevel = 4;\n"
            + "    fields := struct {\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n"
            + "    };\n"
            + "};\n"
            + "\n"
            + "event {\n"
            + "    name = \"ust_tests_demo:starting\";\n"
            + "    id = 1;\n"
            + "    stream_id = 0;\n"
            + "    loglevel = 2;\n"
            + "    model.emf.uri = \"http://example.com/path_to_model?q=ust_tests_demo:starting\";\n"
            + "    fields := struct {\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n"
            + "    };\n"
            + "};\n"
            + "\n"
            + "event {\n"
            + "    name = \"ust_tests_demo:done\";\n"
            + "    id = 2;\n"
            + "    stream_id = 0;\n"
            + "    loglevel = 2;\n"
            + "    model.emf.uri = \"http://example.com/path_to_model?q=ust_tests_demo:done\";\n"
            + "    fields := struct {\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n"
            + "    };\n"
            + "};\n"
            + "\n"
            + "event {\n"
            + "    name = \"ust_tests_demo2:loop\";\n"
            + "    id = 3;\n"
            + "    stream_id = 0;\n"
            + "    loglevel = 4;\n"
            + "    fields := struct {\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _intfield;\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 16; } _intfield2;\n"
            + "        integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _longfield;\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; byte_order = be; } _netintfield;\n"
            + "        integer { size = 32; align = 8; signed = 1; encoding = none; base = 16; byte_order = be; } _netintfieldhex;\n"
            + "        integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _arrfield1[3];\n"
            + "        integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _arrfield2[10];\n"
            + "        integer { size = 64; align = 8; signed = 0; encoding = none; base = 10; } __seqfield1_length;\n"
            + "        integer { size = 8; align = 8; signed = 1; encoding = none; base = 10; } _seqfield1[ __seqfield1_length ];\n"
            + "        integer { size = 64; align = 8; signed = 0; encoding = none; base = 10; } __seqfield2_length;\n"
            + "        integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _seqfield2[ __seqfield2_length ];\n"
            + "        string _stringfield;\n"
            + "        floating_point { exp_dig = 8; mant_dig = 24; align = 8; } _floatfield;\n"
            + "        floating_point { exp_dig = 11; mant_dig = 53; align = 8; } _doublefield;\n"
            + "    };\n"
            + "};\n"
            + "\n";

    private static final String enumMd =
            "typealias integer { size = 32; align = 8; signed = false; } := int;\n"
                    + "typealias enum { ONE = 0, a,b,c=10, d} := useless_enum;\n"
                    + "struct useless{ \n"
                    + "    enum : uint8_t { A=0, \"B\",} enum3;\n"
                    + "    useless_enum enum2;"
                    + "    enum { C, D, E } enum4;\n"
                    + "    uint16_t val;\n"
                    + "} ;\n"
                    + "\n"
                    + "event {\n"
                    + "   name = \"enumEvent\";\n"
                    + "   id = 6;\n"
                    + "   stream_id = 0;\n"
                    + "   loglevel = 5;\n"
                    + "   fields := struct{\n"
                    + "       uint16_t _some_field;\n"
                    // + "       useless junk;\n"
                    // + "       bad_enum a;\n"
                    + "       enum {A, B, C = 3 , } _other_enum;\n"
                    + "   };\n"
                    + "};\n"
                    + "\n";

    private final static String contextMD =
            "event {\n" +
                    "   name = \"someOtherEvent\";\n" +
                    "   id = 5;\n" +
                    "   stream_id = 0;\n" +
                    "   loglevel = 5;\n" +
                    "   context := struct{\n" +
                    "       uint16_t _someContext;\n" +
                    "   };\n" +
                    "   fields := struct{\n" +
                    "       uint16_t _somefield;\n" +
                    "   };\n" +
                    "};\n " +
                    "\n";

    private static final String callsiteMD =
            "callsite {\n"
                    + "    name = \"ust_tests_demo2:loop\";\n"
                    + "    func = \"main\";\n" + "    ip = 0x400a29;\n"
                    + "    file = \"demo.c\";\n" + "    line = 59;\n" + "};\n" + "\n"
                    + "callsite {\n" + "    name = \"ust_tests_demo3:done\";\n"
                    + "    func = \"main\";\n" + "    ip = 0x400a6c;\n"
                    + "    file = \"demo.c\";\n" + "    line = 62;\n" + "};\n" + "\n"
                    + "callsite {\n" + "    name = \"ust_tests_demo:done\";\n"
                    + "    func = \"main\";\n" + "    ip = 0x400aaf;\n"
                    + "    file = \"demo.c\";\n" + "    line = 61;\n" + "};\n" + "\n"
                    + "callsite {\n" + "    name = \"ust_tests_demo:starting\";\n"
                    + "    func = \"main\";\n" + "    ip = 0x400af2;\n"
                    + "    file = \"demo.c\";\n" + "    line = 55;\n" + "};\n";

    private static final String simpleTSDL = metadataDecs + ctfStart + ctfHeaders
            + ctfBody;
    private static final String enumTSDL = metadataDecs + ctfStart + ctfHeaders
            + ctfBody + enumMd;
    private static final String clockTSDL = metadataDecs + clockMD + ctfStart
            + ctfHeaders + ctfBody;
    private static final String envTSDL = metadataDecs + environmentMD + ctfStart
            + ctfHeaders + ctfBody;
    private static final String contextTSDL = metadataDecs + environmentMD + ctfStart
            + ctfHeaders + ctfBody + contextMD;
    private static final String callsiteTSDL = metadataDecs + ctfStart + ctfHeaders
            + ctfBody + callsiteMD;
    private static final String allDressedTSDL = metadataDecs + environmentMD + clockMD
            + ctfStart + ctfHeaders + ctfBody + enumMd + callsiteMD;

    static final String tempTraceDir = System.getProperty("java.io.tmpdir")
            + "/" + "tempTrace";

    private static void deltree(File f) {
        for (File elem : f.listFiles()) {
            if (elem.isDirectory()) {
                deltree(elem);
            }
            elem.delete();
        }
        f.delete();
    }

    private static void createDummyTrace(String metadata) {

        File f;
        try {
            f = new File(tempTraceDir);
            if (f.exists()) {
                deltree(f);
            }
            f.mkdirs();
            f = new File(tempTraceDir + "/metadata");
            FileWriter fw = new FileWriter(f);
            fw.write(metadata);
            fw.close();
            f.createNewFile();

            byte magicLE[] = { (byte) 0xC1, (byte) 0x1F, (byte) 0xFC, (byte) 0xC1 };
            byte uuid[] = { (byte) 0xb0, 0x4d, 0x39, 0x1b, (byte) 0xe7, 0x36,
                    0x44, (byte) 0xc1, (byte) 0x8d, (byte) 0x89, 0x4b,
                    (byte) 0xb4, 0x38, (byte) 0x85, 0x7f, (byte) 0x8d };
            final int size = 4096;
            byte[] data = new byte[size];
            for (int i = 0; i < size; i++) {
                data[i] = 0x00;
            }
            f = new File(tempTraceDir + "/dummyChan");
            fw = new FileWriter(f);
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(magicLE);
            fos.write(uuid);
            fos.write(data);
            fos.close();
            fw.close();
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            f = null;
        }

    }

    /**
     * Simple test (only the minimum)
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLSimpleTest() throws CTFReaderException {
        createDummyTrace(simpleTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);

    }

    /**
     * Test with environment variables
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLEnvironmentTest() throws CTFReaderException {
        createDummyTrace(envTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);
    }

    /**
     * Test with Clocks
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLEnumTest() throws CTFReaderException {
        createDummyTrace(enumTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);
    }

    /**
     * Test with Clocks
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLClockTest() throws CTFReaderException {
        createDummyTrace(clockTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);
    }

    /**
     * Test with Contexts
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLContextTest() throws CTFReaderException {
        createDummyTrace(contextTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);
    }

    /**
     * Test with Callsites
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLCallsiteTest() throws CTFReaderException {
        createDummyTrace(callsiteTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);
    }

    /**
     * Test everything
     *
     * @throws CTFReaderException
     *             something wrong happened
     */
    @Test
    public void TSDLAllTest() throws CTFReaderException {
        createDummyTrace(allDressedTSDL);
        CTFTrace trace = new CTFTrace(tempTraceDir);
        assertNotNull(trace);

        HashMap<Long, IEventDeclaration> events = trace.getEvents(0L);
        final EventDeclaration eventDeclaration = (EventDeclaration) events.get(2L);
        assertEquals("http://example.com/path_to_model?q=ust_tests_demo:done",
                eventDeclaration.getCustomAttribute("model.emf.uri"));
    }

}

Back to the top