File: frame1.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 (734 lines) | stat: -rw-r--r-- 22,638 bytes parent folder | download | duplicates (2)
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
/*
Copyright (c) 2009-2018 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:

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

    (2) 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.

    (3)The name of the author may not be used to endorse
    or promote products derived from this software without
    specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.

*/
/*  simplereader.c
    This is an example of code reading dwarf .debug_frame.
    It is kept as simple as possible to expose essential features.
    It does not do all possible error reporting or error handling.

    It specifically calls dwarf_expand_frame_instructions()
    to verify that works without crashing!

    To use, try
        make
        ./frame1 frame1

    gcc/clang may produce .eh_frame without .debug_frame.
    To read .eh_frame call dwarf_get_fde_list_eh()
    below instead of dwarf_get_fde_list() .
*/
#include "config.h"

/* Windows specific header files */
#if defined(_WIN32) && defined(HAVE_STDAFX_H)
#include "stdafx.h"
#endif /* HAVE_STDAFX_H */


#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h> /* For open() */
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>  /* For open() */
#endif /* HAVE_SYS_STAT_H */
#include <fcntl.h>     /* For open() */
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* for exit(), C89 malloc */
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_MALLOC_H
/* Useful include for some Windows compilers. */
#include <malloc.h>
#endif /* HAVE_MALLOC_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>     /* For close() */
#elif defined(_WIN32) && defined(_MSC_VER)
#include <io.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h> /* For uintptr_t */
#endif /* HAVE_STDINT_H */
#ifdef _O_BINARY
/*  This is for a Windows environment */
#define O_BINARY _O_BINARY
#else
# ifndef O_BINARY
# define O_BINARY 0  /* So it does nothing in Linux/Unix */
# endif
#endif /* O_BINARY */
#include <string.h>     /* For strcmp* */
#include <stdio.h>
#include <errno.h>
#include "dwarf.h"
#include "libdwarf.h"


static void read_frame_data(Dwarf_Debug dbg,const char *sec);
static void print_fde_instrs(Dwarf_Debug dbg, Dwarf_Fde fde,
    Dwarf_Error *error);
static void print_regtable(Dwarf_Regtable3 *tab3);
static void print_cie_instrs(Dwarf_Cie cie,Dwarf_Error *error);
static void print_fde_selected_regs( Dwarf_Fde fde);
static void print_reg(int r);

static int just_print_selected_regs = 0;

/*  Depending on the ABI we set INITIAL_VAL
    differently.  For ia64 initial value is
    UNDEF_VAL, for MIPS and others initial
    value is SAME_VAL.
    Here we'll set it UNDEF_VAL
    as that way we'll see when first set. */
#define UNDEF_VAL 2000
#define SAME_VAL 2001
#define CFA_VAL 2002
#define INITIAL_VAL UNDEF_VAL

/*  Don't really want getopt here. yet.
    so hand do options. */

