debuggercdi/com.nokia.cdt.debug.cw.symbian.tests/src/com/nokia/cdt/debug/cw/symbian/tests/DwarfReaderTest.java
author cawthron
Sat, 25 Apr 2009 12:54:10 -0500
changeset 127 c937102a5510
parent 0 fb279309251b
parent 9 6ef327765a4e
permissions -rw-r--r--
merge heads from Austin sync
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     1
/*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     3
* All rights reserved.
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     4
* This component and the accompanying materials are made available
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     6
* which accompanies this distribution, and is available
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     8
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     9
* Initial Contributors:
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    10
* Nokia Corporation - initial contribution.
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    11
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    12
* Contributors:
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    13
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    14
* Description: 
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    15
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    16
*/
2
cawthron
parents:
diff changeset
    17
package com.nokia.cdt.debug.cw.symbian.tests;
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
import java.io.File;
cawthron
parents:
diff changeset
    20
import java.io.FileOutputStream;
cawthron
parents:
diff changeset
    21
import java.io.IOException;
cawthron
parents:
diff changeset
    22
import java.io.PrintStream;
cawthron
parents:
diff changeset
    23
import java.util.ArrayList;
cawthron
parents:
diff changeset
    24
import java.util.Arrays;
cawthron
parents:
diff changeset
    25
cawthron
parents:
diff changeset
    26
import junit.framework.TestCase;
cawthron
parents:
diff changeset
    27
cawthron
parents:
diff changeset
    28
import org.eclipse.cdt.utils.debug.dwarf.DwarfReader;
cawthron
parents:
diff changeset
    29
cawthron
parents:
diff changeset
    30
