2 * Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
13 #include "MemoryReader.h"
16 MemoryReader::MemoryReader()
24 MemoryReader::~MemoryReader()
27 delete_port(fReplyPort
);
32 MemoryReader::Init(port_id nubPort
)
35 delete_port(fReplyPort
);
39 fReplyPort
= create_port(1, "memory reader reply");
41 fprintf(stderr
, "Failed to create memory reader reply port: %s\n",
42 strerror(fReplyPort
));
51 MemoryReader::Read(void *_address
, void *_buffer
, int32 size
, int32
&bytesRead
)
53 char *address
= (char*)_address
;
54 char *buffer
= (char*)_buffer
;
59 if (toRead
> B_MAX_READ_WRITE_MEMORY_SIZE
)
60 toRead
= B_MAX_READ_WRITE_MEMORY_SIZE
;
62 int32 actuallyRead
= 0;
63 status_t error
= _Read(address
, buffer
, toRead
, actuallyRead
);
65 // If reading fails, we only fail, if we haven't read anything yet.
72 bytesRead
+= actuallyRead
;
73 address
+= actuallyRead
;
74 buffer
+= actuallyRead
;
83 MemoryReader::_Read(void *address
, void *buffer
, int32 size
, int32
&bytesRead
)
86 debug_nub_read_memory message
;
87 message
.reply_port
= fReplyPort
;
88 message
.address
= address
;
93 status_t error
= write_port(fNubPort
, B_DEBUG_MESSAGE_READ_MEMORY
,
94 &message
, sizeof(message
));
97 if (error
!= B_INTERRUPTED
)
103 debug_nub_read_memory_reply reply
;
105 ssize_t bytesRead
= read_port(fReplyPort
, &code
, &reply
, sizeof(reply
));
108 if (bytesRead
!= B_INTERRUPTED
)
112 if (reply
.error
!= B_OK
)
115 bytesRead
= reply
.size
;
116 memcpy(buffer
, reply
.data
, bytesRead
);