int
main(int argc, char **argv)
{

    Dwarf_Debug dbg = 0;
    int fd = -1;
    const char *filepath = "<stdin>";
    int res = DW_DLV_ERROR;
    Dwarf_Error error;
    Dwarf_Handler errhand = 0;
    Dwarf_Ptr errarg = 0;
    int regtabrulecount = 0;
    int curopt = 0;

    for(curopt = 1;curopt < argc; ++curopt) {
        if (strncmp(argv[curopt],"--",2)) {
            break;
        }
        if (!strcmp(argv[curopt],"--just-print-selected-regs")) {
            just_print_selected_regs++;
        }
    }

    if(curopt == argc ) {
        fd = 0; /* stdin */
    } else if (curopt == (argc-1)) {
        filepath = argv[curopt];
        fd = open(filepath,O_RDONLY|O_BINARY);
    } else {
        printf("Too many args, giving up. \n");
        exit(1);
    }
    if(fd < 0) {
        printf("Failure attempting to open %s\n",filepath);
    }
    res = dwarf_init_b(fd,DW_DLC_READ,DW_GROUPNUMBER_ANY,
        errhand,errarg, &dbg,&error);
    if(res != DW_DLV_OK) {
        printf("Giving up, dwarf_init failed, "
            "cannot do DWARF processing\n");
        if (res == DW_DLV_ERROR) {
            printf("Error code %s\n",dwarf_errmsg(error));
        }
        exit(1);
    }
    /*  Do this setting after init before any real operations.
        These return the old values, but here we do not
        need to know the old values.  The sizes and
        values here are higher than most ABIs and entirely
        arbitrary.

        The setting of initial_value to
        the same as undefined-value (the other possible choice being
        same-value) is arbitrary, different ABIs do differ, and
        you have to know which is right.

        In dwarfdump we get the SAME_VAL, UNDEF_VAL,
        INITIAL_VAL CFA_VAL from dwconf_s struct.   */
    regtabrulecount=1999;
    dwarf_set_frame_undefined_value(dbg, UNDEF_VAL);
    dwarf_set_frame_rule_initial_value(dbg, INITIAL_VAL);
    dwarf_set_frame_same_value(dbg,SAME_VAL);
    dwarf_set_frame_cfa_value(dbg,CFA_VAL);
    dwarf_set_frame_rule_table_size(dbg,regtabrulecount);

    read_frame_data(dbg,".debug_frame");
    read_frame_data(dbg,".eh_frame");
    res = dwarf_finish(dbg,&error);
    if(res != DW_DLV_OK) {
        printf("dwarf_finish failed!\n");
    }
    close(fd);
    return 0;
}

static void
read_frame_data(Dwarf_Debug dbg,const char *sect)
{
    Dwarf_Error error;
    Dwarf_Signed cie_element_count = 0;
    Dwarf_Signed fde_element_count = 0;
    Dwarf_Cie *cie_data = 0;
    Dwarf_Fde *fde_data = 0;
    int res = DW_DLV_ERROR;
    Dwarf_Signed fdenum = 0;


    /*  If you wish to read .eh_frame data, use
        dwarf_get_fde_list_eh() instead.
        Get debug_frame with dwarf_get_fde_list. */
    printf(" Print %s\n",sect);
    if (!strcmp(sect,".eh_frame"))
    {
        res = dwarf_get_fde_list_eh(dbg,&cie_data,&cie_element_count,
            &fde_data,&fde_element_count,&error);
    } else {
        res = dwarf_get_fde_list(dbg,&cie_data,&cie_element_count,
            &fde_data,&fde_element_count,&error);
    }
    if(res == DW_DLV_NO_ENTRY) {
        printf("No %s data present\n",sect);
        return;
    }
    if( res == DW_DLV_ERROR) {
        printf("Error reading frame data ");
        exit(1);
    }
    printf( "%" DW_PR_DSd " cies present. "
        "%" DW_PR_DSd " fdes present. \n",
        cie_element_count,fde_element_count);
    /*if(fdenum >= fde_element_count) {
        printf("Want fde %d but only %" DW_PR_DSd " present\n",fdenum,
            fde_element_count);
        exit(1);
    }*/

    for(fdenum = 0; fdenum < fde_element_count; ++fdenum) {
        Dwarf_Cie cie = 0;

        res = dwarf_get_cie_of_fde(fde_data[fdenum],&cie,&error);
        if(res != DW_DLV_OK) {
            printf("Error accessing cie of fdenum %" DW_PR_DSd
                " to get its cie\n",fdenum);
            exit(1);
        }
        printf("Print cie of fde %" DW_PR_DSd  "\n",fdenum);
        print_cie_instrs(cie,&error);
        printf("Print fde %" DW_PR_DSd  "\n",fdenum);
        if (just_print_selected_regs) {
            print_fde_selected_regs(fde_data[fdenum]);
        } else {
            print_fde_instrs(dbg,fde_data[fdenum],&error);
        }
    }

    /* Done with the data. */
    dwarf_fde_cie_list_dealloc(dbg,cie_data,cie_element_count,
        fde_data, fde_element_count);
    return;
}




