aboutsummaryrefslogtreecommitdiff
path: root/lldb/test/API/api/multithreaded/test_concurrent_unwind.cpp.template
blob: e5101dde79619a11df0ee8655d52dc4376bdf919 (plain)
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
#include "pseudo_barrier.h"

#include <atomic>
#include <thread>

%include_SB_APIs%

#include "common.h"

using namespace lldb;

void test (SBDebugger &dbg, std::vector<std::string> args) {

SBError error;
  dbg.SetAsync(false);
  SBTarget target = dbg.CreateTarget(args.at(0).c_str());
  if (!target.IsValid())
    throw Exception("Invalid target");

  // Now set our breakpoint and launch:
  SBFileSpec main_sourcefile("deep_stack.cpp");
  SBBreakpoint bkpt = target.BreakpointCreateBySourceRegex("Set a breakpoint here",
                                                           main_sourcefile);
  if (bkpt.GetNumLocations() == 0)
    throw Exception("Main breakpoint got no locations");

  SBLaunchInfo launch_info = target.GetLaunchInfo();
  SBProcess process = target.Launch(launch_info, error);
  if (error.Fail())
    throw Exception("Failed to launch process");
  if (!process.IsValid())
    throw Exception("Process is not valid");
  if (process.GetState() != lldb::eStateStopped)
    throw Exception("Process was not stopped");

  size_t num_threads = process.GetNumThreads();
  if (num_threads != 1)
    throw Exception("Unexpected number of threads.");
  SBThread cur_thread = process.GetThreadAtIndex(0);
  if (!cur_thread.IsValid())
    throw Exception("Didn't get a valid thread");

  // Record the number of frames at the point where we stopped:
  const size_t num_frames = cur_thread.GetNumFrames();
  // Now step once to clear the frame cache:
  cur_thread.StepOver();
  
  // Create three threads and set them to getting frames simultaneously,
  // and make sure we don't deadlock.
  pseudo_barrier_t rendevous;
  pseudo_barrier_init(rendevous, 5);
  std::atomic_size_t success(true);
  std::atomic_size_t largest(0);

  auto lambda = [&](size_t stride){
    pseudo_barrier_wait(rendevous);
    bool younger = true;
    while (1) {
      size_t cursor = largest;
      if (cursor > stride && !younger) {
        cursor -= stride;
        younger = true;
      } else {
        cursor += stride;
        largest += stride;
        younger = false;
      }
      SBFrame frame = cur_thread.GetFrameAtIndex(cursor);
      if (!frame.IsValid()) {
        if (cursor < num_frames)
          success = false;
        break;
      }
    }
    
  };

  std::thread thread1(lambda, 1);
  std::thread thread2(lambda, 3);
  std::thread thread3(lambda, 5);
  std::thread thread4(lambda, 7);
  std::thread thread5(lambda, 11);
  thread1.join();
  thread2.join();
  thread3.join();
  thread4.join();
  thread5.join();
  
  if (!success)
    throw Exception("One thread stopped before 1000");
}