summaryrefslogtreecommitdiff
path: root/cesar/lib/src/trace.c
blob: ad0d54377aa50fd6d918132b05d8115049d570c2 (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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/src/trace.c
 * \brief   Trace system.
 * \ingroup lib
 */
#include "common/std.h"

#include "lib/trace.h"
#include "hal/arch/arch.h"
#include "hal/arch/io.h"

#include <stdio.h>

#define TRACE_ALIGN (sizeof (u32))
#define TRACE_DEBUG_ARGS 0

#define TRACE_CHUNK_SIZE (BLK_SIZE / TRACE_ALIGN)

/** Size of output buffer. */
#define TRACE_DUMP_TEXT 2000

/** Number of slack characters to avoid dump retry when at end of output
 * buffer. */
#define TRACE_DUMP_TEXT_SLACK 20

/** Uninitialised trace data. */
#define TRACE_UNINITIALISED 0xffffffff

#if CONFIG_TRACE_ALL_SLOW && CONFIG_TRACE_ALL_FAST
# error "traces can not be slow and fast"
#endif

/** Trace system context. */
struct trace_t
{
    /** List of buffers. */
    list_t buffers;
};

static trace_t trace_global;

void
trace_init (void)
{
    trace_t * const ctx = &trace_global;
    list_init (&ctx->buffers);
#if CONFIG_TRACE_ON_FATAL
    dbg_assert (!dbg_fatal_cb[0]);
    dbg_fatal_cb[0] = trace_dbg_dump_all;
#endif
}

void
trace_uninit (void)
{
    trace_t * const ctx = &trace_global;
    dbg_assert (list_empty (&ctx->buffers));
}

bool
trace_drop_chunks (uint n)
{
    dbg_assert (n > 0);
    trace_t * const ctx = &trace_global;
    /* Can not help if no trace buffers. */
    if (list_empty (&ctx->buffers))
        return false;
    while (n)
    {
        /* Search the biggest buffer, taking drop level into account. */
        trace_buffer_t *bigest_buf = NULL;
        uint bigest_buf_size = 0, second_buf_size = 0;
        list_node_t *i, *end;
        i = list_begin (&ctx->buffers);
        end = list_end (&ctx->buffers);
        for (; i != end; i = list_next (i))
        {
            trace_buffer_t *buf = PARENT_OF (trace_buffer_t, node, i);
            if (!buf->locked)
            {
                uint buf_size_shifted = buf->chunks_nb > buf->preload
                    ? (buf->chunks_nb - buf->preload) << buf->drop_level
                    : 0;
                if (buf_size_shifted > bigest_buf_size)
                {
                    second_buf_size = bigest_buf_size;
                    bigest_buf_size = buf_size_shifted;
                    bigest_buf = buf;
                }
            }
        }
        /* Drop as many block as possible to satisfy drop request. */
        if (bigest_buf_size == 0)
            return false;
        uint i_can_drop = MIN (n, ((bigest_buf_size - second_buf_size +
                                    (1 << bigest_buf->drop_level) - 1)
                                   >> bigest_buf->drop_level));
        trace_chunk_t *hdrop, *tdrop;
        hdrop = bigest_buf->head;
        tdrop = hdrop;
        n -= i_can_drop;
        arch_atomic_add ((int *) &bigest_buf->chunks_nb, -i_can_drop);
        while (--i_can_drop)
            tdrop = tdrop->next;
        bigest_buf->head = tdrop->next;
        bigest_buf->tail->next = bigest_buf->head;
        blk_release_desc_range ((blk_t *) hdrop, (blk_t *) tdrop);
    }
    return true;
}

static int
trace_format_bool (char *text, uint text_size, int data)
{
    const char *t;
    uint ts;
    if (!data)
    {
        t = "false"; ts = 5;
    }
    else
    {
        t = "true"; ts = 4;
    }
    if (ts > text_size)
        return -1;
    else
    {
        memcpy (text, t, ts);
        return ts;
    }
}

static int
trace_format_decimal (char *text, uint text_size, int data)
{
    int ret = snprintf (text, text_size, "%d", data);
    return ret < (int) text_size ? ret : -1;
}

static int
trace_format_unsigned (char *text, uint text_size, int data)
{
    int ret = snprintf (text, text_size, "%u", data);
    return ret < (int) text_size ? ret : -1;
}

static int
trace_format_hexa (char *text, uint text_size, int data)
{
    int ret = snprintf (text, text_size, "0x%08x", data);
    return ret < (int) text_size ? ret : -1;
}

static const char trace_format_hexdigits[] = "0123456789abcdef";

static int
trace_format_mac (char *text, uint text_size, u64 data)
{
    const uint size = 3 * 6 - 1;
    if (text_size < size)
        return -1;
    else
    {
        uint i;
        u64 v;
        v = data;
        for (i = 5; i; i--)
        {
            *text++ = trace_format_hexdigits[(v >> 1*4) & 0xf];
            *text++ = trace_format_hexdigits[(v >> 0*4) & 0xf];
            *text++ = ':';
            v >>= 8;
        }
        *text++ = trace_format_hexdigits[(v >> 1*4) & 0xf];
        *text++ = trace_format_hexdigits[(v >> 0*4) & 0xf];
        return size;
    }
}

void
trace_namespace_init (trace_namespace_t *ns,
                      const trace_event_id_t *event_ids, uint event_ids_nb)
{
    uint i;
    dbg_assert (ns);
    for (i = 0; i < event_ids_nb; i++)
        dbg_assert (event_ids[i].format_string);
    /* Initialise fields. */
    ns->event_ids = event_ids;
    ns->event_ids_nb = event_ids_nb;
    for (i = 0; i < COUNT (ns->formats); i++)
        ns->formats[i].size = 0;
    /* Provide default useful formats. */
    trace_namespace_register_format (ns, 'b', trace_format_bool);
    trace_namespace_register_format (ns, 'd', trace_format_decimal);
    trace_namespace_register_format (ns, 'u', trace_format_unsigned);
    trace_namespace_register_format (ns, 'x', trace_format_hexa);
    trace_namespace_register_format_u64 (ns, 'm', trace_format_mac);
}

void
trace_namespace_register_format (trace_namespace_t *ns, char code,
                                 trace_format_u32_t callback)
{
    dbg_assert (ns);
    dbg_assert ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z'));
    dbg_assert (callback);
    ns->formats[code - 'A'].callback.format_u32 = callback;
    ns->formats[code - 'A'].size = 1;
}

void
trace_namespace_register_format_u64 (trace_namespace_t *ns, char code,
                                     trace_format_u64_t callback)
{
    dbg_assert (ns);
    dbg_assert ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z'));
    dbg_assert (callback);
    ns->formats[code - 'A'].callback.format_u64 = callback;
    ns->formats[code - 'A'].size = 2;
}

void
trace_namespace_register_format_table (trace_namespace_t *ns, char code,
                                       trace_format_table_t callback,
                                       uint size)
{
    dbg_assert (ns);
    dbg_assert ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z'));
    dbg_assert (callback);
    dbg_assert (size > 2);
    ns->formats[code - 'A'].callback.format_table = callback;
    ns->formats[code - 'A'].size = size;
}

void
trace_buffer_add (trace_buffer_t *buf, const char *name, uint drop_level,
                  uint preload, bool locked, trace_namespace_t *namespace)
{
    dbg_assert (buf);
    dbg_assert (name);
    dbg_assert (preload > 0);
    trace_t * const ctx = &trace_global;
    /* Initialise trace buffer. */
    list_init_node (&buf->node);
    buf->chunks_nb = preload;
    buf->drop_level = drop_level;
    buf->preload = preload + (CONFIG_TRACE_ALL_FAST ? 4 : 0);
    buf->locked = locked && !CONFIG_TRACE_ALL_SLOW || CONFIG_TRACE_ALL_FAST;
    buf->bare = NULL;
    buf->name = name;
    buf->namespace = namespace;
    /* Allocate chunks. */
    blk_t *tail;
    buf->head = (trace_chunk_t *) blk_alloc_desc_range (preload, &tail);
    buf->tail = (trace_chunk_t *) tail;
    buf->tail->next = buf->head;
    /* Initialise chunks. */
    trace_chunk_t *i = buf->head;
    do
    {
        i->tail_index = 0;
        i->commit_index = 0;
        if (i == buf->tail)
            break;
        i = i->next;
    } while (1);
    /* Add it to context. */
    list_push (&ctx->buffers, &buf->node);
}

void
trace_buffer_add_bare (trace_buffer_t *buf, const char *name,
                       trace_buffer_bare_t *bare, u32 *data, uint size,
                       trace_namespace_t *namespace)
{
    dbg_assert (buf);
    dbg_assert (name);
    dbg_assert (bare);
    dbg_assert_ptr (data);
    trace_t * const ctx = &trace_global;
    /* Initialise trace buffer. */
    list_init_node (&buf->node);
    buf->chunks_nb = 0;
    buf->drop_level = 0;
    buf->preload = 0;
    buf->locked = true;
    buf->head = buf->tail = NULL;
    buf->bare = bare;
    buf->name = name;
    buf->namespace = namespace;
    /* Initialise bare buffer. */
    bare->data_tail = data;
    bare->data_end = data + size;
    bare->data = data;
    /* Initialise bare buffer content. */
    u32 *p;
    for (p = data; p != data + size; p++)
        *p = TRACE_UNINITIALISED;
    /* Add it to context. */
    list_push (&ctx->buffers, &buf->node);
}

void
trace_buffer_remove (trace_buffer_t *buf)
{
    dbg_assert (buf);
    trace_t * const ctx = &trace_global;
    /* Remove from context. */
    list_remove (&ctx->buffers, &buf->node);
    /* Release chunks. */
    if (buf->head)
        blk_release_desc_range ((blk_t *) buf->head, (blk_t *) buf->tail);
}

trace_buffer_t *
trace_buffer_get (const char *name)
{
    dbg_assert (name);
    trace_t * const ctx = &trace_global;
    /* Search in list. */
    list_node_t *i, *end;
    i = list_begin (&ctx->buffers);
    end = list_end (&ctx->buffers);
    for (; i != end; i = list_next (i))
    {
        trace_buffer_t *buf = PARENT_OF (trace_buffer_t, node, i);
        if (strcmp (buf->name, name) == 0)
            return buf;
    }
    return NULL;
}

static int
trace_buffer_dump_event (char *text, char *text_end,
                         trace_namespace_t *namespace,
                         uint **data, uint *data_end)
{
    dbg_assert (text && text_end && text < text_end);
    dbg_assert (namespace);
    dbg_assert (data && *data && data_end && *data < data_end);
    /* Read event ID. */
    uint id = **data;
    uint args = id & 0xff;
    id >>= 8;
    uint oargs = args;
    int *parg = (int *) *data + 1;
    dbg_assert (parg + args <= (int *) data_end);
    dbg_assert (id < namespace->event_ids_nb);
    const trace_event_id_t *ei = &namespace->event_ids[id];
    dbg_assert (ei->format_string);
    char *p = text;
    /* Print time stamp. */
    if (ei->timestamp)
    {
        dbg_assert (args);
        args--;
        int ret = snprintf (p, text_end - p, "[0x%08x] ", *parg++);
        if (ret >= text_end - p)
            return -1;
        p += ret;
    }
    else
    {
        int ret = snprintf (p, text_end - p, "[.] ");
        if (ret >= text_end - p)
            return -1;
        p += ret;
    }
    /* Decode format string. */
    const char *fp;
    for (fp = ei->format_string; *fp; fp++)
    {
        if (*fp == '%' && *++fp != '%')
        {
            dbg_assert (((*fp >= 'A' && *fp <= 'Z')
                         || (*fp >= 'a' && *fp <= 'z')));
            uint format_size = namespace->formats[*fp - 'A'].size;
            dbg_assert (format_size);
            int ret;
            if (TRACE_DEBUG_ARGS && args < format_size)
            {
                if (p == text_end)
                    return -1;
                *p++ = '?';
                ret = 1;
            }
            else
            {
                dbg_assert (args >= format_size);
                args -= format_size;
                if (format_size == 1)
                {
                    ret = namespace->formats[*fp - 'A'].callback.format_u32 (
                        p, text_end - p, *parg);
                }
                else if (format_size == 2)
                {
                    u64 arg = (u64) parg[1] << 32 | parg[0];
                    ret = namespace->formats[*fp - 'A'].callback.format_u64 (
                        p, text_end - p, arg);
                }
                else
                {
                    ret = namespace->formats[*fp - 'A'].callback.format_table
                        (p, text_end - p, parg, format_size);
                }
                parg += format_size;
            }
            if (ret == -1)
                return -1;
            p += ret;
        }
        else
        {
            /* No room left, cancel the dump of this event. */
            if (p == text_end)
                return -1;
            *p++ = *fp;
        }
    }
    dbg_assert (args == 0);
    /* No room left for the trailing new line, cancel the dump of this
     * event. */
    if (p == text_end)
        return -1;
    *p++ = '\n';
    *data += 1 + oargs;
    return p - text;
}

/**
 * Dump many events from a data buffer.
 * \param  text  text buffer
 * \param  text_size  current text buffer size
 * \param  namespace  name space for trace text dump
 * \param  data  data buffer
 * \param  data_end  end of data buffer
 * \param  cb  callback called several times with text data
 * \param  user  user data passed to the callback
 * \return  number of character given to user callback or -1 on error
 */
static int
trace_buffer_dump_events (char *text, int *text_size,
                          trace_namespace_t *namespace,
                          uint *data, uint *data_end,
                          trace_dump_callback_t cb, void *user)
{
    int sum = 0;
    /* Skip uninitialised data. */
    while (data < data_end && *data == TRACE_UNINITIALISED)
        data++;
    /* Loop on this chunk. */
    while (data < data_end)
    {
        /* Dump one event. */
        int ret = trace_buffer_dump_event (
            text + *text_size, text + TRACE_DUMP_TEXT,
            namespace, &data, data_end);
        /* Add dumped text. */
        if (ret != -1)
            *text_size += ret;
        /* If dump failed or almost no more space, call user callback. */
        if (ret == -1
            || *text_size + TRACE_DUMP_TEXT_SLACK >= TRACE_DUMP_TEXT)
        {
            dbg_assert (*text_size != 0);
            if (cb (user, text, *text_size) != *text_size)
            {
                /* Get out, error. */
                return -1;
            }
            sum += *text_size;
            *text_size = 0;
        }
    }
    return sum;
}

int
trace_buffer_dump (trace_buffer_t *buf, trace_dump_callback_t cb, void *user)
{
    static char text[TRACE_DUMP_TEXT];
    int text_size = 0;
    trace_chunk_t *head, *tail;
    u32 *data, *data_end;
    int sum = 0;
    dbg_assert (buf);
    dbg_assert (buf->name);
    dbg_assert (cb);
    /* Dump buffer name. */
    static const char trace_start_before[] = "---trace-begin[";
    static const char trace_start_after[] = "]---\n";
    static const char trace_stop[] = "---trace-end---\n";
    uint name_len = strlen (buf->name);
    memcpy (text + text_size, trace_start_before,
            sizeof (trace_start_before) - 1);
    text_size += sizeof (trace_start_before) - 1;
    memcpy (text + text_size, buf->name, name_len);
    text_size += name_len;
    memcpy (text + text_size, trace_start_after,
            sizeof (trace_start_after) - 1);
    text_size += sizeof (trace_start_after) - 1;
    dbg_assert ((uint) text_size < sizeof (text) - TRACE_DUMP_TEXT_SLACK);
    /* TODO: acquire lock, increment reference counter. */
    tail = buf->tail;
    head = buf->head;
    if (head)
    {
        /* Loop for each chunks. */
        do
        {
            data = head->data;
            data_end = data + head->commit_index;
            int ret = trace_buffer_dump_events (
                text, &text_size, buf->namespace, data, data_end, cb, user);
            if (ret == -1)
                sum = -1;
            else
                sum += ret;
            if (sum == -1 || head == tail)
                break;
            head = head->next;
        } while (1);
    }
    else
    {
        int ret;
        /* Dump bare buffer. */
        dbg_assert (buf->bare);
        dbg_assert (buf->bare->data <= buf->bare->data_tail
                    && buf->bare->data_tail < buf->bare->data_end);
        /* First part, after tail. */
        ret = trace_buffer_dump_events (
            text, &text_size, buf->namespace, buf->bare->data_tail,
            buf->bare->data_end, cb, user);
        if (ret == -1)
            sum = -1;
        else
        {
            sum += ret;
            /* Second part, before tail. */
            ret = trace_buffer_dump_events (
                text, &text_size, buf->namespace, buf->bare->data,
                buf->bare->data_tail, cb, user);
            if (ret == -1)
                sum = -1;
            else
                sum += ret;
        }
    }
    /* Final text, if sum is -1, there was a user callback error. */
    if (sum != -1)
    {
        dbg_assert (sizeof (trace_stop) - 1 < TRACE_DUMP_TEXT_SLACK);
        memcpy (text + text_size, trace_stop, sizeof (trace_stop) - 1);
        text_size += sizeof (trace_stop) - 1;
        if (cb (user, text, text_size) != text_size)
            sum = -1;
        else
            sum += text_size;
    }
    return sum;
}

int
trace_bundle_start (const char *name, trace_dump_callback_t cb, void *user)
{
    int name_len = 0;
    dbg_assert (cb);
    static const char bundle_start_before[] = "---trace-bundle-begin[";
    static const char bundle_start_after[] = "]---\n";
    if (cb (user, bundle_start_before, sizeof (bundle_start_before) - 1)
        != sizeof (bundle_start_before) - 1)
        return -1;
    if (name)
    {
        name_len = strlen (name);
        if (cb (user, name, name_len) != name_len)
            return -1;
    }
    if (cb (user, bundle_start_after, sizeof (bundle_start_after) - 1)
        != sizeof (bundle_start_after) - 1)
        return -1;
    return sizeof (bundle_start_before) - 1 + sizeof (bundle_start_after) - 1
        + name_len;
}

int
trace_bundle_stop (trace_dump_callback_t cb, void *user)
{
    dbg_assert (cb);
    static const char bundle_stop[] = "---trace-bundle-end---\n";
    if (cb (user, bundle_stop, sizeof (bundle_stop) - 1)
        != sizeof (bundle_stop) - 1)
        return -1;
    return sizeof (bundle_stop) - 1;
}

int
trace_bundle_dump_all (const char *name, trace_dump_callback_t cb, void *user)
{
    trace_t * const ctx = &trace_global;
    int sum = 0, r;
    /* Header. */
    r = trace_bundle_start (name, cb, user);
    if (r == -1) return -1;
    sum += r;
    /* Dump all. */
    list_node_t *i, *end;
    i = list_begin (&ctx->buffers);
    end = list_end (&ctx->buffers);
    for (; i != end; i = list_next (i))
    {
        trace_buffer_t *buf = PARENT_OF (trace_buffer_t, node, i);
        r = trace_buffer_dump (buf, cb, user);
        if (r == -1) return -1;
        sum += r;
    }
    /* Footer. */
    r = trace_bundle_stop (cb, user);
    if (r == -1) return -1;
    sum += r;
    return sum;
}

static int
trace_buffer_dbg_dump_callback (void *user, const char *text, uint text_size)
{
    dbg_assert (text && text_size);
    arch_io_write (text, text_size);
    return text_size;
}

void
trace_buffer_dbg_dump (trace_buffer_t *buf)
{
    trace_buffer_dump (buf, trace_buffer_dbg_dump_callback, NULL);
}

void
trace_dbg_dump_all (void)
{
    trace_bundle_dump_all ("dbg", trace_buffer_dbg_dump_callback, NULL);
}

static void
trace_printn_prepare (trace_buffer_t *buf, uint count)
{
    dbg_assert (buf && buf->tail);
    if (CONFIG_TRACE_ALL_FAST)
        return;
    trace_chunk_t *tail = buf->tail;
    if (tail->tail_index + count > TRACE_CHUNK_SIZE)
    {
        /* No room left, allocate a new chunk. */
        dbg_assert (!buf->locked);
        trace_chunk_t *c = (trace_chunk_t *) blk_alloc_desc ();
        c->next = tail->next;
        c->tail_index = 0;
        c->commit_index = 0;
        tail->next = c;
        REORDER_BARRIER ();
        buf->tail = c;
        REORDER_BARRIER ();
        arch_atomic_add ((int *) &buf->chunks_nb, 1);
        REORDER_BARRIER ();
    }
}

void
trace_print0 (trace_buffer_t *buf, uint id)
{
    trace_printn_prepare (buf, 1);
    trace_fast_print0 (buf, id);
}

void
trace_print1 (trace_buffer_t *buf, uint id, int arg0)
{
    trace_printn_prepare (buf, 2);
    trace_fast_print1 (buf, id, arg0);
}

void
trace_print2 (trace_buffer_t *buf, uint id, int arg0, int arg1)
{
    trace_printn_prepare (buf, 3);
    trace_fast_print2 (buf, id, arg0, arg1);
}

void
trace_print3 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2)
{
    trace_printn_prepare (buf, 4);
    trace_fast_print3 (buf, id, arg0, arg1, arg2);
}