/* Simply shows the instructions at hand for this fde. */
static void
print_cie_instrs(Dwarf_Cie cie,Dwarf_Error *error)
{
    int res = DW_DLV_ERROR;
    Dwarf_Unsigned bytes_in_cie = 0;
    Dwarf_Small version = 0;
    char *augmentation = 0;
    Dwarf_Unsigned code_alignment_factor = 0;
    Dwarf_Signed data_alignment_factor = 0;
    Dwarf_Half return_address_register_rule = 0;
    Dwarf_Ptr instrp = 0;
    Dwarf_Unsigned instr_len = 0;

    res = dwarf_get_cie_info(cie,&bytes_in_cie,
        &version, &augmentation, &code_alignment_factor,
        &data_alignment_factor, &return_address_register_rule,
        &instrp,&instr_len,error);
    if(res != DW_DLV_OK) {
        printf("Unable to get cie info!\n");
        exit(1);
    }
}


/*  This was written with DWARF2 Dwarf_Frame_Op!
    FIXME need dwarf_expand_frame_instructions_b() */
static void
print_frame_instrs(Dwarf_Frame_Op *frame_op_array,
  Dwarf_Signed frame_op_count)
{
    Dwarf_Signed i = 0;
    printf("Base op. Ext op. Reg. Offset. Instr-offset.\n");
    for(i = 0; i < frame_op_count; ++i) {
        Dwarf_Frame_Op *fo = frame_op_array +i;

        printf("[%2" DW_PR_DSd "]", i);
        printf(" %2d. ", fo->fp_base_op);
        printf(" 0x%02x. ", fo->fp_extended_op);
        printf(" %3d. ", fo->fp_register);

        /*  DWARF3 and later:
            for DW_CFA_def_cfa_expression,
            DW_CFA_val_expression, and
            DW_CFA_expression the fp_offset is a pointer
            into runtime memory. Printing it is not helpful
            in a diff.  */
        if (fo->fp_extended_op == DW_CFA_def_cfa ||
            fo->fp_extended_op == DW_CFA_val_expression ||
            fo->fp_extended_op == DW_CFA_def_cfa_expression ) {
            /* The offset is really a pointer. */
            printf(" (pointer-value) ");
        } else {
            /* The offset is a real offset /value of some sort. */
            printf(" %6" DW_PR_DSd ". ", fo->fp_offset);
        }
        printf(" 0x%08" DW_PR_DUx ". ", fo->fp_instr_offset);
        printf("\n");
    }
}



static void
print_fde_col(Dwarf_Signed k,
    Dwarf_Addr   jsave,
    Dwarf_Small  value_type,
    Dwarf_Signed offset_relevant,
    Dwarf_Signed reg_used,
    Dwarf_Signed offset_or_block_len,
    Dwarf_Ptr    block_ptr,
    Dwarf_Addr   row_pc,
    Dwarf_Bool   has_more_rows,
    Dwarf_Addr   subsequent_pc)
{
    char *type_title = "";
    Dwarf_Unsigned rule_id = k;

    printf(" pc=0x%" DW_PR_DUx ,jsave);
    if (row_pc != jsave) {
        printf(" row_pc=0x%" DW_PR_DUx ,row_pc);
    }
    printf(" col=%" DW_PR_DSd " ",k);
    switch(value_type) {
    case DW_EXPR_OFFSET:
        type_title = "off";
        goto preg2;
    case DW_EXPR_VAL_OFFSET:
        type_title = "valoff";

        preg2:
        printf("<%s ", type_title);
        if (reg_used == SAME_VAL) {
            printf(" SAME_VAL");
            break;
        } else if (reg_used == INITIAL_VAL) {
            printf(" INITIAL_VAL");
            break;
        }
        print_reg(rule_id);

        printf("=");
        if (offset_relevant == 0) {
            print_reg(reg_used);
            printf(" ");
        } else {
            printf("%02" DW_PR_DSd , offset_or_block_len);
            printf("(");
            print_reg(reg_used);
            printf(") ");
        }
        break;
    case DW_EXPR_EXPRESSION:
        type_title = "expr";
        goto pexp2;
    case DW_EXPR_VAL_EXPRESSION:
        type_title = "valexpr";

        pexp2:
        printf("<%s ", type_title);
        print_reg(rule_id);
        printf("=");
        printf("expr-block-len=%" DW_PR_DSd , offset_or_block_len);
        printf(" block-ptr=%" DW_PR_DUx,
            (Dwarf_Unsigned)(uintptr_t)block_ptr);
#if 0
        {
            char pref[40];

            strcpy(pref, "<");
            strcat(pref, type_title);
            strcat(pref, "bytes:");
            /*  The data being dumped comes direct from
                libdwarf so libdwarf validated it. */
            dump_block(pref, block_ptr, offset_or_block_len);
            printf("%s", "> ");
            if (glflags.verbose) {
                struct esb_s exprstring;
                esb_constructor(&exprstring);
                get_string_from_locs(dbg,
                    block_ptr,offset,addr_size,
                    offset_size,version,&exprstring);
                printf("<expr:%s>",esb_get_string(&exprstring));
                esb_destructor(&exprstring);
            }
        }
#endif
        break;
    default:
        printf("Internal error in libdwarf, value type %d\n",
            value_type);
        exit(1);
    }
    printf(" more=%d",has_more_rows);
    printf(" next=0x%" DW_PR_DUx,subsequent_pc);
    printf("%s", "> ");
    printf("\n");
}