public class DwarfReaderTest extends TestCase {
cawthron
parents:
diff changeset
    31
cawthron
parents:
diff changeset
    32
	private String m_outputFileName = "c:\\temp\\DwarfReaderOutput.txt";
cawthron
parents:
diff changeset
    33
cawthron
parents:
diff changeset
    34
	// The Elf file to parse. 
cawthron
parents:
diff changeset
    35
	//  
cawthron
parents:
diff changeset
    36
	private String[]  m_dataFileNames; 
cawthron
parents:
diff changeset
    37
		
cawthron
parents:
diff changeset
    38
	private PrintStream	m_outputFileStream = null;
cawthron
parents:
diff changeset
    39
	
cawthron
parents:
diff changeset
    40
	protected void setUp() throws Exception {
cawthron
parents:
diff changeset
    41
		super.setUp();
cawthron
parents:
diff changeset
    42
cawthron
parents:
diff changeset
    43
		m_dataFileNames = getDataFileNames();
cawthron
parents:
diff changeset
    44
		
cawthron
parents:
diff changeset
    45
		if (m_outputFileStream == null) {
cawthron
parents:
diff changeset
    46
			File outputFile = new File(m_outputFileName);
cawthron
parents:
diff changeset
    47
			if (!outputFile.exists())
cawthron
parents:
diff changeset
    48
				outputFile.createNewFile();
cawthron
parents:
diff changeset
    49
cawthron
parents:
diff changeset
    50
			m_outputFileStream = new PrintStream(new FileOutputStream(
cawthron
parents:
diff changeset
    51
					outputFile, false /* append? */));
cawthron
parents:
diff changeset
    52
cawthron
parents:
diff changeset
    53
			m_outputFileStream.println("Output of test on CDT DwarfReader");
cawthron
parents:
diff changeset
    54
			m_outputFileStream.println("==========================================");
cawthron
parents:
diff changeset
    55
		}
cawthron
parents:
diff changeset
    56
	}
cawthron
parents:
diff changeset
    57
cawthron
parents:
diff changeset
    58
	protected void tearDown() throws Exception {
cawthron
parents:
diff changeset
    59
		super.tearDown();
cawthron
parents:
diff changeset
    60
		
cawthron
parents:
diff changeset
    61
		m_outputFileStream.close();
cawthron
parents:
diff changeset
    62
	}
cawthron
parents:
diff changeset
    63
cawthron
parents:
diff changeset
    64
	public final void testGetSourceFiles() {
cawthron
parents:
diff changeset
    65
		for (String fname: m_dataFileNames) {
cawthron
parents:
diff changeset
    66
			File f = new File(fname);
cawthron
parents:
diff changeset
    67
			if (f == null || !f.exists())
cawthron
parents:
diff changeset
    68
				fail("Data file not found: " + f.getAbsolutePath());
cawthron
parents:
diff changeset
    69
cawthron
parents:
diff changeset
    70
			// Currently this just check if the parsing process succeeds,
cawthron
parents:
diff changeset
    71
			// not check if the output is correct.
cawthron
parents:
diff changeset
    72
			// Look at the output file designated by "m_outputFileName"
cawthron
parents:
diff changeset
    73
			// and see if it's correct.
cawthron
parents:
diff changeset
    74
			getSourceFilesForBinary(fname);
cawthron
parents:
diff changeset
    75
		}
cawthron
parents:
diff changeset
    76
	}
cawthron
parents:
diff changeset
    77
cawthron
parents:
diff changeset
    78
	// Get data files to test
cawthron
parents:
diff changeset
    79
	//
cawthron
parents:
diff changeset
    80
	private String[] getDataFileNames() throws Exception {
cawthron
parents:
diff changeset
    81
		String[] fnames = null;
cawthron
parents:
diff changeset
    82
		
cawthron
parents:
diff changeset
    83
		ArrayList<String> list = new ArrayList<String>(10);
cawthron
parents:
diff changeset
    84
cawthron
parents:
diff changeset
    85
		// To add individual data files...
cawthron
parents:
diff changeset
    86
		//
cawthron
parents:
diff changeset
    87
		/*
cawthron
parents:
diff changeset
    88
		list.add("M:\\epoc32\\RELEASE\\armv5\\udeb\\agenda.sym");
cawthron
parents:
diff changeset
    89
		list.add(TestsPlugin.getPluginPath()+"\\data\\one2.sym");
cawthron
parents:
diff changeset
    90
		list.add(TestsPlugin.getPluginPath()+"\\data\\_h2_dma.sym");
cawthron
parents:
diff changeset
    91
		*/
cawthron
parents:
diff changeset
    92
		
cawthron
parents:
diff changeset
    93
		// To add files under one folder...
cawthron
parents:
diff changeset
    94
		//
cawthron
parents:
diff changeset
    95
		// All all .sym files under data folder in the project.
cawthron
parents:
diff changeset
    96
		list.addAll(Arrays.asList(TestsPlugin.getFileFullNamesInFolder(
cawthron
parents:
diff changeset
    97
				TestsPlugin.getPluginPath()+"\\data\\", ".sym")));
cawthron
parents:
diff changeset
    98
		/*
cawthron
parents:
diff changeset
    99
		list.addAll(Arrays.asList(TestsPlugin.getFileFullNamesInFolder(
cawthron
parents:
diff changeset
   100
				"M:\\epoc32\\RELEASE\\armv5\\udeb\\", ".sym")));
cawthron
parents:
diff changeset
   101
		list.addAll(Arrays.asList(TestsPlugin.getFileFullNamesInFolder(
cawthron
parents:
diff changeset
   102
				"M:\\epoc32\\RELEASE\\gcce\\udeb\\", ".sym")));
cawthron
parents:
diff changeset
   103
		*/
cawthron
parents:
diff changeset
   104
		
cawthron
parents:
diff changeset
   105
		fnames = new String[list.size()];
cawthron
parents:
diff changeset
   106
		list.toArray(fnames);
cawthron
parents:
diff changeset
   107
		
cawthron
parents:
diff changeset
   108
		return fnames;
cawthron
parents:
diff changeset
   109
	}
cawthron
parents:
diff changeset
   110
	
cawthron
parents:
diff changeset
   111
	private void getSourceFilesForBinary(String binaryFileName) {
cawthron
parents:
diff changeset
   112
		DwarfReader reader = null;
cawthron
parents:
diff changeset
   113
		
cawthron
parents:
diff changeset
   114
		try {
cawthron
parents:
diff changeset
   115
			reader = new DwarfReader(binaryFileName);
cawthron
parents:
diff changeset
   116
		} catch (IOException e) {
cawthron
parents:
diff changeset
   117
			e.printStackTrace();
cawthron
parents:
diff changeset
   118
			fail("File \"" + binaryFileName + "\" is not valid Elf file.");
cawthron
parents:
diff changeset
   119
			return;
cawthron
parents:
diff changeset
   120
		}
cawthron
parents:
diff changeset
   121
cawthron
parents:
diff changeset
   122
		m_outputFileStream.println("\n----------------------------------------------------");
cawthron
parents:
diff changeset
   123
		m_outputFileStream.println("Data file to check: ");
cawthron
parents:
diff changeset
   124
		m_outputFileStream.println("\t" + binaryFileName);
cawthron
parents:
diff changeset
   125
		m_outputFileStream.println("----------------------------------------------------");
cawthron
parents:
diff changeset
   126
		
cawthron
parents:
diff changeset
   127
		String[] files = reader.getSourceFiles();
cawthron
parents:
diff changeset
   128
		
cawthron
parents:
diff changeset
   129
		m_outputFileStream.print("Number of source files: ");
cawthron
parents:
diff changeset
   130
		m_outputFileStream.println(files.length);
cawthron
parents:
diff changeset
   131
		m_outputFileStream.println();
cawthron
parents:
diff changeset
   132
cawthron
parents:
diff changeset
   133
		// Sort the file lists.
cawthron
parents:
diff changeset
   134
		Arrays.sort(files);
cawthron
parents:
diff changeset
   135
		
cawthron
parents:
diff changeset
   136
		for (String f : files) {
cawthron
parents:
diff changeset
   137
			m_outputFileStream.println(f);
cawthron
parents:
diff changeset
   138
		}
cawthron
parents:
diff changeset
   139
	}
cawthron
parents:
diff changeset
   140
cawthron
parents:
diff changeset
   141
	// --- NOTE ------
cawthron
parents:
diff changeset
   142
	// This is for my own manual junit test only, not for public 
cawthron
parents:
diff changeset
   143
	// auto-test.
cawthron
parents:
diff changeset
   144
	public void my_testLineInfoContainsMoreSourceFiles() {
cawthron
parents:
diff changeset
   145
		
cawthron
parents:
diff changeset
   146
		// Iterate through those files and check...
cawthron
parents:
diff changeset
   147
		for (String f : m_dataFileNames) {
cawthron
parents:
diff changeset
   148
			m_outputFileStream.format("Check [%s] :\n", f);
cawthron
parents:
diff changeset
   149
			
cawthron
parents:
diff changeset
   150
			if (lineInfoContainsMoreSourceFiles(f))			
cawthron
parents:
diff changeset
   151
				m_outputFileStream.println("yes !");
cawthron
parents:
diff changeset
   152
			else
cawthron
parents:
diff changeset
   153
				m_outputFileStream.println("No.");
cawthron
parents:
diff changeset
   154
		}
cawthron
parents:
diff changeset
   155
	}
cawthron
parents:
diff changeset
   156
	
cawthron
parents:
diff changeset
   157
	/*
cawthron
parents:
diff changeset
   158
	 * This function checks if .debug_line section contains 
cawthron
parents:
diff changeset
   159
	 * source files that are not referenced by any TAG_compile_units.
cawthron
parents:
diff changeset
   160
	 * 
cawthron
parents:
diff changeset
   161
	 * @param dataFileName
cawthron
parents:
diff changeset
   162
	 */
cawthron
parents:
diff changeset
   163
	private boolean lineInfoContainsMoreSourceFiles(String dataFileName) {
cawthron
parents:
diff changeset
   164
		DwarfReader reader = null;
cawthron
parents:
diff changeset
   165
		
cawthron
parents:
diff changeset
   166
		try {
cawthron
parents:
diff changeset
   167
			reader = new DwarfReader(dataFileName);
cawthron
parents:
diff changeset
   168
		} catch (IOException e) {
cawthron
parents:
diff changeset
   169
			e.printStackTrace();
cawthron
parents:
diff changeset
   170
			fail("File \"" + dataFileName + "\" is not valid Elf file.");
cawthron
parents:
diff changeset
   171
			return false;
cawthron
parents:
diff changeset
   172
		}
cawthron
parents:
diff changeset
   173
cawthron
parents:
diff changeset
   174
		// Hold on !
cawthron
parents:
diff changeset
   175
		// Look at code of getSourceFiles() and add two public 
cawthron
parents:
diff changeset
   176
		// APIs like 
cawthron
parents:
diff changeset
   177
		//		"getSourcesFromCompileUnit()" and 
cawthron
parents:
diff changeset
   178
		//		"getSourcesFromDebugLine()"
cawthron
parents:
diff changeset
   179
		// and then test here.
cawthron
parents:
diff changeset
   180
		String[] filesFromCU = reader.getSourceFiles(); // change to getFromCompileUnit()
cawthron
parents:
diff changeset
   181
		String[] filesFromLineInfo = reader.getSourceFiles(); // change to getFromDebugLine()
cawthron
parents:
diff changeset
   182
		
cawthron
parents:
diff changeset
   183
/*		
cawthron
parents:
diff changeset
   184
 		// Sort the file lists and compare the list if needed.
cawthron
parents:
diff changeset
   185
 		//
cawthron
parents:
diff changeset
   186
		Arrays.sort(filesFromCU);
cawthron
parents:
diff changeset
   187
		Arrays.sort(filesFromLineInfo);
cawthron
parents:
diff changeset
   188
cawthron
parents:
diff changeset
   189
		assertEquals(filesFromCU.length, filesFromLineInfo.length);
cawthron
parents:
diff changeset
   190
		for (int i = 0; i < filesFromCU.length; i++) {
cawthron
parents:
diff changeset
   191
			assertEquals(filesFromCU[i], filesFromLineInfo[i]);
cawthron
parents:
diff changeset
   192
		}
cawthron
parents:
diff changeset
   193
*/
cawthron
parents:
diff changeset
   194
		return (filesFromCU.length > filesFromLineInfo.length);
cawthron
parents:
diff changeset
   195
	}
cawthron
parents:
diff changeset
   196
cawthron
parents:
diff changeset
   197
}