source: anotherchoice/tags/jsp-1.4.4-full-UTF8/cfg/base/fc_windows.cpp@ 26

Last change on this file since 26 was 26, checked in by ykominami, 12 years ago

initial

File size: 10.8 KB
RevLine 
[26]1/*
2 * TOPPERS/JSP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 *
9 * 上記著作権者
10は,以下の (1)〜(4) の条件か,Free Software Foundation
11 * によってå…
12¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
13 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
14 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
15å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
16 * 利用と呼ぶ)することを無償で許諾する.
17 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
18 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
19 * スコード中に含まれていること.
20 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
21 * 用できる形で再é…
22å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
23å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
24 * 者
25マニュアルなど)に,上記の著作権表示,この利用条件および下記
26 * の無保証規定を掲載すること.
27 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
28 * 用できない形で再é…
29å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
30 * と.
31 * (a) 再é…
32å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
33マニュアルなど)に,上記の著
34 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
35 * (b) 再é…
36å¸ƒã®å½¢æ…
37‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
38 * 報告すること.
39 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
40 * 害からも,上記著作権者
41およびTOPPERSプロジェクトをå…
42è²¬ã™ã‚‹ã“と.
43 *
44 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
45お
46 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
47 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
48 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
49 *
50 * @(#) $Id: fc_windows.cpp,v 1.12 2003/12/20 06:51:58 takayuki Exp $
51 */
52
53#if (defined(FILECONTAINER_WINDOWS) || defined(TESTSUITE)) && defined(_MSC_VER)
54
55#pragma warning(disable:4786)
56
57#include "base/filecontainer.h"
58#include <windows.h>
59#include <imagehlp.h>
60#include <string>
61
62using namespace std;
63
64namespace {
65
66 class FileContainerWindowsImpl : public FileContainer
67 {
68 protected:
69 HANDLE process;
70 LOADED_IMAGE image;
71 DWORD base;
72
73 inline bool isLoaded(void) const
74 { return base != 0; }
75
76 public:
77 FileContainerWindowsImpl(void) throw();
78 virtual ~FileContainerWindowsImpl(void) throw();
79
80 /* インタフェース部 */
81 virtual void attachModule(const string & filename) throw(Exception);
82 virtual void loadContents(void * dest, address_t address, size_t size) throw(Exception);
83 virtual address_t getSymbolAddress(const string & symbol) throw(Exception);
84 virtual std::string getArchitecture(void) throw();
85
86 TESTSUITE_PROTOTYPE(main)
87 };
88
89 FileContainerWindowsImpl instance_of_FileContaienrWindowsImpl;
90
91 /* コンストラクタ */
92 FileContainerWindowsImpl::FileContainerWindowsImpl(void) throw()
93 : process(NULL), image(), base(0)
94 {}
95
96 /* デストラクタ */
97 FileContainerWindowsImpl::~FileContainerWindowsImpl(void) throw()
98 {
99 if(isLoaded()) {
100 ::UnMapAndLoad(&image);
101 ::SymUnloadModule(process, base);
102 process = NULL;
103 base = 0;
104 }
105 }
106
107 /* 対象モジュールの割付 */
108 void FileContainerWindowsImpl::attachModule(const std::string & _filename) throw(Exception)
109 {
110 string filename(_filename);
111
112 process = ::GetCurrentProcess();
113 if(::SymInitialize( process , NULL, FALSE) == FALSE)
114 ExceptionMessage("[Internal Error] ImageHelper API initialization failure","[内
115部エラー] 初期化に失敗しました (ImageHlp)").throwException();
116
117 base = ::SymLoadModule(process, NULL, (PSTR)filename.c_str(), NULL, 0, 0);
118
119 image.SizeOfImage = sizeof(LOADED_IMAGE);
120 if(::MapAndLoad((PSTR)filename.c_str(), NULL, &image, FALSE, TRUE) == FALSE)
121 ExceptionMessage("[Internel error] Module loading failure [%]","[内
122部エラー] モジュールの読み込みに失敗しました [%]") << filename << throwException;
123 }
124
125 /* 内
126容の取得 */
127 void FileContainerWindowsImpl::loadContents(void * dest, address_t address, size_t size) throw(Exception)
128 {
129 PIMAGE_SECTION_HEADER header;
130 unsigned int i;
131
132 address -= base;
133 for(i=0;i<image.NumberOfSections;i++) {
134 header = image.Sections+i;
135 if(address >= header->VirtualAddress && address < header->VirtualAddress + header->SizeOfRawData) {
136 address -= header->VirtualAddress - header->PointerToRawData;
137 ::CopyMemory(dest,image.MappedAddress + address,size);
138 break;
139 }
140 }
141
142 if(i == image.NumberOfSections)
143 ExceptionMessage("[Internel error] Memory read with unmapped address","[内
144部エラー] マップされてないアドレスを使ってメモリリードが行われました").throwException();
145 }
146
147 /* シンボルアドレスの解決 */
148 FileContainer::address_t FileContainerWindowsImpl::getSymbolAddress(const string & _symbol) throw(Exception)
149 {
150 FileContainer::address_t result = 0;
151 string symbol(_symbol);
152 IMAGEHLP_SYMBOL sym;
153
154 if(process == NULL || base == 0)
155 ExceptionMessage("Not initialized","初期化されてません").throwException();
156
157 sym.SizeOfStruct = sizeof(sym);
158 sym.MaxNameLength = 0;
159
160 if(::SymGetSymFromName(process, (PSTR)symbol.c_str(), &sym) == TRUE)
161 result = static_cast<FileContainer::address_t>(sym.Address);
162
163 if(result == 0)
164 ExceptionMessage("Unknown symbol [%]","不正なシンボル名 [%]") << symbol << throwException;
165
166 return static_cast<FileContainer::address_t>(sym.Address);
167 }
168
169 string FileContainerWindowsImpl::getArchitecture(void) throw()
170 { return "Windows (Win32)"; }
171
172}
173
174//---------------------------------------------
175
176#ifdef TESTSUITE
177#include "base/coverage_undefs.h"
178
179#pragma warning(disable:4311) //'reinterpret_cast' : ポインタを 'const int *__w64 ' から 'FileContainer::address_t' へ切り詰めます。
180
181extern "C" const int FileContainerWindowsImplTestVariable = 0x01234567;
182extern "C" const int _FileContainerWindowsImplTestVariableWithUnderbar = 0x89abcdef;
183
184TESTSUITE(main, FileContainerWindowsImpl)
185{
186 PREDECESSOR("TFileContainer");
187
188 SingletonBase::ContextChain chain;
189 chain.saveContext<RuntimeObjectTable>();
190 chain.renewInstance();
191
192 BEGIN_CASE("attachModule","attachModule") {
193 BEGIN_CASE("1","実行しているプログラムが開けるか") {
194 FileContainerWindowsImpl fcwi;
195 bool result = true;
196 try { fcwi.attachModule(TestSuite::getProgName()); } catch(...) { result = false; }
197
198 TEST_CASE("1", "例外は起きない", result);
199 } END_CASE;
200
201 BEGIN_CASE("2","存在しないファイル名で例外") {
202 FileContainerWindowsImpl fcwi;
203 bool result = false;
204 try { fcwi.attachModule("..."); } catch(...) { result = true; }
205
206 TEST_CASE("1", "例外が起きる", result);
207 } END_CASE;
208 } END_CASE;
209
210 BEGIN_CASE("getSymbolAddress","getSymbolAddress") {
211 FileContainerWindowsImpl fcwi;
212
213 BEGIN_CASE("1","初期化していない状æ…
214‹ã§æ¤œç´¢ã™ã‚‹") {
215 bool result = false;
216 try { fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable"); } catch(...) { result = true; }
217 TEST_CASE("1","例外がおきる", result);
218 } END_CASE;
219
220 fcwi.attachModule(TestSuite::getProgName());
221
222 BEGIN_CASE("2","存在するシンボルを検索する") {
223 FileContainer::address_t addr = 0;
224 bool result = true;
225
226 try { addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable"); } catch(...) { result = false; }
227
228 TEST_CASE("1","例外は起きない", result);
229 TEST_CASE("2","アドレスが正しい", addr == reinterpret_cast<FileContainer::address_t>(&FileContainerWindowsImplTestVariable));
230 } END_CASE;
231
232 BEGIN_CASE("3","余計な_を勝手に付加しない") {
233 FileContainer::address_t addr = 0;
234 bool result = false;
235
236 try { addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariableWithUnderbar"); } catch(...) { result = true; }
237
238 TEST_CASE("1","例外が起きる", result);
239 TEST_CASE("2","アドレスは0のまま", addr == 0);
240 } END_CASE;
241
242 BEGIN_CASE("4","存在しないシンボルを検索する") {
243 FileContainer::address_t addr = 0;
244 bool result = false;
245
246 try { addr = fcwi.getSymbolAddress("____unknown____"); } catch(...) { result = true; }
247
248 TEST_CASE("1","例外がおきる", result);
249 } END_CASE;
250 } END_CASE;
251
252 BEGIN_CASE("loadContents","loadContents") {
253 FileContainerWindowsImpl fcwi;
254 fcwi.attachModule(TestSuite::getProgName());
255
256 BEGIN_CASE("1","存在する変数を読み出す") {
257 FileContainer::address_t addr;
258 int i;
259
260 addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable");
261 bool result = true;
262 try { fcwi.loadContents(&i, addr, sizeof(i)); } catch(...) { result = false; }
263
264 TEST_CASE("1","例外は起きない", result);
265 TEST_CASE("2","読み出された内
266容は正しい", i == FileContainerWindowsImplTestVariable);
267 } END_CASE;
268
269 BEGIN_CASE("2","存在しない変数を読み出す") {
270 FileContainer::address_t addr;
271 int i;
272
273 addr = ~0;
274 bool result = false;
275 try { fcwi.loadContents(&i, addr, sizeof(i)); } catch(Exception &) { result = true; }
276
277 TEST_CASE("1","例外が起こる", result);
278 } END_CASE;
279
280 } END_CASE;
281
282 chain.restoreContext();
283}
284
285#endif /* TESTSUITE */
286
287#endif /* FILECONTAINER_WINDOWS || TESTSUITE */
288
Note: See TracBrowser for help on using the repository browser.