/*  In dwarfdump we use
    dwarf_get_fde_info_for_cfa_reg3_b() to get subsequent pc
    and avoid incrementing pc by for the next cfa,
    using has_more_rows and subsequent_pc passed back.

    Here, to verify function added in May 2018,
    we instead use dwarf_get_fde_info_for_reg3_b()
    which has the has_more_rows and subsequent_pc functions
    for the case where one is tracking a particular register
    and not closely watching the CFA value itself. */

static void
print_fde_selected_regs( Dwarf_Fde fde)
{
    Dwarf_Error oneferr = 0;
    /* Arbitrary column numbers for testing. */
    static int selected_cols[] = {1,3,5};
    static int selected_cols_count =
        sizeof(selected_cols)/sizeof(selected_cols[0]);
    Dwarf_Signed k = 0;
    int fres = 0;

    Dwarf_Addr low_pc = 0;
    Dwarf_Unsigned func_length = 0;
    Dwarf_Ptr fde_bytes = NULL;
    Dwarf_Unsigned fde_bytes_length = 0;
    Dwarf_Off cie_offset = 0;
    Dwarf_Signed cie_index = 0;
    Dwarf_Off fde_offset = 0;
    Dwarf_Fde curfde = fde;
    Dwarf_Cie cie = 0;
    Dwarf_Addr jsave = 0;
    Dwarf_Addr high_addr = 0;
    Dwarf_Addr next_jsave = 0;
    Dwarf_Bool has_more_rows = 0;
    Dwarf_Addr subsequent_pc = 0;
    Dwarf_Error error = 0;
    int res = 0;

    fres = dwarf_get_fde_range(curfde,
        &low_pc, &func_length,
        &fde_bytes,
        &fde_bytes_length,
        &cie_offset, &cie_index,
        &fde_offset, &oneferr);

    if (fres == DW_DLV_ERROR) {
        printf("FAIL: dwarf_get_fde_range err %" DW_PR_DUu
            " line %d\n",
            dwarf_errno(oneferr),__LINE__);
        exit(1);
    }
    if (fres == DW_DLV_NO_ENTRY) {
        printf("No fde range data available\n");
        return;
    }
    res = dwarf_get_cie_of_fde(fde,&cie,&error);
    if(res != DW_DLV_OK) {
        printf("Error getting cie from fde\n");
        exit(1);
    }

    high_addr = low_pc + func_length;
    /*  Could check has_more_rows here instead of high_addr,
        If we initialized has_more_rows to 1 above. */
    for(jsave = low_pc ; next_jsave < high_addr; jsave = next_jsave) {
        next_jsave = jsave+1;
        printf("\n");
        for(k = 0; k < selected_cols_count ; ++k ) {
            Dwarf_Signed reg = 0;
            Dwarf_Signed offset_relevant = 0;
            int fires = 0;
            Dwarf_Small value_type = 0;
            Dwarf_Ptr block_ptr = 0;
            Dwarf_Signed offset_or_block_len = 0;
            Dwarf_Addr row_pc = 0;

            fires = dwarf_get_fde_info_for_reg3_b(curfde,
                selected_cols[k],
                jsave,
                &value_type,
                &offset_relevant,
                &reg,
                &offset_or_block_len,
                &block_ptr,
                &row_pc,
                &has_more_rows,
                &subsequent_pc,
                &oneferr);
            if (fires == DW_DLV_ERROR) {
                printf("FAIL: reading reg err %" DW_PR_DUu " line %d",
                    dwarf_errno(oneferr),__LINE__);
                exit(1);
            }
            if (fires == DW_DLV_NO_ENTRY) {
                continue;
            }
            print_fde_col(
                selected_cols[k],jsave,
                value_type,offset_relevant,
                reg,offset_or_block_len,block_ptr,row_pc,
                has_more_rows, subsequent_pc);
            if (has_more_rows) {
                next_jsave = subsequent_pc;
            } else {
                next_jsave = high_addr;
            }
        }
    }
}