void
trace_print4 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
              int arg3)
{
    trace_printn_prepare (buf, 5);
    trace_fast_print4 (buf, id, arg0, arg1, arg2, arg3);
}

void
trace_print5 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
              int arg3, int arg4)
{
    trace_printn_prepare (buf, 6);
    trace_fast_print5 (buf, id, arg0, arg1, arg2, arg3, arg4);
}

void
trace_print6 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
              int arg3, int arg4, int arg5)
{
    trace_printn_prepare (buf, 7);
    trace_fast_print6 (buf, id, arg0, arg1, arg2, arg3, arg4, arg5);
}

void
trace_print7 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
              int arg3, int arg4, int arg5, int arg6)
{
    trace_printn_prepare (buf, 8);
    trace_fast_print7 (buf, id, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
}

void
trace_print8 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
              int arg3, int arg4, int arg5, int arg6, int arg7)
{
    trace_printn_prepare (buf, 9);
    trace_fast_print8 (buf, id, arg0, arg1, arg2, arg3, arg4, arg5, arg6,
                       arg7);
}

static inline u32 *
trace_fast_printn_prepare (trace_buffer_t *buf, uint count)
{
    /* Fast may become slow on request. */
    if (CONFIG_TRACE_ALL_SLOW)
        trace_printn_prepare (buf, count);
    /* "Fast" means no allocation. */
    if (DEBUG_MORE)
        dbg_assert (buf && buf->tail);
    trace_chunk_t *tail = buf->tail;
    u32 *data_end;
    if (tail->tail_index + count > TRACE_CHUNK_SIZE)
    {
        /* No room left, use the oldest chunk for the new chunk. */
        if (DEBUG_MORE)
            dbg_assert (buf->head && buf->locked);
        buf->head = buf->head->next;
        tail = tail->next;
        buf->tail = tail;
        data_end = tail->data;
        tail->tail_index = count;
        tail->commit_index = 0;
    }
    else
    {
        data_end = tail->data + tail->tail_index;
        tail->tail_index += count;
    }
    /* Dump to buffer. */
    return data_end;
}

