Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e7c455e21353f68cf740757577978f3b246951a2 (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
/*******************************************************************************
 * Copyright (c) 2011, 2015 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.model;

import java.math.BigInteger;
import java.util.Map;

import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.tcf.core.ErrorReport;
import org.eclipse.tcf.internal.debug.model.TCFSymFileRef;
import org.eclipse.tcf.internal.debug.ui.ColorCache;
import org.eclipse.tcf.internal.debug.ui.ImageCache;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeExecContext.MemoryRegion;
import org.eclipse.tcf.protocol.JSON;
import org.eclipse.tcf.services.IMemoryMap;
import org.eclipse.tcf.util.TCFDataCache;

/**
 * A node representing a memory region (module).
 */
public class TCFNodeModule extends TCFNode implements IDetailsProvider {

    private final TCFData<MemoryRegion> region;
    private int sort_pos;

    protected TCFNodeModule(final TCFNodeExecContext parent, String id, final int index) {
        super(parent, id);
        region = new TCFData<MemoryRegion>(channel) {
            @Override
            protected boolean startDataRetrieval() {
                TCFDataCache<MemoryRegion[]> map_cache = parent.getMemoryMap();
                if (!map_cache.validate(this)) return false;
                Throwable error = map_cache.getError();
                MemoryRegion[] map_data = map_cache.getData();
                MemoryRegion region = null;
                if (map_data != null && index < map_data.length) region = map_data[index];
                set(null, error, region);
                return true;
            }
        };
    }

    public TCFDataCache<MemoryRegion> getRegion() {
        return region;
    }

    void setSortPosition(int sort_pos) {
        this.sort_pos = sort_pos;
    }

    void onMemoryMapChanged() {
        region.reset();
    }

    @Override
    protected boolean getData(ILabelUpdate update, Runnable done) {
        if (!region.validate(done)) return false;
        MemoryRegion mr = region.getData();
        IMemoryMap.MemoryRegion r = mr != null ? mr.region : null;
        if (r == null) {
            update.setLabel("...", 0);
        }
        else {
            String[] col_ids = update.getColumnIds();
            if (col_ids == null) {
                update.setLabel(r.getFileName(), 0);
            }
            else {
                for (int i=0; i < col_ids.length; ++i) {
                    String col_id = col_ids[i];
                    if (TCFColumnPresentationModules.COL_NAME.equals(col_id)) {
                        update.setLabel(r.getFileName(), i);
                    }
                    else if (TCFColumnPresentationModules.COL_ADDRESS.equals(col_id)) {
                        update.setLabel(toHexString(r.getAddress()), i);
                    }
                    else if (TCFColumnPresentationModules.COL_SIZE.equals(col_id)) {
                        update.setLabel(toHexString(r.getSize()), i);
                    }
                    else if (TCFColumnPresentationModules.COL_FLAGS.equals(col_id)) {
                        update.setLabel(getFlagsLabel(r.getFlags()), i);
                    }
                    else if (TCFColumnPresentationModules.COL_OFFSET.equals(col_id)) {
                        update.setLabel(toHexString(r.getOffset()), i);
                    }
                    else if (TCFColumnPresentationModules.COL_SECTION.equals(col_id)) {
                        String sectionName = r.getSectionName();
                        update.setLabel(sectionName != null ? sectionName : "", i);
                    }
                }
            }
        }
        update.setImageDescriptor(ImageCache.getImageDescriptor(ImageCache.IMG_MEMORY_MAP), 0);
        return true;
    }

    @Override
    protected void getFontData(ILabelUpdate update, String view_id) {
        FontData fn = TCFModelFonts.getNormalFontData(view_id);
        String[] cols = update.getColumnIds();
        if (cols == null || cols.length == 0) {
            update.setFontData(fn, 0);
        }
        else {
            String[] ids = update.getColumnIds();
            for (int i = 0; i < cols.length; i++) {
                if (TCFColumnPresentationModules.COL_ADDRESS.equals(ids[i]) ||
                        TCFColumnPresentationModules.COL_OFFSET.equals(ids[i]) ||
                        TCFColumnPresentationModules.COL_SIZE.equals(ids[i])) {
                    update.setFontData(TCFModelFonts.getMonospacedFontData(view_id), i);
                }
                else {
                    update.setFontData(fn, i);
                }
            }
        }
    }

    public boolean getDetailText(StyledStringBuffer bf, Runnable done) {
        if (!region.validate(done)) return false;
        MemoryRegion mr = region.getData();
        IMemoryMap.MemoryRegion r = mr != null ? mr.region : null;
        if (r == null) return true;
        String file_name = r.getFileName();
        if (file_name != null) {
            bf.append("File name: ", SWT.BOLD).append(file_name).append('\n');
            TCFNodeExecContext exe = (TCFNodeExecContext)parent;
            TCFDataCache<TCFSymFileRef> sym_cache = exe.getSymFileInfo(JSON.toBigInteger(r.getAddress()));
            if (sym_cache != null) {
                if (!sym_cache.validate(done)) return false;
                TCFSymFileRef sym_data = sym_cache.getData();
                if (sym_data != null) {
                    if (sym_data.props != null) {
                        String sym_file_name = (String)sym_data.props.get("FileName");
                        if (sym_file_name != null && !sym_file_name.equals(file_name)) {
                            bf.append("Symbol file: ", SWT.BOLD).append(sym_file_name).append('\n');
                        }
                        @SuppressWarnings("unchecked")
                        Map<String,Object> map = (Map<String,Object>)sym_data.props.get("FileError");
                        if (map != null) {
                            String msg = TCFModel.getErrorMessage(new ErrorReport("", map), false);
                            bf.append("Symbol file error: ", SWT.BOLD).append(msg, SWT.ITALIC, null, ColorCache.rgb_error).append('\n');
                        }
                    }
                    if (sym_data.error != null) bf.append("Symbol file error: ", SWT.BOLD).append(
                            TCFModel.getErrorMessage(sym_data.error, false),
                            SWT.ITALIC, null, ColorCache.rgb_error).append('\n');
                }
            }
            String section = r.getSectionName();
            Number offset = r.getOffset();
            if (section != null) bf.append("File section: ", SWT.BOLD).append(section).append('\n');
            if (offset != null) bf.append("File offset: ", SWT.BOLD).append(toHexString(offset), StyledStringBuffer.MONOSPACED).append('\n');
        }
        Number addr = r.getAddress();
        Number size = r.getSize();
        if (addr != null) bf.append("Address: ", SWT.BOLD).append(toHexString(addr), StyledStringBuffer.MONOSPACED).append('\n');
        if (size != null) bf.append("Size: ", SWT.BOLD).append(toHexString(size), StyledStringBuffer.MONOSPACED).append('\n');
        @SuppressWarnings("unchecked")
        Map<String,Object> kernel_module = (Map<String,Object>)r.getProperties().get(IMemoryMap.PROP_KERNEL_MODULE);
        if (kernel_module != null) {
            int cnt = 0;
            Number init = (Number)kernel_module.get("Init");
            Number core = (Number)kernel_module.get("Core");
            Number init_size = (Number)kernel_module.get("InitSize");
            Number core_size = (Number)kernel_module.get("CoreSize");
            bf.append("Kernel module: ", SWT.BOLD);
            if (init != null) {
                bf.append("init addr ", SWT.BOLD).append(toHexString(init), StyledStringBuffer.MONOSPACED);
                cnt++;
            }
            if (init_size != null) {
                if (cnt > 0) bf.append(", ");
                bf.append("init size ", SWT.BOLD).append(toHexString(init_size), StyledStringBuffer.MONOSPACED);
                cnt++;
            }
            if (core != null) {
                if (cnt > 0) bf.append(", ");
                bf.append("core addr ", SWT.BOLD).append(toHexString(core), StyledStringBuffer.MONOSPACED);
                cnt++;
            }
            if (core_size != null) {
                if (cnt > 0) bf.append(", ");
                bf.append("core size ", SWT.BOLD).append(toHexString(core_size), StyledStringBuffer.MONOSPACED);
                cnt++;
            }
            bf.append('\n');
        }
        String query = r.getContextQuery();
        if (query != null) bf.append("Context query: ", SWT.BOLD).append(query).append('\n');
        bf.append("Flags: ", SWT.BOLD).append(getFlagsLabel(r.getFlags())).append('\n');
        return true;
    }

    private String toHexString(Number address) {
        if (address == null) return "";
        BigInteger addr = JSON.toBigInteger(address);
        String s = addr.toString(16);
        int sz = s.length() <= 8 ? 8 : 16;
        int l = sz - s.length();
        if (l < 0) l = 0;
        if (l > 16) l = 16;
        return "0x0000000000000000".substring(0, 2 + l) + s;
    }

    private String getFlagsLabel(int flags) {
        StringBuilder flagsLabel = new StringBuilder(3);
        if ((flags & IMemoryMap.FLAG_READ) != 0) flagsLabel.append('r');
        else flagsLabel.append('-');
        if ((flags & IMemoryMap.FLAG_WRITE) != 0) flagsLabel.append('w');
        else flagsLabel.append('-');
        if ((flags & IMemoryMap.FLAG_EXECUTE) != 0) flagsLabel.append('x');
        else flagsLabel.append('-');
        return flagsLabel.toString();
    }

    @Override
    public int compareTo(TCFNode n) {
        TCFNodeModule e = (TCFNodeModule)n;
        if (sort_pos < e.sort_pos) return -1;
        if (sort_pos > e.sort_pos) return +1;
        return 0;
    }
}

Back to the top