/* Just prints the instructions in the fde. */
static void
print_fde_instrs(Dwarf_Debug dbg,
    Dwarf_Fde fde, Dwarf_Error *error)
{
    int res;
    Dwarf_Addr lowpc = 0;
    Dwarf_Unsigned func_length = 0;
    Dwarf_Ptr fde_bytes;
    Dwarf_Unsigned fde_byte_length = 0;
    Dwarf_Off cie_offset = 0;
    Dwarf_Signed cie_index = 0;
    Dwarf_Off fde_offset = 0;
    Dwarf_Addr arbitrary_addr = 0;
    Dwarf_Addr actual_pc = 0;
    Dwarf_Regtable3 tab3;
    int oldrulecount = 0;
    Dwarf_Ptr outinstrs = 0;
    Dwarf_Unsigned instrslen = 0;
    Dwarf_Frame_Op * frame_op_array = 0;
    Dwarf_Signed frame_op_count = 0;
    Dwarf_Cie cie = 0;



    res = dwarf_get_fde_range(fde,&lowpc,&func_length,&fde_bytes,
        &fde_byte_length,&cie_offset,&cie_index,&fde_offset,error);
    if(res != DW_DLV_OK) {
        printf("Problem getting fde range \n");
        exit(1);
    }

    arbitrary_addr = lowpc + (func_length/2);
    printf("function low pc 0x%" DW_PR_DUx
        "  and length 0x%" DW_PR_DUx
        "  and midpoint addr we choose 0x%" DW_PR_DUx
        "\n",
        lowpc,func_length,arbitrary_addr);

    /*  1 is arbitrary. We are winding up getting the
        rule count here while leaving things unchanged. */
    oldrulecount = dwarf_set_frame_rule_table_size(dbg,1);
    dwarf_set_frame_rule_table_size(dbg,oldrulecount);

    tab3.rt3_reg_table_size = oldrulecount;
    tab3.rt3_rules = (struct Dwarf_Regtable_Entry3_s *) malloc(
        sizeof(struct Dwarf_Regtable_Entry3_s)* oldrulecount);
    if (!tab3.rt3_rules) {
        printf("Unable to malloc for %d rules\n",oldrulecount);
        exit(1);
    }

    res = dwarf_get_fde_info_for_all_regs3(fde,arbitrary_addr ,
        &tab3,&actual_pc,error);
    printf("function actual addr of row 0x%" DW_PR_DUx "\n",
        actual_pc);

    if(res != DW_DLV_OK) {
        printf("dwarf_get_fde_info_for_all_regs3 failed!\n");
        exit(1);
    }
    print_regtable(&tab3);

    res = dwarf_get_fde_instr_bytes(fde,&outinstrs,&instrslen,error);
    if(res != DW_DLV_OK) {
        printf("dwarf_get_fde_instr_bytes failed!\n");
        exit(1);
    }
    res = dwarf_get_cie_of_fde(fde,&cie,error);
    if(res != DW_DLV_OK) {
        printf("Error getting cie from fde\n");
        exit(1);
    }

    res = dwarf_expand_frame_instructions(cie,
        outinstrs,instrslen,&frame_op_array,
        &frame_op_count,error);
    if(res != DW_DLV_OK) {
        printf("dwarf_expand_frame_instructions failed!\n");
        exit(1);
    }
    printf("Frame op count: %" DW_PR_DUu "\n",frame_op_count);
    print_frame_instrs(frame_op_array,frame_op_count);

    dwarf_dealloc(dbg,frame_op_array, DW_DLA_FRAME_BLOCK);
    free(tab3.rt3_rules);
}

