1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "Image.h"
7
8#include "ImageDebugInfo.h"
9#include "LocatableFile.h"
10#include "Team.h"
11#include "TeamDebugInfo.h"
12
13
14Image::Image(Team* team,const ImageInfo& imageInfo, LocatableFile* imageFile)
15	:
16	fTeam(team),
17	fInfo(imageInfo),
18	fImageFile(imageFile),
19	fDebugInfo(NULL),
20	fDebugInfoState(IMAGE_DEBUG_INFO_NOT_LOADED)
21{
22	if (fImageFile != NULL)
23		fImageFile->AcquireReference();
24}
25
26
27Image::~Image()
28{
29	if (fDebugInfo != NULL) {
30		if (fTeam != NULL)
31			fTeam->DebugInfo()->RemoveImageDebugInfo(fDebugInfo);
32		fDebugInfo->ReleaseReference();
33	}
34	if (fImageFile != NULL)
35		fImageFile->ReleaseReference();
36}
37
38
39status_t
40Image::Init()
41{
42	return B_OK;
43}
44
45
46bool
47Image::ContainsAddress(target_addr_t address) const
48{
49	return (address >= fInfo.TextBase()
50			&& address < fInfo.TextBase() + fInfo.TextSize())
51		|| (address >= fInfo.DataBase()
52			&& address < fInfo.DataBase() + fInfo.DataSize());
53}
54
55
56status_t
57Image::SetImageDebugInfo(ImageDebugInfo* debugInfo,
58	image_debug_info_state state)
59{
60	if (debugInfo == fDebugInfo && state == fDebugInfoState)
61		return B_OK;
62
63	if (fDebugInfo != NULL) {
64		fTeam->DebugInfo()->RemoveImageDebugInfo(fDebugInfo);
65		fDebugInfo->ReleaseReference();
66	}
67
68	fDebugInfo = debugInfo;
69	fDebugInfoState = state;
70
71	status_t error = B_OK;
72	if (fDebugInfo != NULL) {
73		error = fTeam->DebugInfo()->AddImageDebugInfo(fDebugInfo);
74		if (error == B_OK) {
75			fDebugInfo->AcquireReference();
76		} else {
77			fDebugInfo = NULL;
78			fDebugInfoState = IMAGE_DEBUG_INFO_UNAVAILABLE;
79		}
80	}
81
82	// notify listeners
83	fTeam->NotifyImageDebugInfoChanged(this);
84
85	return error;
86}
87