Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be0ab5ca9e21423f24b8e7bf883a9e8dda420a62 (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * Symbols service - common code that is shared between different implementations of the service.
 */

#include <tcf/config.h>

#if SERVICE_Symbols || ENABLE_SymbolsProxy

#include <tcf/framework/myalloc.h>
#include <tcf/framework/exceptions.h>
#include <tcf/services/stacktrace.h>
#include <tcf/services/symbols.h>
#include <tcf/services/vm.h>

static LocationInfo * loc_info = NULL;

static LocationExpressionState * evaluate_symbol_location(const Symbol * sym) {
    Trap trap;
    Context * ctx = NULL;
    int frame = STACK_NO_FRAME;
    StackFrame * frame_info = NULL;
    LocationExpressionState * state = NULL;

    if (get_symbol_frame(sym, &ctx, &frame) < 0) return NULL;
    if (get_location_info(sym, &loc_info) < 0) return NULL;
    if (loc_info->args_cnt > 0) {
        set_errno(ERR_OTHER, "Object location is relative to owner address");
        return NULL;
    }
    if (frame != STACK_NO_FRAME && get_frame_info(ctx, frame, &frame_info) < 0) return NULL;
    if (!set_trap(&trap)) return NULL;
    state = evaluate_location_expression(ctx, frame_info,
        loc_info->value_cmds.cmds, loc_info->value_cmds.cnt, NULL, 0);
    clear_trap(&trap);
    return state;
}

static const char * pieces_err_msg(LocationExpressionState * state) {
    if (state->pieces->optimized_away) return "is optimized away";
    if (state->pieces->reg != NULL) return "is located in a register";
    if (state->pieces->value != NULL) return "is a constant value";
    return "is a bit field";
}

int get_symbol_address(const Symbol * sym, ContextAddress * address) {
    LocationExpressionState * state = evaluate_symbol_location(sym);
    if (state == NULL) return -1;
    if (state->pieces_cnt == 1 &&
            state->pieces->implicit_pointer == 0 && state->pieces->optimized_away == 0 &&
            state->pieces->reg == NULL && state->pieces->value == NULL && state->pieces->bit_offs == 0) {
        *address = state->pieces->addr;
        return 0;
    }
    if (state->pieces_cnt > 0) {
        set_fmt_errno(ERR_OTHER, "Cannot get object address: the object %s", pieces_err_msg(state));
        return -1;
    }
    if (state->stk_pos == 1) {
        *address = (ContextAddress)state->stk[0];
        return 0;
    }
    set_errno(ERR_OTHER, "Object does not have memory address");
    return -1;
}

int get_symbol_offset(const Symbol * sym, ContextAddress * offset) {
    LocationInfo * loc_info = NULL;
    if (get_location_info(sym, &loc_info) < 0) return -1;
    if (loc_info->args_cnt == 1) {
        /* Relative location. Only static offset can be returned.
         * Dynamic offset can only be computed in an expression. */
        if (loc_info->value_cmds.cnt == 3 &&
                loc_info->value_cmds.cmds[0].cmd == SFT_CMD_ARG &&
                loc_info->value_cmds.cmds[1].cmd == SFT_CMD_NUMBER &&
                loc_info->value_cmds.cmds[2].cmd == SFT_CMD_ADD) {
            *offset = (ContextAddress)loc_info->value_cmds.cmds[1].args.num;
            return 0;
        }
    }
    set_errno(ERR_OTHER, "Object does not have member offset");
    return -1;
}

int get_symbol_register(const Symbol * sym, Context ** ctx, int * frame, RegisterDefinition ** reg) {
    LocationExpressionState * state = evaluate_symbol_location(sym);
    if (state == NULL) return -1;
    if (state->pieces_cnt == 1 && state->pieces->reg != NULL && state->pieces->reg->size == state->pieces->size) {
        if (get_symbol_frame(sym, ctx, frame) < 0) return -1;
        *reg = state->pieces->reg;
        return 0;
    }
    set_errno(ERR_OTHER, "Symbol is not located in a register");
    return -1;
}

int get_symbol_value(const Symbol * sym, void ** value, size_t * size, int * big_endian) {
    Trap trap;
    LocationExpressionState * state = evaluate_symbol_location(sym);
    if (state == NULL) return -1;
    if (!set_trap(&trap)) return -1;
    if (state->pieces_cnt > 0) {
        read_location_pieces(state->ctx, state->stack_frame,
            state->pieces, state->pieces_cnt, loc_info->big_endian, value, size);
    }
    else {
        ContextAddress sym_size = 0;
        size_t buf_size = 0;
        void * buf = NULL;
        if (state->stk_pos != 1) str_exception(ERR_OTHER, "Invalid location expression");
        if (get_symbol_size(sym, &sym_size) < 0) exception(errno);
        buf_size = (size_t)sym_size;
        buf = tmp_alloc(buf_size);
        if (context_read_mem(state->ctx, (ContextAddress)state->stk[0], buf, buf_size) < 0) exception(errno);
        *value = buf;
        *size = buf_size;
    }
    *big_endian = loc_info->big_endian;
    clear_trap(&trap);
    return 0;
}

#endif

Back to the top