static void
print_reg(int r)
{
   switch(r) {
   case SAME_VAL:
        printf(" %d SAME_VAL ",r);
        break;
   case UNDEF_VAL:
        printf(" %d UNDEF_VAL ",r);
        break;
   case CFA_VAL:
        printf(" %d (CFA) ",r);
        break;
   default:
        printf(" r%d ",r);
        break;
   }
}

static void
print_one_regentry(const char *prefix,
    struct Dwarf_Regtable_Entry3_s *entry)
{
    int is_cfa = !strcmp("cfa",prefix);
    printf("%s ",prefix);
    printf("type: %d %s ",
        entry->dw_value_type,
        (entry->dw_value_type == DW_EXPR_OFFSET)? "DW_EXPR_OFFSET":
        (entry->dw_value_type == DW_EXPR_VAL_OFFSET)?
            "DW_EXPR_VAL_OFFSET":
        (entry->dw_value_type == DW_EXPR_EXPRESSION)?
            "DW_EXPR_EXPRESSION":
        (entry->dw_value_type == DW_EXPR_VAL_EXPRESSION)?
            "DW_EXPR_VAL_EXPRESSION":
            "Unknown");
    switch(entry->dw_value_type) {
    case DW_EXPR_OFFSET:
        print_reg(entry->dw_regnum);
        printf(" offset_rel? %d ",entry->dw_offset_relevant);
        if(entry->dw_offset_relevant) {
            printf(" offset  %" DW_PR_DSd " " ,
                entry->dw_offset_or_block_len);
            if(is_cfa) {
                printf("defines cfa value");
            } else {
                printf("address of value is CFA plus signed offset");
            }
            if(!is_cfa  && entry->dw_regnum != CFA_VAL) {
                printf(" compiler botch, regnum != CFA_VAL");
            }
        } else {
            printf("value in register");
        }
        break;
    case DW_EXPR_VAL_OFFSET:
        print_reg(entry->dw_regnum);
        printf(" offset  %" DW_PR_DSd " " ,
            entry->dw_offset_or_block_len);
        if(is_cfa) {
            printf("does this make sense? No?");
        } else {
            printf("value at CFA plus signed offset");
        }
        if(!is_cfa  && entry->dw_regnum != CFA_VAL) {
            printf(" compiler botch, regnum != CFA_VAL");
        }
        break;
    case DW_EXPR_EXPRESSION:
        print_reg(entry->dw_regnum);
        printf(" offset_rel? %d ",entry->dw_offset_relevant);
        printf(" offset  %" DW_PR_DSd " " ,
            entry->dw_offset_or_block_len);
        printf("Block ptr set? %s ",entry->dw_block_ptr?"yes":"no");
        printf(" Value is at address given by expr val ");
        /* printf(" block-ptr  0x%" DW_PR_DUx " ",
            (Dwarf_Unsigned)entry->dw_block_ptr); */
        break;
    case DW_EXPR_VAL_EXPRESSION:
        printf(" expression byte len  %" DW_PR_DSd " " ,
            entry->dw_offset_or_block_len);
        printf("Block ptr set? %s ",entry->dw_block_ptr?"yes":"no");
        printf(" Value is expr val ");
        if(!entry->dw_block_ptr) {
            printf("Compiler botch. ");
        }
        /* printf(" block-ptr  0x%" DW_PR_DUx " ",
            (Dwarf_Unsigned)entry->dw_block_ptr); */
        break;
    }
    printf("\n");
}

static void
print_regtable(Dwarf_Regtable3 *tab3)
{
    int r;
    /* We won't print too much. A bit arbitrary. */
    int max = 10;
    if(max > tab3->rt3_reg_table_size) {
        max = tab3->rt3_reg_table_size;
    }
    print_one_regentry("cfa",&tab3->rt3_cfa_rule);

    for(r = 0; r < max; r++) {
        char rn[30];
        snprintf(rn,sizeof(rn),"reg %d",r);
        print_one_regentry(rn,tab3->rt3_rules+r);
    }


}