b532915856b4a4463ac213e4df532ec6bc3ccd7d
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / CTFCallsite.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.ctf.core.event;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.equalsNullable;
17
18 /**
19 * Callsite information to help with cdt integration
20 *
21 * @author Matthew Khouzam
22 */
23 public class CTFCallsite implements Comparable<CTFCallsite> {
24
25 // Integer size
26 private static final int INT_BITS = Integer.SIZE;
27
28 // a bitmask for the size of an integer
29 private static final long INT_MASK = (1L << INT_BITS) - 1;
30
31 /**
32 * The event name
33 */
34 private final String fEventName;
35
36 /**
37 * the file name of the callsite
38 */
39 private final String fFileName;
40
41 /**
42 * the instruction pointer
43 */
44 private final long fIp;
45
46 /**
47 * the function name
48 */
49 private final String fFunctionName;
50
51 /**
52 * the line number of the callsite
53 */
54 private final long fLineNumber;
55
56 /**
57 * The callsite constructor
58 *
59 * @param en
60 * The event name
61 * @param func
62 * the function name
63 * @param ip
64 * the instruction pointer of the callsite
65 * @param fn
66 * the file name of the callsite
67 * @param line
68 * the line number of the callsite
69 */
70 public CTFCallsite(String en, String func, long ip, String fn, long line) {
71 fEventName = en;
72 fFileName = fn;
73 fFunctionName = func;
74 fIp = ip;
75 fLineNumber = line;
76 }
77
78 /**
79 * @return the eventName
80 */
81 public String getEventName() {
82 return fEventName;
83 }
84
85 /**
86 * @return the fileName
87 */
88 public String getFileName() {
89 return fFileName;
90 }
91
92 /**
93 * @return the ip
94 */
95 public long getIp() {
96 return fIp;
97 }
98
99 /**
100 * @return the functionName
101 */
102 public String getFunctionName() {
103 return fFunctionName;
104 }
105
106 /**
107 * @return the lineNumber
108 */
109 public long getLineNumber() {
110 return fLineNumber;
111 }
112
113 /*
114 * The callsites will be sorted by calling addresses. To do this we take IPs
115 * (instruction pointers) and compare them. Java only supports signed
116 * operation and since memory addresses are unsigned, we will convert the
117 * longs into integers that contain the high and low bytes and compare them.
118 */
119 @Override
120 public int compareTo(CTFCallsite o) {
121 /*
122 * mask32 is 32 zeros followed by 32 ones, when we bitwise and this it
123 * will return the lower 32 bits
124 */
125
126 long other = o.fIp;
127 /*
128 * To get a high int: we downshift by 32 and bitwise and with the mask
129 * to get rid of the sign
130 *
131 * To get the low int: we bitwise and with the mask.
132 */
133 long otherHigh = (other >> INT_BITS) & INT_MASK;
134 long otherLow = other & INT_MASK;
135 long ownHigh = (fIp >> INT_BITS) & INT_MASK;
136 long ownLow = fIp & INT_MASK;
137 /* are the high values different, if so ignore the lower values */
138 if (ownHigh > otherHigh) {
139 return 1;
140 }
141 if (ownHigh < otherHigh) {
142 return -1;
143 }
144 /* the high values are the same, compare the lower values */
145 if (ownLow > otherLow) {
146 return 1;
147 }
148 if (ownLow < otherLow) {
149 return -1;
150 }
151 /* the values are identical */
152 return 0;
153 }
154
155 @Override
156 public int hashCode() {
157 final int prime = 31;
158 int result = 1;
159 result = prime * result + ((fEventName == null) ? 0 : fEventName.hashCode());
160 result = prime * result + ((fFileName == null) ? 0 : fFileName.hashCode());
161 result = prime * result + ((fFunctionName == null) ? 0 : fFunctionName.hashCode());
162 result = prime * result + (int) (fIp ^ (fIp >>> 32));
163 result = prime * result + (int) (fLineNumber ^ (fLineNumber >>> 32));
164 return result;
165 }
166
167 @Override
168 public boolean equals(Object obj) {
169 if (this == obj) {
170 return true;
171 }
172 if (obj == null) {
173 return false;
174 }
175 if (getClass() != obj.getClass()) {
176 return false;
177 }
178 CTFCallsite other = (CTFCallsite) obj;
179 if (!equalsNullable(fEventName, other.fEventName)) {
180 return false;
181 }
182 if (!equalsNullable(fFileName, other.fFileName)) {
183 return false;
184 }
185 if (!equalsNullable(fFunctionName, other.fFunctionName)) {
186 return false;
187 }
188 if (fIp != other.fIp) {
189 return false;
190 }
191 if (fLineNumber != other.fLineNumber) {
192 return false;
193 }
194 return true;
195 }
196
197 @Override
198 public String toString() {
199 return fFileName + "/" + fFunctionName + ":" + fLineNumber; //$NON-NLS-1$ //$NON-NLS-2$
200 }
201 }
This page took 0.035705 seconds and 4 git commands to generate.