crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/containers/Thread.java
changeset 16 72f198be1c1d
parent 4 615035072f7e
equal deleted inserted replaced
15:0367d2db2c06 16:72f198be1c1d
    65 	private final String threadStackPointer;
    65 	private final String threadStackPointer;
    66 	private final String threadLinkRegister;
    66 	private final String threadLinkRegister;
    67 	private final List<Stack> threadStacks;
    67 	private final List<Stack> threadStacks;
    68 	private final List<RegisterSet> threadRegisters;
    68 	private final List<RegisterSet> threadRegisters;
    69 
    69 
    70 	private Thread(int id, String fullName, String exitType, String exitCategory, 
    70 	private Thread(final int id, final String fullName, final String exitType, final String exitCategory, 
    71 					String panicDescription, String programCounter, String stackPointer, String linkRegister, 
    71 					final String panicDescription, final String programCounter, final String stackPointer, final String linkRegister, 
    72 					String exitReason, String exitDescription, List<Stack> stacks, List<RegisterSet> registers) {
    72 					final String exitReason, final String exitDescription, final List<Stack> stacks, final List<RegisterSet> registers) {
    73 		threadId = id;
    73 		threadId = id;
    74 		threadFullName = fullName;
    74 		threadFullName = fullName;
    75 		threadExitType = exitType;
    75 		threadExitType = exitType;
    76 		threadExitCategory = exitCategory;
    76 		threadExitCategory = exitCategory;
    77 		threadExitReason = exitReason;
    77 		threadExitReason = exitReason;
   137 	 * @param out
   137 	 * @param out
   138 	 * @param stackItems
   138 	 * @param stackItems
   139 	 * @param html
   139 	 * @param html
   140 	 * @throws IOException
   140 	 * @throws IOException
   141 	 */
   141 	 */
   142 	public void writeTo(BufferedWriter out, StackItems stackItems, boolean html) throws IOException {
   142 	public void writeTo(final BufferedWriter out, final StackItems stackItems, final boolean html) throws IOException {
   143 		writeLine(out,"");
   143 		writeLine(out,"");
   144 		writeLine(out, "THREAD:");
   144 		writeLine(out, "THREAD:");
   145 		writeLine(out, "--------");
   145 		writeLine(out, "--------");
   146 		writeLine(out, "Thread Name", threadFullName);
   146 		writeLine(out, "Thread Name", threadFullName);
   147 		writeLine(out, "Exit Type", threadExitType);
   147 		writeLine(out, "Exit Type", threadExitType);
   154 		writeLine(out, "Exit Description", threadExitDescription);
   154 		writeLine(out, "Exit Description", threadExitDescription);
   155 		
   155 		
   156 		writeLine(out, "");
   156 		writeLine(out, "");
   157 		if (threadRegisters != null && !threadRegisters.isEmpty()) {
   157 		if (threadRegisters != null && !threadRegisters.isEmpty()) {
   158 			for (int i = 0; i < threadRegisters.size(); i++) {
   158 			for (int i = 0; i < threadRegisters.size(); i++) {
   159 				RegisterSet registerSet = threadRegisters.get(i);
   159 				final RegisterSet registerSet = threadRegisters.get(i);
   160 				registerSet.writeTo(out);
   160 				registerSet.writeTo(out);
   161 				writeLine(out, "");
   161 				writeLine(out, "");
   162 			}
   162 			}
   163 		}
   163 		}
   164 		
   164 		
   165 		if (threadStacks != null && !threadStacks.isEmpty()) {
   165 		if (threadStacks != null && !threadStacks.isEmpty()) {
   166 			for (int i = 0; i < threadStacks.size(); i++) {
   166 			for (int i = 0; i < threadStacks.size(); i++) {
   167 				Stack stack = threadStacks.get(i);
   167 				final Stack stack = threadStacks.get(i);
   168 				stack.writeTo(out, stackItems, html);
   168 				stack.writeTo(out, stackItems, html);
   169 			}
   169 			}
   170 		}
   170 		}
   171 	}
   171 	}
   172 	
   172 	
   173 	void writeLine(BufferedWriter out, String line) throws IOException {
   173 	void writeLine(final BufferedWriter out, final String line) throws IOException {
   174 		out.write(line);
   174 		out.write(line);
   175 		out.newLine();
   175 		out.newLine();
   176 	}
   176 	}
   177 	
   177 	
   178 	void writeLine(BufferedWriter out, String header, String value) throws IOException {
   178 	void writeLine(final BufferedWriter out, final String header, final String value) throws IOException {
   179 		if (!"".equals(value)) {
   179 		if (!"".equals(value)) {
   180 			out.write(String.format(FORMAT, header, value));
   180 			out.write(String.format(FORMAT, header, value));
   181 			out.newLine();
   181 			out.newLine();
   182 		}
   182 		}
   183 	}
   183 	}
   189 	 * @param symbols
   189 	 * @param symbols
   190 	 * @param stacks
   190 	 * @param stacks
   191 	 * @param errorLibrary
   191 	 * @param errorLibrary
   192 	 * @return created thread or null
   192 	 * @return created thread or null
   193 	 */
   193 	 */
   194 	public static Thread read(Element elementThread,
   194 	public static Thread read(final Element elementThread,
   195 								Map<Integer, RegisterSet> registers,
   195 								final Map<Integer, RegisterSet> registers,
   196 								Map<Integer, Symbol> symbols,
   196 								final Map<Integer, Symbol> symbols,
   197 								Map<Integer, Stack> stacks,
   197 								final Map<Integer, Stack> stacks,
   198 								ErrorLibrary errorLibrary) {
   198 								final ErrorLibrary errorLibrary) {
   199 		try {
   199 		try {
   200 			// read thread id
   200 			// read thread id
   201 			String threadId = XmlUtils.getTextValue(elementThread, TAG_ID);
   201 			final String threadId = XmlUtils.getTextValue(elementThread, TAG_ID);
   202 			if (threadId == null)
   202 			if (threadId == null)
   203 				return null;
   203 				return null;
   204 			
   204 			
   205 			// convert thread id to integer
   205 			// convert thread id to integer
   206 			int id;
   206 			int id;
   209 			} catch (Exception e) {
   209 			} catch (Exception e) {
   210 				return null;
   210 				return null;
   211 			}
   211 			}
   212 			
   212 			
   213 			// read the threads full name
   213 			// read the threads full name
   214 			String fullName = XmlUtils.getTextValue(elementThread, TAG_FULLNAME);
   214 			final String fullName = XmlUtils.getTextValue(elementThread, TAG_FULLNAME);
   215 			if (fullName == null)
   215 			if (fullName == null)
   216 				return null;
   216 				return null;
   217 			
   217 			
   218 			String exitType = "";
   218 			String exitType = "";
   219 			String exitCategory = "";
   219 			String exitCategory = "";
   220 			String exitReason = "";
   220 			String exitReason = "";
   221 			String exitDescription = "";
   221 			String exitDescription = "";
   222 			// get child nodes such as exit_info, stacks, registers
   222 			// get child nodes such as exit_info, stacks, registers
   223 			NodeList childNodes = elementThread.getChildNodes();
   223 			final NodeList childNodes = elementThread.getChildNodes();
   224 			if (childNodes == null || childNodes.getLength() < 1)
   224 			if (childNodes == null || childNodes.getLength() < 1)
   225 				return null;
   225 				return null;
   226 			
   226 			
   227 			// read Exit info
   227 			// read Exit info
   228 			NodeList exitInfo = elementThread.getElementsByTagName(TAG_EXIT_INFO);
   228 			final NodeList exitInfo = elementThread.getElementsByTagName(TAG_EXIT_INFO);
   229 			if (exitInfo != null && exitInfo.getLength() > 0) {
   229 			if (exitInfo != null && exitInfo.getLength() > 0) {
   230 				NodeList exitInfos = exitInfo.item(0).getChildNodes();
   230 				final NodeList exitInfos = exitInfo.item(0).getChildNodes();
   231 				if (exitInfos != null && exitInfos.getLength() > 0) {
   231 				if (exitInfos != null && exitInfos.getLength() > 0) {
   232 					for (int i = 0; i < exitInfos.getLength(); i++) {
   232 					for (int i = 0; i < exitInfos.getLength(); i++) {
   233 						Node el = exitInfos.item(i);
   233 						final Node el = exitInfos.item(i);
   234 						Node firstChild = null;
   234 						Node firstChild = null;
   235 						if (TAG_EXIT_TYPE.equals(el.getNodeName())) {
   235 						if (TAG_EXIT_TYPE.equals(el.getNodeName())) {
   236 							// read exit type (Exception, Panic, Kill, Terminate)
   236 							// read exit type (Exception, Panic, Kill, Terminate)
   237 							firstChild = el.getFirstChild();
   237 							firstChild = el.getFirstChild();
   238 							if (firstChild != null) {
   238 							if (firstChild != null) {
   287 			String panicDescription = "";
   287 			String panicDescription = "";
   288 			if (!"".equals(exitCategory) && !"".equals(exitReason)) {
   288 			if (!"".equals(exitCategory) && !"".equals(exitReason)) {
   289 				panicDescription = errorLibrary.getPanicDescription(exitCategory, exitReason);
   289 				panicDescription = errorLibrary.getPanicDescription(exitCategory, exitReason);
   290 			}
   290 			}
   291 			
   291 			
   292 			List<Stack> threadStacks = new ArrayList<Stack>();
   292 			final List<Stack> threadStacks = new ArrayList<Stack>();
   293 			List<RegisterSet> threadRegisters = new ArrayList<RegisterSet>();
   293 			final List<RegisterSet> threadRegisters = new ArrayList<RegisterSet>();
   294 			String programCounter = "";
   294 			String programCounter = "";
   295 			String stackPointer = "";
   295 			String stackPointer = "";
   296 			String linkRegister = "";
   296 			String linkRegister = "";
   297 			
   297 			
   298 			// see if register has a symbol and/or message
   298 			// see if register has a symbol and/or message
   299 			NodeList nl = elementThread.getElementsByTagName(TAG_LINK);
   299 			final NodeList nl = elementThread.getElementsByTagName(TAG_LINK);
   300 			if (nl != null && nl.getLength() > 0) {
   300 			if (nl != null && nl.getLength() > 0) {
   301 				for (int i = 0; i < nl.getLength(); i++) {
   301 				for (int i = 0; i < nl.getLength(); i++) {
   302 					Node linkNode = nl.item(i);
   302 					final Node linkNode = nl.item(i);
   303 					String nodeValue = XmlUtils.getNodeValue(linkNode);
   303 					final String nodeValue = XmlUtils.getNodeValue(linkNode);
   304 					NamedNodeMap attributes = linkNode.getAttributes();
   304 					final NamedNodeMap attributes = linkNode.getAttributes();
   305 					if (attributes != null && attributes.getLength() > 0) {
   305 					if (attributes != null && attributes.getLength() > 0) {
   306 						Node seg = attributes.getNamedItem(ATTRIBUTE_SEG);
   306 						final Node seg = attributes.getNamedItem(ATTRIBUTE_SEG);
   307 						// stack id
   307 						// stack id
   308 						if (SEGMENT_STACKS.equals(XmlUtils.getNodeValue(seg))) {
   308 						if (SEGMENT_STACKS.equals(XmlUtils.getNodeValue(seg))) {
   309 							int sId = Integer.parseInt(nodeValue);
   309 							final int sId = Integer.parseInt(nodeValue);
   310 							if (stacks.containsKey(sId)) {
   310 							if (stacks.containsKey(sId)) {
   311 								Stack s = stacks.get(sId);
   311 								final Stack s = stacks.get(sId);
   312 								threadStacks.add(s);
   312 								threadStacks.add(s);
   313 								// the most interesting PC, SP and LR comes from
   313 								// the most interesting PC, SP and LR comes from
   314 								// that stack which contains CPSR.
   314 								// that stack which contains CPSR.
   315 								if (s.stackRegisterContainsCpsr()) {
   315 								if (s.stackRegisterContainsCpsr()) {
   316 									programCounter = s.getProgramCounter();
   316 									programCounter = s.getProgramCounter();
   318 									linkRegister = s.getLinkRegister();
   318 									linkRegister = s.getLinkRegister();
   319 								}
   319 								}
   320 							}
   320 							}
   321 						// register id
   321 						// register id
   322 						} else if (SEGMENT_REGISTERS.equals(XmlUtils.getNodeValue(seg))) {
   322 						} else if (SEGMENT_REGISTERS.equals(XmlUtils.getNodeValue(seg))) {
   323 							int rId = Integer.parseInt(nodeValue);
   323 							final int rId = Integer.parseInt(nodeValue);
   324 							// if passed registers list contains a register for this id
   324 							// if passed registers list contains a register for this id
   325 							if (registers.containsKey(rId)) {
   325 							if (registers.containsKey(rId)) {
   326 								RegisterSet registerSet = registers.get(rId);
   326 								final RegisterSet registerSet = registers.get(rId);
   327 								threadRegisters.add(registerSet);								
   327 								threadRegisters.add(registerSet);								
   328 							}							
   328 							}							
   329 						}
   329 						}
   330 					}
   330 					}
   331 				}
   331 				}
   338 		} catch (Exception e) {
   338 		} catch (Exception e) {
   339 			return null;
   339 			return null;
   340 		}
   340 		}
   341 	}
   341 	}
   342 	
   342 	
   343 	public Map<Integer, Stack> removeOwnStacks(Map<Integer, Stack> stacks) {
   343 	public Map<Integer, Stack> removeOwnStacks(final Map<Integer, Stack> stacks) {
   344 		
   344 		
   345 		if (threadStacks != null && !threadStacks.isEmpty()) {
   345 		if (threadStacks != null && !threadStacks.isEmpty()) {
   346 			for (int i = 0; i < threadStacks.size(); i++) {
   346 			for (int i = 0; i < threadStacks.size(); i++) {
   347 				if (stacks.containsKey(threadStacks.get(i).getId())) {
   347 				if (stacks.containsKey(threadStacks.get(i).getId())) {
   348 					stacks.remove(threadStacks.get(i).getId());
   348 					stacks.remove(threadStacks.get(i).getId());
   351 		}
   351 		}
   352 		
   352 		
   353 		return stacks;
   353 		return stacks;
   354 	}
   354 	}
   355 	
   355 	
   356 	public Map<Integer, RegisterSet> removeOwnRegisterSets(Map<Integer, RegisterSet> registerSets) {
   356 	public Map<Integer, RegisterSet> removeOwnRegisterSets(final Map<Integer, RegisterSet> registerSets) {
   357 		
   357 		
   358 		if (threadRegisters != null && !threadRegisters.isEmpty()) {
   358 		if (threadRegisters != null && !threadRegisters.isEmpty()) {
   359 			for (int i = 0; i < threadRegisters.size(); i++) {
   359 			for (int i = 0; i < threadRegisters.size(); i++) {
   360 				if (registerSets.containsKey(threadRegisters.get(i).getId())) {
   360 				if (registerSets.containsKey(threadRegisters.get(i).getId())) {
   361 					registerSets.remove(threadRegisters.get(i).getId());
   361 					registerSets.remove(threadRegisters.get(i).getId());