|
13 | 13 |
|
14 | 14 | gdb_apit::gdb_apit(const char *arg) : binary_name(arg)
|
15 | 15 | {
|
16 |
| - memset(buffer, 0, MAX_READ_SIZE_GDB_BUFFER); |
17 | 16 | }
|
18 | 17 |
|
19 | 18 | int gdb_apit::create_gdb_process()
|
20 | 19 | {
|
| 20 | + pid_t gdb_process; |
| 21 | + |
| 22 | + int pipe_input[2]; |
| 23 | + int pipe_output[2]; |
| 24 | + |
21 | 25 | if(pipe(pipe_input) == -1)
|
22 | 26 | {
|
23 | 27 | throw gdb_interaction_exceptiont("could not create pipe for stdin!");
|
@@ -56,55 +60,60 @@ int gdb_apit::create_gdb_process()
|
56 | 60 | // parent process
|
57 | 61 | close(pipe_input[0]);
|
58 | 62 | close(pipe_output[1]);
|
| 63 | + |
| 64 | + // get stream for reading the gdb output |
| 65 | + input_stream = fdopen(pipe_output[0], "r"); |
| 66 | + |
| 67 | + // get stream for writing to gdb |
| 68 | + output_stream = fdopen(pipe_input[1], "w"); |
| 69 | + |
59 | 70 | write_to_gdb("set max-value-size unlimited\n");
|
60 | 71 | }
|
61 | 72 | return 0;
|
62 | 73 | }
|
63 | 74 |
|
64 | 75 | void gdb_apit::terminate_gdb_process()
|
65 | 76 | {
|
66 |
| - close(pipe_input[0]); |
67 |
| - close(pipe_input[1]); |
| 77 | + fclose(input_stream); |
| 78 | + fclose(output_stream); |
68 | 79 | }
|
69 | 80 |
|
70 | 81 | void gdb_apit::write_to_gdb(const std::string &command)
|
71 | 82 | {
|
72 |
| - if( |
73 |
| - write(pipe_input[1], command.c_str(), command.length()) != |
74 |
| - (ssize_t)command.length()) |
| 83 | + if(fputs(command.c_str(), output_stream) == EOF) |
75 | 84 | {
|
76 | 85 | throw gdb_interaction_exceptiont("Could not write a command to gdb");
|
77 | 86 | }
|
78 | 87 | }
|
79 | 88 |
|
80 | 89 | std::string gdb_apit::read_next_line()
|
81 | 90 | {
|
82 |
| - char token; |
83 |
| - std::string line; |
| 91 | + std::string result; |
| 92 | + |
84 | 93 | do
|
85 | 94 | {
|
86 |
| - while(buffer_position >= last_read_size) |
| 95 | + const size_t buf_size = 1024; |
| 96 | + char buf[buf_size]; |
| 97 | + |
| 98 | + const char *c = fgets(buf, buf_size, input_stream); |
| 99 | + |
| 100 | + if(c == NULL) |
87 | 101 | {
|
88 |
| - read_next_buffer_chunc(); |
| 102 | + if(ferror(input_stream)) |
| 103 | + { |
| 104 | + throw gdb_interaction_exceptiont("error reading from gdb"); |
| 105 | + } |
| 106 | + |
| 107 | + INVARIANT(feof(input_stream), ""); |
| 108 | + INVARIANT(result.back() != '\n', ""); |
| 109 | + |
| 110 | + return result; |
89 | 111 | }
|
90 |
| - token = buffer[buffer_position]; |
91 |
| - line += token; |
92 |
| - ++buffer_position; |
93 |
| - } while(token != '\n'); |
94 |
| - return line; |
95 |
| -} |
96 | 112 |
|
97 |
| -void gdb_apit::read_next_buffer_chunc() |
98 |
| -{ |
99 |
| - memset(buffer, 0, last_read_size); |
100 |
| - const auto read_size = |
101 |
| - read(pipe_output[0], &buffer, MAX_READ_SIZE_GDB_BUFFER); |
102 |
| - if(read_size < 0) |
103 |
| - { |
104 |
| - throw gdb_interaction_exceptiont("Error reading from gdb_process"); |
105 |
| - } |
106 |
| - last_read_size = read_size; |
107 |
| - buffer_position = 0; |
| 113 | + result += std::string(buf); |
| 114 | + } while(result.back() != '\n'); |
| 115 | + |
| 116 | + return result; |
108 | 117 | }
|
109 | 118 |
|
110 | 119 | void gdb_apit::run_gdb_from_core(const std::string &corefile)
|
|
0 commit comments