test_usdt_args.cc 2.34 KB
Newer Older
Vicent Marti's avatar
Vicent Marti committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2016 GitHub, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
16
#include <iostream>
17 18
#include <string>

19 20 21
#include "catch.hpp"
#include "usdt.h"

22 23 24
using std::experimental::optional;
using std::experimental::nullopt;

25
static void verify_register(USDT::ArgumentParser_x64 &parser, int arg_size,
26
                            int constant) {
27
  USDT::Argument arg;
28 29
  REQUIRE(parser.parse(&arg));
  REQUIRE(arg.arg_size() == arg_size);
30 31

  REQUIRE(arg.constant());
32
  REQUIRE(arg.constant() == constant);
33 34 35 36 37 38 39 40 41 42 43 44 45
}

static void verify_register(USDT::ArgumentParser_x64 &parser, int arg_size,
                            const std::string &regname,
                            optional<int> deref_offset = nullopt,
                            optional<std::string> deref_ident = nullopt) {
  USDT::Argument arg;
  REQUIRE(parser.parse(&arg));
  REQUIRE(arg.arg_size() == arg_size);

  REQUIRE(arg.register_name());
  REQUIRE(arg.register_name() == regname);

46 47
  REQUIRE(arg.deref_offset() == deref_offset);
  REQUIRE(arg.deref_ident() == deref_ident);
48 49 50 51 52 53 54 55 56 57
}

TEST_CASE("test usdt argument parsing", "[usdt]") {
  SECTION("argument examples from the Python implementation") {
    USDT::ArgumentParser_x64 parser(
        "-4@$0 8@$1234 %rdi %rax %rsi "
        "-8@%rbx 4@%r12 8@-8(%rbp) 4@(%rax) "
        "-4@global_max_action(%rip) "
        "8@24+mp_(%rip) ");

58 59 60
    verify_register(parser, -4, 0);
    verify_register(parser, 8, 1234);

61 62 63 64 65
    verify_register(parser, 8, "di");
    verify_register(parser, 8, "ax");
    verify_register(parser, 8, "si");
    verify_register(parser, -8, "bx");
    verify_register(parser, 4, "r12");
66

67 68
    verify_register(parser, 8, "bp", -8);
    verify_register(parser, 4, "ax", 0);
69

70 71
    verify_register(parser, -4, "ip", 0, std::string("global_max_action"));
    verify_register(parser, 8, "ip", 24, std::string("mp_"));
72 73 74 75

    REQUIRE(parser.done());
  }
}