File: gdb_api.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (270 lines) | stat: -rw-r--r-- 6,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************\

Module: GDB Machine Interface API unit tests

Author: Malte Mues <mail.mues@gmail.com>
        Daniel Poetzl

\*******************************************************************/

#include <util/run.h>
#include <util/tempfile.h>

#include <memory-analyzer/gdb_api.h>
#include <testing-utils/use_catch.h>

#include <cstdio>
#include <fstream> // IWYU pragma: keep
#include <iostream>
#include <regex>
#include <string>
#include <vector>

struct compile_test_filet
{
  compile_test_filet() : compiled("test", "")
  {
    temporary_filet tmp("test", ".c");
    std::ofstream of(tmp().c_str());
    REQUIRE(of.is_open());

    of <<
#include <memory-analyzer/test.inc> // IWYU pragma: keep
      ;                             // NOLINT(whitespace/semicolon)
    of.close();

    REQUIRE(run("gcc", {"gcc", "-g", "-o", compiled(), tmp()}) == 0);
  }

  temporary_filet compiled;
};

void check_for_gdb()
{
  REQUIRE(run("gdb", {"gdb", "--version"}, "", "/dev/null", "/dev/null") == 0);
}

class gdb_api_testt : public gdb_apit
{
public:
  explicit gdb_api_testt(const std::vector<std::string> &args) : gdb_apit(args)
  {
  }

  friend void gdb_api_internals_test();

  using gdb_apit::r_hex_addr;
};

void gdb_api_internals_test()
{
  check_for_gdb();
  compile_test_filet test_file;

  std::vector<std::string> args;
  args.push_back(test_file.compiled());

  SECTION("parse gdb output record")
  {
    gdb_api_testt gdb_api(args);

    gdb_api_testt::gdb_output_recordt gor = gdb_api.parse_gdb_output_record(
      "a = \"1\", b = \"2\", c = {1, 2}, d = [3, 4], e=\"0x0\"");

    REQUIRE(gor["a"] == "1");
    REQUIRE(gor["b"] == "2");
    REQUIRE(gor["c"] == "{1, 2}");
    REQUIRE(gor["d"] == "[3, 4]");
    REQUIRE(gor["e"] == "0x0");
  }

  SECTION("read a line from an input stream")
  {
    temporary_filet tmp("input", ".txt");
    std::ofstream of(tmp().c_str());
    REQUIRE(of.is_open());

    of <<
#include <memory-analyzer/input.inc> // IWYU pragma: keep
      ;                              // NOLINT(whitespace/semicolon)
    of.close();

    gdb_api_testt gdb_api(args);

    FILE *f = fopen(tmp().c_str(), "r");
    gdb_api.response_stream = f;

    std::string line = gdb_api.read_next_line();
    REQUIRE(line == "abc\n");

    line = gdb_api.read_next_line();
    REQUIRE(line == std::string(1120, 'a') + "\n");

    line = gdb_api.read_next_line();
    REQUIRE(line == "xyz\n");
  }

  SECTION("start and exit gdb")
  {
    gdb_api_testt gdb_api(args);

    gdb_api.create_gdb_process();

    // check input and output streams
    REQUIRE(!ferror(gdb_api.response_stream));
    REQUIRE(!ferror(gdb_api.command_stream));
  }
}

TEST_CASE("gdb api internals test", "[core][memory-analyzer]")
{
  gdb_api_internals_test();
}

TEST_CASE("gdb api test", "[core][memory-analyzer]")
{
  check_for_gdb();
  compile_test_filet test_file;

  std::vector<std::string> args;
  args.push_back(test_file.compiled());

  {
    gdb_apit gdb_api(args, true);
    gdb_api.create_gdb_process();

    try
    {
      const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
      REQUIRE(r);
    }
    catch(const gdb_interaction_exceptiont &e)
    {
      std::cerr << "warning: cannot fully unit test GDB API as program cannot "
                << "be run with gdb\n";
      std::cerr << "warning: this may be due to not having the required "
                << "permissions (e.g., to invoke ptrace() or to disable ASLR)"
                << "\n";
      std::cerr << "gdb_interaction_exceptiont:" << '\n';
      std::cerr << e.what() << '\n';

      std::ifstream file("gdb.txt");
      CHECK_RETURN(file.is_open());
      std::string line;

      std::cerr << "=== gdb log begin ===\n";

      while(getline(file, line))
      {
        std::cerr << line << '\n';
      }

      file.close();

      std::cerr << "=== gdb log end ===\n";

      return;
    }
  }

  gdb_api_testt gdb_api(args);

  std::regex hex_addr(gdb_api.r_hex_addr);

  gdb_api.create_gdb_process();

  SECTION("breakpoint is hit")
  {
    const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
    REQUIRE(r);
  }

  SECTION("breakpoint is not hit")
  {
    const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
    REQUIRE(!r);
  }

  SECTION("breakpoint does not exist")
  {
    REQUIRE_THROWS_AS(
      gdb_api.run_gdb_to_breakpoint("checkpoint3"), gdb_interaction_exceptiont);
  }

  SECTION("query variables, primitive types")
  {
    const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
    REQUIRE(r);

    const auto &x_value = gdb_api.get_value("x");
    const auto &y_value = gdb_api.get_value("y");
    const auto &z_value = gdb_api.get_value("z");

    REQUIRE(x_value.has_value());
    REQUIRE(*x_value == "8");
    REQUIRE(y_value.has_value());
    REQUIRE(*y_value == "2.5");
    REQUIRE(z_value.has_value());
    REQUIRE(*z_value == "c");
  }

  SECTION("query pointers")
  {
    const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
    REQUIRE(r);

    {
      auto value = gdb_api.get_memory("s");
      REQUIRE(std::regex_match(value.address.string(), hex_addr));
      REQUIRE(value.pointee.empty());
      REQUIRE(value.character.empty());
      REQUIRE(*value.string == "abc");
    }

    {
      auto value = gdb_api.get_memory("p");
      REQUIRE(std::regex_match(value.address.string(), hex_addr));
      REQUIRE(value.pointee == "x");
      REQUIRE(value.character.empty());
      REQUIRE(!value.string);
    }

    {
      auto value = gdb_api.get_memory("vp");
      REQUIRE(std::regex_match(value.address.string(), hex_addr));
      REQUIRE(value.pointee == "x");
      REQUIRE(value.character.empty());
      REQUIRE(!value.string);
    }

    {
      auto value = gdb_api.get_memory("np");
      REQUIRE(value.address.is_null());
      REQUIRE(value.pointee.empty());
      REQUIRE(value.character.empty());
      REQUIRE(!value.string);
    }

    {
      auto value = gdb_api.get_memory("vp_string");
      REQUIRE(std::regex_match(value.address.string(), hex_addr));
      REQUIRE(value.pointee.empty());
      REQUIRE(value.character.empty());
      REQUIRE(!value.string);
    }
  }

  SECTION("query expressions")
  {
    const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
    REQUIRE(r);

    {
      auto value = gdb_api.get_memory("&x");
      REQUIRE(std::regex_match(value.address.string(), hex_addr));
      REQUIRE(value.pointee == "x");
      REQUIRE(value.character.empty());
      REQUIRE(!value.string);
    }
  }
}