static inline void
trace_fast_printn_commit (trace_buffer_t *buf)
{
    if (DEBUG_MORE)
        dbg_assert (buf && buf->tail);
    buf->tail->commit_index = buf->tail->tail_index;
}

void ARCH_ILRAM
trace_fast_print0 (trace_buffer_t *buf, uint id)
{
    u32 *p = trace_fast_printn_prepare (buf, 1);
    *p++ = id << 8 | 0;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print1 (trace_buffer_t *buf, uint id, int arg0)
{
    u32 *p = trace_fast_printn_prepare (buf, 2);
    *p++ = id << 8 | 1;
    *p++ = arg0;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print2 (trace_buffer_t *buf, uint id, int arg0, int arg1)
{
    u32 *p = trace_fast_printn_prepare (buf, 3);
    *p++ = id << 8 | 2;
    *p++ = arg0;
    *p++ = arg1;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print3 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2)
{
    u32 *p = trace_fast_printn_prepare (buf, 4);
    *p++ = id << 8 | 3;
    *p++ = arg0;
    *p++ = arg1;
    *p++ = arg2;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print4 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
                   int arg3)
{
    u32 *p = trace_fast_printn_prepare (buf, 5);
    *p++ = id << 8 | 4;
    *p++ = arg0;
    *p++ = arg1;
    *p++ = arg2;
    *p++ = arg3;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print5 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
                   int arg3, int arg4)
{
    u32 *p = trace_fast_printn_prepare (buf, 6);
    *p++ = id << 8 | 5;
    *p++ = arg0;
    *p++ = arg1;
    *p++ = arg2;
    *p++ = arg3;
    *p++ = arg4;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print6 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
                   int arg3, int arg4, int arg5)
{
    u32 *p = trace_fast_printn_prepare (buf, 7);
    *p++ = id << 8 | 6;
    *p++ = arg0;
    *p++ = arg1;
    *p++ = arg2;
    *p++ = arg3;
    *p++ = arg4;
    *p++ = arg5;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print7 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
                   int arg3, int arg4, int arg5, int arg6)
{
    u32 *p = trace_fast_printn_prepare (buf, 8);
    *p++ = id << 8 | 7;
    *p++ = arg0;
    *p++ = arg1;
    *p++ = arg2;
    *p++ = arg3;
    *p++ = arg4;
    *p++ = arg5;
    *p++ = arg6;
    trace_fast_printn_commit (buf);
}

void ARCH_ILRAM
trace_fast_print8 (trace_buffer_t *buf, uint id, int arg0, int arg1, int arg2,
                   int arg3, int arg4, int arg5, int arg6, int arg7)
{
    u32 *p = trace_fast_printn_prepare (buf, 9);
    *p++ = id << 8 | 8;
    *p++ = arg0;
    *p++ = arg1;
    *p++ = arg2;
    *p++ = arg3;
    *p++ = arg4;
    *p++ = arg5;
    *p++ = arg6;
    *p++ = arg7;
    trace_fast_printn_commit (buf);
}