summaryrefslogtreecommitdiffstats
path: root/dexcomparator/src/com/android/jack/comparator/DebugInfo.java
blob: 0aaf7072845fb6926ab602056c05435d83fe5af5 (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.comparator;

import com.android.jack.dx.dex.file.DebugInfoDecoder;
import com.android.jack.dx.dex.file.DebugInfoDecoder.LocalEntry;
import com.android.jack.dx.dex.file.DebugInfoDecoder.PositionEntry;
import com.android.jack.dx.io.Code;
import com.android.jack.dx.io.DexBuffer;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Debug information for one method.
 */
public class DebugInfo {

  /**
   * The scope of a {@link LocalVar}.
   */
  public static class Interval {
    private int start;
    private int end;
    private int pendingCloseCount;

    public Interval(int start) {
      this.start = start;
      this.end = start;
      pendingCloseCount = 1;
    }

    public Interval(int start, int end) {
      this.start = start;
      this.end = end;
    }

    /**
     * @return the end
     */
    public int getEnd() {
      return end;
    }

    /**
     * @return the start
     */
    public int getStart() {
      return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(int start) {
      augment(start);
      pendingCloseCount++;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(int end) {
      augment(end);
      pendingCloseCount--;
      assert pendingCloseCount >= 0;
    }

    @Override
    public String toString() {
      return "[" + start + ", " + end + "]";
    }

    @Override
    public boolean equals(Object obj) {
      if (obj instanceof Interval) {
        Interval other = (Interval) obj;
        return start == other.start && end == other.end;
      }
      return false;
    }

    @Override
    public int hashCode() {
      return 7529 * start + 11113 * end;
    }

    /**
     * Increase this interval so that it includes the new limit.
     */
    private void augment(int limit) {
      if (this.end < limit) {
        this.end = limit;
      }
      if (this.start > limit) {
        this.start = limit;
      }
    }

    private boolean isClosed() {
      return pendingCloseCount == 0;
    }
  }

  /**
   * Debugging information for a local variable.
   */
  public static class LocalVar {
    @Nonnull
    private final String name;
    @CheckForNull
    private final String typeSignature;

    @Nonnull
    private final Interval scope;

    @Nonnull
    private static final char NON_SOURCE_CONFLICTING_CHAR = '-';

    private LocalVar(@Nonnull String name, @CheckForNull String typeSignature, int start) {
      this.name = name;
      this.typeSignature = typeSignature;
      scope = new Interval(start);
    }

    @Nonnull
    public String getName() {
      return name;
    }

    @CheckForNull
    public String getTypeSignature() {
      return typeSignature;
    }

    @Nonnull
    public Interval getScope() {
      return scope;
    }

    public boolean isSynthetic() {
      return name.indexOf(NON_SOURCE_CONFLICTING_CHAR) != -1;
    }

    @Nonnull
    @Override
    public String toString() {
      return typeSignature + " " + name + " " + scope;
    }

    void addInterval(int start) {
      scope.setStart(start);
    }

    void closeInterval(int end) {
      scope.setEnd(end);
    }
  }

  private static final int NO_INDEX = -1;

  private List<PositionEntry> lines;
  private final HashMap<String, LocalVar> locals = new HashMap<String, DebugInfo.LocalVar>();
  private DexBuffer dex;
  private int sizeInBytes;
  private int debugInfoOffset;

  public DebugInfo(DebugInfoDecoder decoder, DexBuffer dex, Code codeItem, int sizeInbytes) {
    this.dex = dex;
    this.sizeInBytes = sizeInbytes;
    debugInfoOffset = codeItem.getDebugInfoOffset();
    lines = decoder.getPositionList();
    Collections.sort(lines, new Comparator<PositionEntry>() {
      @Override
      public int compare(PositionEntry o1, PositionEntry o2) {
        return o2.address > o1.address ? -1 : (o2.address == o1.address ? 0 : 1);
      }});

    LocalVar[] regs = new LocalVar[codeItem.getRegistersSize()];

    for (LocalEntry localEntry : decoder.getLocals()) {

        int reg = localEntry.reg;
        int line = getLine(localEntry.address - 1);
        if (localEntry.isStart && regs[reg] != null) {
            regs[reg].closeInterval(line);
            regs[reg] = null;
        }

        if (localEntry.nameIndex != NO_INDEX) {

          LocalVar localVar = getOrCreate(localEntry);

          if (localEntry.isStart) {
            localVar.addInterval(line);
            regs[reg] = localVar;
          } else {
            localVar.closeInterval(line);
            regs[reg] = null;
          }
        }
    }

    int endLine = -1;
    for (PositionEntry line : lines) {
      endLine = Math.max(line.line, endLine);
    }
    for (LocalVar localVar : locals.values()) {
        if (!localVar.getScope().isClosed()) {
            localVar.closeInterval(endLine);
        }
    }
  }

  private LocalVar getOrCreate(LocalEntry localEntry) {
    String name = dex.strings().get(localEntry.nameIndex);
    String desc = null;
    if (localEntry.signatureIndex != NO_INDEX) {
      desc = dex.strings().get(localEntry.signatureIndex);
    } else if (localEntry.typeIndex != NO_INDEX) {
      desc = dex.typeNames().get(localEntry.typeIndex);
    }

    String key = name + desc;
    LocalVar local = locals.get(key);
    if (local == null) {
      assert localEntry.isStart;
      local = new LocalVar(name, desc, getLine(localEntry.address));
      locals.put(key, local);
    }
    return local;
  }

  public int getLine(int address) {
    int line = -1;
    for (PositionEntry entry : lines) {
      if (entry.address > address) {
        if (line == -1) {
          line = entry.line;
        }
        break;
      } else {
        line = entry.line;
      }
    }
    return line;
  }

  public Collection<LocalVar> getLocals() {
    return locals.values();
  }

  public LocalVar getLocal(LocalVar other) {
    return getLocal(other.getName(), other.getTypeSignature());
  }

  public LocalVar getLocal(String name, String desc) {
    return locals.get(name + desc);
  }

  public int getSizeInBytes() {
    return sizeInBytes;
  }

  public int getDebugInfoOffset() {
    return debugInfoOffset;
  }
}