RemoteDrawingEngine: Reduce RP_READ_BITMAP result timeout.
[haiku.git] / src / bin / debug / strace / MemoryReader.cpp
blobdf593b6fc7046ff19dc4426b51a0006b9a7cd6bf
1 /*
2 * Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include <debugger.h>
13 #include "MemoryReader.h"
16 MemoryReader::MemoryReader()
18 fNubPort(-1),
19 fReplyPort(-1)
24 MemoryReader::~MemoryReader()
26 if (fReplyPort >= 0)
27 delete_port(fReplyPort);
31 status_t
32 MemoryReader::Init(port_id nubPort)
34 if (fReplyPort >= 0)
35 delete_port(fReplyPort);
37 fNubPort = nubPort;
39 fReplyPort = create_port(1, "memory reader reply");
40 if (fReplyPort < 0) {
41 fprintf(stderr, "Failed to create memory reader reply port: %s\n",
42 strerror(fReplyPort));
43 return fReplyPort;
46 return B_OK;
50 status_t
51 MemoryReader::Read(void *_address, void *_buffer, int32 size, int32 &bytesRead)
53 char *address = (char*)_address;
54 char *buffer = (char*)_buffer;
55 bytesRead = 0;
57 while (size > 0) {
58 int32 toRead = size;
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.
66 if (error != B_OK) {
67 if (bytesRead > 0)
68 return B_OK;
69 return error;
72 bytesRead += actuallyRead;
73 address += actuallyRead;
74 buffer += actuallyRead;
75 size -= actuallyRead;
78 return B_OK;
82 status_t
83 MemoryReader::_Read(void *address, void *buffer, int32 size, int32 &bytesRead)
85 // prepare message
86 debug_nub_read_memory message;
87 message.reply_port = fReplyPort;
88 message.address = address;
89 message.size = size;
91 // send message
92 while (true) {
93 status_t error = write_port(fNubPort, B_DEBUG_MESSAGE_READ_MEMORY,
94 &message, sizeof(message));
95 if (error == B_OK)
96 break;
97 if (error != B_INTERRUPTED)
98 return error;
101 // get reply
102 int32 code;
103 debug_nub_read_memory_reply reply;
104 while (true) {
105 ssize_t bytesRead = read_port(fReplyPort, &code, &reply, sizeof(reply));
106 if (bytesRead > 0)
107 break;
108 if (bytesRead != B_INTERRUPTED)
109 return bytesRead;
112 if (reply.error != B_OK)
113 return reply.error;
115 bytesRead = reply.size;
116 memcpy(buffer, reply.data, bytesRead);
118 return B_OK;