24 std::atomic<std::uint64_t> *publish_storage,
25 std::size_t publish_word_capacity,
27 std::size_t publish_entry_capacity,
28 std::atomic<std::uint64_t> *trace_storage,
29 std::size_t trace_word_capacity,
34 if(runtime.program_ ==
nullptr || target.
resource ==
nullptr ||
35 target.
task ==
nullptr || publish_storage ==
nullptr ||
36 publish_entries ==
nullptr ||
37 (trace_word_capacity != 0 && trace_storage ==
nullptr))
39 if(runtime.debug_hooks_ !=
nullptr && runtime.debug_hooks_ !=
this)
45 if(trace_word_capacity != 0 &&
47 sizeof(DebugTraceRecord) /
sizeof(std::uint64_t))
49 ConfigurationRuntime::ResourceRuntime *resource =
50 runtime.find_resource(target.
resource);
51 int task_index = resource ==
nullptr
53 : runtime.find_task(*resource, target.
task);
54 if(task_index < 0)
return last_error_;
55 ConfigurationRuntime::TaskRuntime &
task =
56 resource->tasks[
static_cast<std::size_t
>(task_index)];
57 if(
task.info->mappings.empty())
return last_error_;
58 const std::size_t mapping_index =
task.info->mappings.front();
59 if(mapping_index >= resource->mapping_count)
return last_error_;
60 std::vector<std::uint32_t> breakpoint_bases(
61 resource->mapping_count, invalid_instruction);
62 std::vector<ActivePou> active_pous;
63 std::vector<SnapshotSfcStep> snapshot_sfc_steps;
64 active_pous.reserve(
task.info->mappings.size());
66 for(
const std::uint16_t selected :
task.info->mappings) {
67 if(selected >= resource->mapping_count)
return last_error_;
68 ConfigurationRuntime::MappingRuntime &selected_mapping =
69 resource->mappings[selected];
70 active_pous.push_back({
72 &selected_mapping.program->program_name,
73 static_cast<std::uint32_t
>(selected)});
74 for(std::size_t network = 0;
75 network < selected_mapping.program->sfc_networks.size();
77 const SfcNetworkInfo &network_info =
78 selected_mapping.program->sfc_networks[network];
79 const SymbolId network_id =
81 for(std::size_t
step = 0;
82 step < network_info.steps.size(); ++
step) {
85 const SfcStepInfo &step_info = network_info.steps[
step];
86 snapshot_sfc_steps.push_back({
87 &selected_mapping, network,
step, network_id,
90 &network_info.name, &step_info.name,
91 static_cast<std::uint32_t
>(selected)});
97 .program->source_map.entries.size();
99 std::numeric_limits<std::uint32_t>::max())
102 const std::size_t breakpoint_words =
104 const std::size_t sfc_state_words =
105 (snapshot_sfc_steps.size() + 63U) / 64U;
106 if(breakpoint_words + header_words + sfc_state_words >
107 publish_word_capacity)
109 static_assert(
sizeof(DebugTraceRecord) %
sizeof(std::uint64_t) == 0,
110 "trace records use complete atomic words");
111 const std::size_t trace_words_per_record =
112 sizeof(DebugTraceRecord) /
sizeof(std::uint64_t);
113 const std::size_t trace_capacity = std::min(
114 trace_word_capacity / trace_words_per_record,
115 options.trace_capacity);
116 for(std::size_t index = 0; index < publish_word_capacity; ++index)
117 publish_storage[index].store(0, std::memory_order_relaxed);
118 for(std::size_t index = 0;
119 index < trace_capacity * trace_words_per_record;
121 trace_storage[index].store(0, std::memory_order_relaxed);
124 resource_ = resource;
126 task_index_ =
static_cast<std::size_t
>(task_index);
127 mapping_index_ = mapping_index;
128 mapping_ = &resource->mappings[mapping_index];
129 program_ = mapping_->program;
132 publish_words_ = publish_storage;
133 publish_word_capacity_ = publish_word_capacity;
134 publish_entries_ = publish_entries;
135 publish_entry_capacity_ = publish_entry_capacity;
136 trace_words_ = trace_storage;
137 trace_words_per_record_ = trace_words_per_record;
138 trace_storage_capacity_ = trace_capacity;
141 watches_.reserve(options.max_watch_symbols);
142 watch_plan_state_.store(watch_plan_open,
143 std::memory_order_relaxed);
144 snapshot_ready_.store(
true, std::memory_order_relaxed);
145 value_word_count_ = 0;
147 breakpoints_.clear();
148 breakpoints_.reserve(options.max_breakpoints);
149 mapping_breakpoint_bases_ =
150 static_cast<std::vector<std::uint32_t> &&
>(breakpoint_bases);
151 active_pous_ =
static_cast<std::vector<ActivePou> &&
>(active_pous);
152 snapshot_sfc_steps_ =
153 static_cast<std::vector<SnapshotSfcStep> &&
>(snapshot_sfc_steps);
154 sfc_state_word_count_ = sfc_state_words;
155 breakpoint_word_count_ = breakpoint_words;
156 breakpoint_words_ = publish_words_ +
157 publish_word_capacity_ - breakpoint_word_count_;
158 trace_lock_.store(0, std::memory_order_relaxed);
159 trace_begin_.store(0, std::memory_order_relaxed);
160 trace_size_.store(0, std::memory_order_relaxed);
161 trace_sequence_.store(0, std::memory_order_relaxed);
162 trace_dropped_.store(0, std::memory_order_relaxed);
163 stop_pending_.store(
false, std::memory_order_relaxed);
164 command_mailbox_.store(0, std::memory_order_relaxed);
165 observed_task_state_.store(
166 static_cast<std::uint8_t
>(
task.storage->status.state),
167 std::memory_order_relaxed);
168 observed_release_count_.store(
task.storage->status.release_count,
169 std::memory_order_relaxed);
170 current_entry_.store(
nullptr, std::memory_order_relaxed);
171 current_mapping_.store(UINT32_MAX, std::memory_order_relaxed);
172 current_instruction_.store(0, std::memory_order_relaxed);
174 skip_instruction_valid_ =
false;
175 runtime.debug_hooks_ =
this;
176 for(
const std::uint16_t index :
task.info->mappings)
177 resource->mappings[index].instance.set_debug_hooks(
this, index);
179 publish_snapshot(snapshot_initial);
188 std::uint8_t expected = watch_plan_open;
189 if(!watch_plan_state_.compare_exchange_strong(
190 expected, watch_plan_editing, std::memory_order_acq_rel,
191 std::memory_order_relaxed))
192 return expected == watch_plan_frozen ||
193 expected == watch_plan_freeze_pending
197 std::uint8_t editing = watch_plan_editing;
198 if(!watch_plan_state_.compare_exchange_strong(
199 editing, watch_plan_open, std::memory_order_release,
200 std::memory_order_relaxed))
201 watch_plan_state_.store(watch_plan_frozen,
202 std::memory_order_release);
205 for(
const Watch &watch : watches_)
207 if(watches_.size() >= options_.max_watch_symbols ||
208 watches_.size() >= publish_entry_capacity_)
211 ConfigurationRuntime::MappingRuntime *watch_mapping =
212 symbol ==
nullptr ? nullptr : find_mapping(*symbol);
213 if(watch_mapping ==
nullptr)
215 const std::size_t
word = value_word_count_ + header_words +
216 sfc_state_word_count_;
217 const std::size_t words = (symbol->
size + 7U) / 8U;
218 if(
word + words > publish_word_capacity_ - breakpoint_word_count_)
221 watch.symbol = symbol;
222 watch.mapping = watch_mapping;
224 watches_.push_back(watch);
228 entry.
offset =
static_cast<std::uint32_t
>(value_bytes_);
230 value_word_count_ += words;
231 value_bytes_ += symbol->
size;
232 snapshot_ready_.store(
false, std::memory_order_release);
241 if(resource_ ==
nullptr || task_ ==
nullptr)
244 id = invalid_instruction;
245 std::vector<std::uint32_t> keys;
246 keys.reserve(task_->info->mappings.size());
247 for(
const std::uint16_t mapping : task_->info->mappings) {
249 find_source(mapping, source, line, column);
250 if(entry ==
nullptr)
continue;
251 const std::uint32_t key = breakpoint_key(mapping, *entry);
253 for(
const std::uint32_t candidate : keys)
261 std::size_t additions = 0;
262 for(std::size_t index = 0; index < keys.size(); ++index) {
263 bool present =
false;
264 for(
const std::uint32_t existing : breakpoints_)
265 present = present || existing == keys[index];
266 if(!present) ++additions;
268 if(additions > options_.max_breakpoints - breakpoints_.size())
270 for(std::size_t index = 0; index < keys.size(); ++index) {
271 bool present =
false;
272 for(
const std::uint32_t existing : breakpoints_)
273 present = present || existing == keys[index];
274 if(present)
continue;
275 breakpoints_.push_back(keys[index]);
276 set_breakpoint(keys[index]);
286 if(resource_ ==
nullptr || task_ ==
nullptr)
289 std::size_t match_mapping = 0;
290 for(
const std::uint16_t mapping : task_->info->mappings) {
292 .program->source_map.entries) {
293 if(entry.instruction != instruction)
continue;
296 match_mapping = mapping;
299 return match ==
nullptr
301 : add_breakpoint_entry(*match, match_mapping,
id);
305 std::uint32_t column)
309 if(resource_ ==
nullptr || task_ ==
nullptr)
311 bool removed =
false;
312 for(
const std::uint16_t mapping : task_->info->mappings) {
314 find_source(mapping, source, line, column);
315 if(entry ==
nullptr)
continue;
316 const std::uint32_t key = breakpoint_key(mapping, *entry);
317 for(std::size_t index = 0; index < breakpoints_.size(); ++index) {
318 if(breakpoints_[index] == key) {
320 breakpoints_.begin() +
321 static_cast<std::ptrdiff_t
>(index));
322 clear_breakpoint(key);
334 return runtime_ ==
nullptr ? 0 : breakpoints_.size();
340 if(!stop_pending_.load(std::memory_order_acquire))
342 for(std::size_t attempt = 0; attempt < 8; ++attempt) {
343 const std::uint64_t before =
344 published_stop_.sequence.load(std::memory_order_acquire);
345 if((before & 1U) != 0)
continue;
346 const std::string *
task =
347 published_stop_.task.load(std::memory_order_relaxed);
348 const std::string *
pou =
349 published_stop_.pou.load(std::memory_order_relaxed);
350 const std::string *source =
351 published_stop_.source.load(std::memory_order_relaxed);
352 const std::string *
sfc =
353 published_stop_.sfc.load(std::memory_order_relaxed);
356 published_stop_.reason.load(std::memory_order_relaxed));
357 candidate.
task =
task ==
nullptr ? std::string_view{} : *
task;
358 candidate.
pou =
pou ==
nullptr ? std::string_view{} : *
pou;
360 source ==
nullptr ? std::string_view{} : *source;
361 candidate.
sfc =
sfc ==
nullptr ? std::string_view{} : *
sfc;
363 published_stop_.line.load(std::memory_order_relaxed);
365 published_stop_.column.load(std::memory_order_relaxed);
367 published_stop_.instruction.load(std::memory_order_relaxed);
369 published_stop_.artifact.load(std::memory_order_relaxed);
371 published_stop_.mapping.load(std::memory_order_relaxed);
373 published_stop_.region.load(std::memory_order_relaxed);
375 published_stop_.offset.load(std::memory_order_relaxed);
378 std::memory_order_relaxed));
380 published_stop_.call_depth.load(std::memory_order_relaxed);
381 const std::uint64_t after =
382 published_stop_.sequence.load(std::memory_order_acquire);
383 if(before != after || (after & 1U) != 0)
continue;
395 if(
static_cast<TaskState>(observed_task_state_.load(
399 observed_task_state_.load(std::memory_order_acquire));
401 command_mailbox_.store(
static_cast<std::uint8_t
>(command) + 1U,
402 std::memory_order_release);
405 if(!stop_pending_.load(std::memory_order_acquire) ||
408 command_mailbox_.store(
static_cast<std::uint8_t
>(command) + 1U,
409 std::memory_order_release);
415 std::size_t entry_capacity,
416 unsigned char *values,
417 std::size_t value_capacity,
418 std::size_t retries)
const noexcept
421 if(runtime_ ==
nullptr || entries ==
nullptr || values ==
nullptr)
423 if(entry_capacity < watches_.size() || value_capacity < value_bytes_)
425 if(!snapshot_ready_.load(std::memory_order_acquire))
428 for(std::size_t attempt = 0; attempt < retries; ++attempt) {
429 const std::uint64_t before =
430 publish_words_[0].load(std::memory_order_acquire);
431 if((before & 1U) != 0)
continue;
432 const std::uint64_t state_bits =
433 publish_words_[1].load(std::memory_order_acquire);
434 const std::uint64_t
fault =
435 publish_words_[2].load(std::memory_order_acquire);
437 published_entry_.load(std::memory_order_relaxed);
438 const std::uint32_t active_mapping =
static_cast<std::uint32_t
>(
439 publish_words_[4].load(std::memory_order_acquire));
440 const std::uint32_t active_instruction =
441 static_cast<std::uint32_t
>(
442 publish_words_[5].load(std::memory_order_acquire));
443 const std::uint8_t state =
static_cast<std::uint8_t
>(state_bits);
444 std::array<std::uint64_t,
446 for(std::size_t
word = 0;
word < sfc_state_word_count_; ++
word)
447 sfc_bits[
word] = publish_words_[header_words +
word].load(
448 std::memory_order_relaxed);
449 std::size_t destination = 0;
450 for(std::size_t index = 0; index < watches_.size(); ++index) {
451 entries[index] = publish_entries_[index];
452 const Watch &watch = watches_[index];
453 std::size_t remaining = watch.symbol->
size;
454 std::size_t
word = watch.word;
455 while(remaining != 0) {
456 const std::uint64_t bits =
457 publish_words_[
word++].load(std::memory_order_relaxed);
458 const std::size_t bytes = std::min<std::size_t>(8, remaining);
459 std::memcpy(values + destination, &bits, bytes);
460 destination += bytes;
464 const std::uint64_t after =
465 publish_words_[0].load(std::memory_order_acquire);
466 if(before != after || (after & 1U) != 0)
continue;
467 snapshot.version = after / 2U;
468 snapshot.value_count = watches_.size();
469 snapshot.value_bytes = value_bytes_;
470 snapshot.task_state =
static_cast<TaskState>(state);
472 if(active !=
nullptr) {
473 std::string_view path = active->call_path;
474 std::size_t begin = 0;
475 while(begin < path.size() &&
477 const std::size_t
slash = path.find(
'/', begin);
478 const std::size_t end =
slash == std::string_view::npos
481 snapshot.call_stack[snapshot.active_pou_count++].pou =
482 path.substr(begin, end - begin);
483 if(
slash == std::string_view::npos)
break;
486 if(snapshot.active_pou_count == 0) {
487 snapshot.call_stack[0].pou = active->pou;
488 snapshot.active_pou_count = 1;
491 snapshot.call_stack[snapshot.active_pou_count - 1U];
494 leaf.
sfc = active->sfc;
495 leaf.
line = active->line;
496 leaf.
column = active->column;
500 snapshot.call_depth =
static_cast<std::uint16_t
>(
501 snapshot.active_pou_count);
503 for(std::size_t index = 0;
504 index < snapshot_sfc_steps_.size(); ++index) {
505 const SnapshotSfcStep &published = snapshot_sfc_steps_[index];
507 snapshot.sfc_steps[snapshot.sfc_step_count++];
509 step.step_id = published.step_id;
510 step.network = *published.network;
511 step.step = *published.step;
512 step.mapping = published.mapping;
514 (sfc_bits[index / 64U] &
515 (std::uint64_t{1} << (index % 64U))) != 0;
523 std::size_t)
const noexcept
531 if(resource_ ==
nullptr || task_ ==
nullptr)
534 if(symbol ==
nullptr || !symbol->
located)
538 const int physical_index = resource_->image.located_index(*symbol);
540 const std::uint64_t target_release =
541 observed_release_count_.load(std::memory_order_acquire) +
542 (
static_cast<TaskState>(observed_task_state_.load(
544 std::uint64_t queue_version = 0;
546 static_cast<std::size_t
>(physical_index),
type,
547 static_cast<const unsigned char *
>(value), size,
548 static_cast<std::uint32_t
>(task_index_), target_release,
563 if(resource_ ==
nullptr || task_ ==
nullptr)
566 if(symbol ==
nullptr || !symbol->
located)
568 const int physical_index = resource_->image.located_index(*symbol);
570 const std::uint64_t target_release =
571 observed_release_count_.load(std::memory_order_acquire) +
572 (
static_cast<TaskState>(observed_task_state_.load(
574 std::uint64_t queue_version = 0;
575 if(resource_->image.queue_release_index(
576 static_cast<std::size_t
>(physical_index),
577 static_cast<std::uint32_t
>(task_index_), target_release,
591 if(runtime_ ==
nullptr || records ==
nullptr)
593 for(std::size_t attempt = 0; attempt < 8; ++attempt) {
594 const std::uint64_t before =
595 trace_lock_.load(std::memory_order_acquire);
596 if((before & 1U) != 0)
continue;
597 const std::size_t size =
598 trace_size_.load(std::memory_order_acquire);
599 const std::size_t begin =
600 trace_begin_.load(std::memory_order_acquire);
601 const std::size_t written = std::min(capacity, size);
602 const std::size_t skip = size - written;
603 for(std::size_t index = 0; index < written; ++index) {
604 const std::size_t logical = skip + index;
605 const std::size_t slot = (begin + logical) %
606 trace_storage_capacity_;
607 for(std::size_t
word = 0;
word < trace_words_per_record_;
609 const std::uint64_t bits = trace_words_[
610 slot * trace_words_per_record_ +
word]
611 .load(std::memory_order_relaxed);
612 std::memcpy(
reinterpret_cast<unsigned char *
>(
615 &bits,
sizeof(bits));
618 const std::uint64_t dropped =
619 trace_dropped_.load(std::memory_order_acquire);
620 const std::uint64_t after =
621 trace_lock_.load(std::memory_order_acquire);
622 if(before != after || (after & 1U) != 0)
continue;
623 report.version = after / 2U;
624 report.written = written;
625 report.dropped = dropped;
632 std::uint32_t instruction)
noexcept override
634 if(!mapping_selected(mapping) || program_ ==
nullptr)
return false;
635 current_mapping_.store(
static_cast<std::uint32_t
>(mapping),
636 std::memory_order_relaxed);
637 current_instruction_.store(instruction, std::memory_order_relaxed);
638 current_entry_.store(&entry, std::memory_order_release);
640 instruction_id.
mapping =
static_cast<std::uint32_t
>(mapping);
641 if(entry.sfc.empty()) {
651 if(skip_instruction_valid_ &&
652 instruction_id == skip_instruction_once_) {
653 skip_instruction_valid_ =
false;
656 bool stop = breakpoint_set(breakpoint_key(mapping, entry));
662 stop = entry.call_depth <= step_origin_depth_;
664 stop = entry.call_depth < step_origin_depth_;
668 if(!stop)
return false;
669 rt_stop_entry_ = &entry;
670 rt_stop_instruction_ = instruction;
671 rt_stop_reason_ = reason;
672 rt_stop_instruction_id_ = instruction_id;
673 rt_stop_depth_ = entry.call_depth;
679 bool faulted)
noexcept override
681 if(resource != resource_ ||
task != task_index_)
return;
682 bool publish =
false;
685 watch_plan_state_.load(std::memory_order_acquire);
686 if(state == watch_plan_frozen) {
690 if(state == watch_plan_freeze_pending)
break;
691 const std::uint8_t desired = state == watch_plan_open
693 : watch_plan_freeze_pending;
694 if(watch_plan_state_.compare_exchange_weak(
695 state, desired, std::memory_order_acq_rel,
696 std::memory_order_acquire)) {
697 publish = desired == watch_plan_frozen;
701 if(!
faulted) current_entry_.store(
nullptr, std::memory_order_release);
703 publish_snapshot(
faulted ? snapshot_fault : snapshot_committed);
704 observed_task_state_.store(
705 static_cast<std::uint8_t
>(task_->storage->status.state),
706 std::memory_order_release);
707 observed_release_count_.store(task_->storage->status.release_count,
708 std::memory_order_release);
714 std::uint32_t instruction,
718 if(resource != resource_ ||
task != task_index_)
return;
719 observed_task_state_.store(
720 static_cast<std::uint8_t
>(task_->storage->status.state),
721 std::memory_order_release);
722 observed_release_count_.store(task_->storage->status.release_count,
723 std::memory_order_release);
726 if(rt_stop_entry_ !=
nullptr) {
727 publish_stop(rt_stop_reason_, *rt_stop_entry_,
728 rt_stop_instruction_, rt_stop_instruction_id_);
729 stop_pending_.store(
true, std::memory_order_release);
730 rt_stop_entry_ =
nullptr;
732 publish_snapshot(snapshot_paused);
734 push_trace(kind, instruction, instruction_id, entry);
739 if(task_ ==
nullptr)
return;
741 const std::uint8_t packed =
742 command_mailbox_.exchange(0, std::memory_order_acq_rel);
744 skip_instruction_once_ = rt_stop_instruction_id_;
745 skip_instruction_valid_ =
true;
747 step_origin_depth_ = rt_stop_depth_;
748 stop_pending_.store(
false, std::memory_order_release);
751 stop_pending_.store(
false, std::memory_order_release);
754 observed_task_state_.store(
static_cast<std::uint8_t
>(status.
state),
755 std::memory_order_release);
757 std::memory_order_release);
767 publish_words_ =
nullptr;
768 publish_entries_ =
nullptr;
769 trace_words_ =
nullptr;
770 stop_pending_.store(
false, std::memory_order_release);
771 command_mailbox_.store(0, std::memory_order_release);
772 current_entry_.store(
nullptr, std::memory_order_release);
773 reset_detached_state();
777 static constexpr std::size_t header_words = 6;
778 static constexpr std::uint32_t invalid_instruction = UINT32_MAX;
779 static constexpr std::uint8_t watch_plan_open = 0;
780 static constexpr std::uint8_t watch_plan_editing = 1;
781 static constexpr std::uint8_t watch_plan_freeze_pending = 2;
782 static constexpr std::uint8_t watch_plan_frozen = 3;
783 static constexpr std::uint8_t snapshot_initial = 0;
784 static constexpr std::uint8_t snapshot_committed = 1;
785 static constexpr std::uint8_t snapshot_paused = 2;
786 static constexpr std::uint8_t snapshot_fault = 3;
790 const std::string *name =
nullptr;
791 std::uint32_t mapping = 0;
793 struct SnapshotSfcStep
795 ConfigurationRuntime::MappingRuntime *mapping_runtime =
nullptr;
796 std::size_t network_index = 0;
797 std::size_t step_index = 0;
798 SymbolId network_id{};
800 const std::string *network =
nullptr;
801 const std::string *step =
nullptr;
802 std::uint32_t mapping = 0;
806 const SymbolInfo *symbol =
nullptr;
807 ConfigurationRuntime::MappingRuntime *mapping =
nullptr;
808 std::size_t word = 0;
812 std::atomic<std::uint64_t> sequence{0};
813 std::atomic<const std::string *> task{
nullptr};
814 std::atomic<const std::string *> pou{
nullptr};
815 std::atomic<const std::string *> source{
nullptr};
816 std::atomic<const std::string *> sfc{
nullptr};
817 std::atomic<std::uint32_t> line{0};
818 std::atomic<std::uint32_t> column{0};
819 std::atomic<std::uint32_t> instruction{0};
820 std::atomic<std::uint32_t> artifact{0};
821 std::atomic<std::uint32_t> mapping{UINT32_MAX};
822 std::atomic<std::uint32_t> region{0};
823 std::atomic<std::uint32_t> offset{0};
824 std::atomic<std::uint16_t> call_depth{0};
825 std::atomic<std::uint8_t> reason{0};
826 std::atomic<std::uint8_t> region_kind{0};
829 void detach() noexcept
831 if(runtime_ !=
nullptr && resource_ !=
nullptr && task_ !=
nullptr) {
832 for(
const std::uint16_t index : task_->info->mappings)
833 resource_->mappings[index].instance.set_debug_hooks(
nullptr,
836 if(runtime_ !=
nullptr && runtime_->debug_hooks_ ==
this)
837 runtime_->debug_hooks_ =
nullptr;
843 publish_words_ =
nullptr;
844 publish_entries_ =
nullptr;
845 trace_words_ =
nullptr;
846 stop_pending_.store(
false, std::memory_order_release);
847 command_mailbox_.store(0, std::memory_order_release);
848 current_entry_.store(
nullptr, std::memory_order_release);
849 reset_detached_state();
852 void reset_detached_state() noexcept
854 publish_word_capacity_ = 0;
855 publish_entry_capacity_ = 0;
856 trace_words_per_record_ = 0;
857 trace_storage_capacity_ = 0;
858 breakpoint_words_ =
nullptr;
859 breakpoint_word_count_ = 0;
860 value_word_count_ = 0;
862 mapping_breakpoint_bases_.clear();
863 breakpoints_.clear();
865 active_pous_.clear();
866 snapshot_sfc_steps_.clear();
867 sfc_state_word_count_ = 0;
868 current_mapping_.store(UINT32_MAX, std::memory_order_release);
869 current_instruction_.store(0, std::memory_order_release);
872 static bool same_name(std::string_view
left,
const char *
right)
noexcept
874 if(
right ==
nullptr ||
left.size() != std::strlen(
right))
return false;
875 for(std::size_t index = 0; index <
left.size(); ++index) {
876 char a =
left[index];
char b =
right[index];
877 if(a >=
'A' && a <=
'Z') a =
static_cast<char>(a -
'A' +
'a');
878 if(b >=
'A' && b <=
'Z') b =
static_cast<char>(b -
'A' +
'a');
879 if(a != b)
return false;
884 const SymbolInfo *find_symbol(SymbolId
id)
const noexcept
886 if(runtime_ ==
nullptr ||
id.value == 0)
return nullptr;
887 for(
const SymbolInfo &symbol : runtime_->program_->symbols)
888 if(symbol.id ==
id)
return &symbol;
892 ConfigurationRuntime::MappingRuntime *find_mapping(
893 const SymbolInfo &symbol)
const noexcept
895 if(resource_ ==
nullptr || task_ ==
nullptr)
return nullptr;
896 for(
const std::uint16_t index : task_->info->mappings) {
897 if(index < resource_->mapping_count &&
898 resource_->mappings[index].info->lower == symbol.instance)
899 return &resource_->mappings[index];
904 const SourceMapEntry *find_source(std::size_t mapping,
905 const char *source, std::uint32_t line,
906 std::uint32_t column)
const noexcept
908 if(!mapping_selected(mapping))
return nullptr;
909 const SourceMapEntry *first =
nullptr;
910 const SourceMapEntry *best =
nullptr;
911 for(
const SourceMapEntry &entry :
912 resource_->mappings[mapping].program->source_map.entries) {
913 if(entry.line != line || !same_name(entry.source_name, source))
915 if(first ==
nullptr || entry.column < first->column) first = &entry;
916 if(entry.column <= column &&
917 (best ==
nullptr || entry.column > best->column))
920 return best ==
nullptr ? first : best;
923 bool mapping_selected(std::size_t mapping)
const noexcept
925 return mapping < mapping_breakpoint_bases_.size() &&
926 mapping_breakpoint_bases_[mapping] != invalid_instruction;
929 std::uint32_t breakpoint_key(
930 std::size_t mapping,
const SourceMapEntry &entry)
const noexcept
932 return mapping_breakpoint_bases_[mapping] + entry.breakpoint_key;
935 DebugError add_breakpoint_entry(
const SourceMapEntry &entry,
939 const std::uint32_t key = breakpoint_key(mapping, entry);
940 for(
const std::uint32_t existing : breakpoints_) {
941 if(existing == key) {
946 if(breakpoints_.size() >= options_.max_breakpoints)
948 breakpoints_.push_back(key);
955 std::uint32_t instruction,
956 const InstructionId &instruction_id)
noexcept
958 const std::uint64_t prior =
959 published_stop_.sequence.load(std::memory_order_relaxed);
960 published_stop_.sequence.store(prior | 1U,
961 std::memory_order_release);
962 published_stop_.task.store(&task_->info->lower,
963 std::memory_order_relaxed);
964 published_stop_.pou.store(&entry.pou, std::memory_order_relaxed);
965 published_stop_.source.store(&entry.source_name,
966 std::memory_order_relaxed);
967 published_stop_.sfc.store(&entry.sfc, std::memory_order_relaxed);
968 published_stop_.line.store(entry.line, std::memory_order_relaxed);
969 published_stop_.column.store(entry.column,
970 std::memory_order_relaxed);
971 published_stop_.instruction.store(instruction,
972 std::memory_order_relaxed);
973 published_stop_.artifact.store(instruction_id.artifact,
974 std::memory_order_relaxed);
975 published_stop_.mapping.store(instruction_id.mapping,
976 std::memory_order_relaxed);
977 published_stop_.region.store(instruction_id.region,
978 std::memory_order_relaxed);
979 published_stop_.offset.store(instruction_id.offset,
980 std::memory_order_relaxed);
981 published_stop_.call_depth.store(entry.call_depth,
982 std::memory_order_relaxed);
983 published_stop_.reason.store(
static_cast<std::uint8_t
>(reason),
984 std::memory_order_relaxed);
985 published_stop_.region_kind.store(
986 static_cast<std::uint8_t
>(instruction_id.region_kind),
987 std::memory_order_relaxed);
988 published_stop_.sequence.store((prior | 1U) + 1U,
989 std::memory_order_release);
992 void publish_snapshot(std::uint8_t snapshot_kind)
noexcept
994 if(publish_words_ ==
nullptr || publish_word_capacity_ < header_words)
996 const std::uint64_t prior =
997 publish_words_[0].load(std::memory_order_relaxed);
998 publish_words_[0].store(prior | 1U, std::memory_order_release);
999 publish_words_[1].store(
1000 static_cast<std::uint64_t
>(task_->storage->status.state) |
1001 (
static_cast<std::uint64_t
>(snapshot_kind) << 8U),
1002 std::memory_order_relaxed);
1003 publish_words_[2].store(
1004 static_cast<std::uint64_t
>(task_->storage->status.fault),
1005 std::memory_order_relaxed);
1006 const SourceMapEntry *
active =
1007 current_entry_.load(std::memory_order_acquire);
1008 published_entry_.store(active, std::memory_order_relaxed);
1009 publish_words_[4].store(
1010 current_mapping_.load(std::memory_order_relaxed),
1011 std::memory_order_relaxed);
1012 publish_words_[5].store(
1013 current_instruction_.load(std::memory_order_relaxed),
1014 std::memory_order_relaxed);
1015 for(std::size_t
word = 0;
word < sfc_state_word_count_; ++
word) {
1016 std::uint64_t bits = 0;
1017 const std::size_t begin =
word * 64U;
1018 const std::size_t end = std::min(
1019 begin + 64U, snapshot_sfc_steps_.size());
1020 for(std::size_t index = begin; index < end; ++index) {
1021 const SnapshotSfcStep &
step = snapshot_sfc_steps_[index];
1022 if(
step.mapping_runtime->instance
1023 .debug_sfc_committed_step_active(
1024 step.network_index,
step.step_index))
1025 bits |= std::uint64_t{1} << (index - begin);
1027 publish_words_[header_words +
word].store(
1028 bits, std::memory_order_relaxed);
1030 for(
const Watch &watch : watches_) {
1031 std::size_t remaining = watch.symbol->size;
1032 std::size_t source = 0;
1033 std::size_t
word = watch.word;
1034 while(remaining != 0) {
1035 std::uint64_t bits = 0;
1036 const std::size_t
count = std::min<std::size_t>(8, remaining);
1037 if(!watch.mapping->instance.read_variable(
1038 watch.symbol->offset +
1039 static_cast<std::uint32_t
>(source),
1040 static_cast<std::uint32_t
>(
count),
1041 reinterpret_cast<unsigned char *
>(&bits)))
1043 publish_words_[
word++].store(bits, std::memory_order_relaxed);
1048 publish_words_[0].store((prior | 1U) + 1U,
1049 std::memory_order_release);
1050 snapshot_ready_.store(
true, std::memory_order_release);
1053 bool breakpoint_set(std::uint32_t key)
const noexcept
1055 const std::size_t
word = key / 64U;
1056 return word < breakpoint_word_count_ &&
1057 (breakpoint_words_[
word].load(std::memory_order_acquire) &
1058 (std::uint64_t{1} << (key % 64U))) != 0;
1061 void set_breakpoint(std::uint32_t key)
noexcept
1063 breakpoint_words_[key / 64U].fetch_or(
1064 std::uint64_t{1} << (key % 64U), std::memory_order_release);
1067 void clear_breakpoint(std::uint32_t key)
noexcept
1069 breakpoint_words_[key / 64U].fetch_and(
1070 ~(std::uint64_t{1} << (key % 64U)),
1071 std::memory_order_release);
1075 std::uint32_t instruction = 0,
1076 const InstructionId *instruction_id =
nullptr,
1077 const SourceMapEntry *entry =
nullptr) noexcept
1079 if(trace_words_ ==
nullptr || trace_storage_capacity_ == 0)
return;
1080 const std::uint64_t prior =
1081 trace_lock_.load(std::memory_order_relaxed);
1082 trace_lock_.store(prior | 1U, std::memory_order_release);
1083 std::size_t size = trace_size_.load(std::memory_order_relaxed);
1084 std::size_t begin = trace_begin_.load(std::memory_order_relaxed);
1085 std::size_t slot = 0;
1086 if(size < trace_storage_capacity_) {
1087 slot = (begin + size) % trace_storage_capacity_;
1091 begin = (begin + 1U) % trace_storage_capacity_;
1092 trace_dropped_.fetch_add(1, std::memory_order_relaxed);
1094 DebugTraceRecord record{};
1095 record.sequence = trace_sequence_.fetch_add(
1096 1, std::memory_order_relaxed) + 1U;
1097 record.resource_id = trace_resource_id_;
1098 record.task_id = trace_task_id_;
1100 observed_release_count_.load(std::memory_order_relaxed);
1101 if(entry !=
nullptr) {
1102 record.pou_id = entry->pou_id;
1103 record.sfc_id = entry->sfc_id;
1106 record.instruction = instruction;
1107 record.instruction_id = instruction_id ==
nullptr
1110 for(std::size_t
word = 0;
word < trace_words_per_record_; ++
word) {
1111 std::uint64_t bits = 0;
1113 reinterpret_cast<const unsigned char *
>(&record) +
1114 word *
sizeof(bits),
1116 trace_words_[slot * trace_words_per_record_ +
word].store(
1117 bits, std::memory_order_relaxed);
1119 trace_begin_.store(begin, std::memory_order_relaxed);
1120 trace_size_.store(size, std::memory_order_relaxed);
1121 trace_lock_.store((prior | 1U) + 1U, std::memory_order_release);
1124 ConfigurationRuntime *runtime_ =
nullptr;
1125 ConfigurationRuntime::ResourceRuntime *resource_ =
nullptr;
1126 ConfigurationRuntime::TaskRuntime *task_ =
nullptr;
1127 ConfigurationRuntime::MappingRuntime *mapping_ =
nullptr;
1128 const Program *program_ =
nullptr;
1129 std::size_t task_index_ = 0;
1130 std::size_t mapping_index_ = 0;
1131 std::atomic<std::uint64_t> *publish_words_ =
nullptr;
1132 std::size_t publish_word_capacity_ = 0;
1133 DebugSnapshotEntry *publish_entries_ =
nullptr;
1134 std::size_t publish_entry_capacity_ = 0;
1135 std::vector<Watch> watches_;
1136 std::vector<ActivePou> active_pous_;
1137 std::vector<SnapshotSfcStep> snapshot_sfc_steps_;
1138 std::size_t sfc_state_word_count_ = 0;
1139 std::atomic<std::uint8_t> watch_plan_state_{watch_plan_open};
1140 std::atomic<bool> snapshot_ready_{
false};
1141 std::vector<std::uint32_t> breakpoints_;
1142 std::vector<std::uint32_t> mapping_breakpoint_bases_;
1143 std::atomic<std::uint64_t> *breakpoint_words_ =
nullptr;
1144 std::size_t breakpoint_word_count_ = 0;
1145 std::size_t value_word_count_ = 0;
1146 std::size_t value_bytes_ = 0;
1147 std::atomic<std::uint64_t> *trace_words_ =
nullptr;
1148 std::size_t trace_words_per_record_ = 0;
1149 std::size_t trace_storage_capacity_ = 0;
1150 mutable std::atomic<std::uint64_t> trace_lock_{0};
1151 std::atomic<std::size_t> trace_begin_{0};
1152 std::atomic<std::size_t> trace_size_{0};
1153 std::atomic<std::uint64_t> trace_sequence_{0};
1154 std::atomic<std::uint64_t> trace_dropped_{0};
1155 std::uint64_t trace_resource_id_ = 0;
1156 std::uint64_t trace_task_id_ = 0;
1157 DebugSessionOptions options_{};
1158 PublishedStop published_stop_{};
1159 std::atomic<bool> stop_pending_{
false};
1160 std::atomic<std::uint8_t> command_mailbox_{0};
1161 std::atomic<std::uint8_t> observed_task_state_{
1163 std::atomic<std::uint64_t> observed_release_count_{0};
1164 std::atomic<const SourceMapEntry *> published_entry_{
nullptr};
1165 std::atomic<const SourceMapEntry *> current_entry_{
nullptr};
1166 std::atomic<std::uint32_t> current_mapping_{UINT32_MAX};
1167 std::atomic<std::uint32_t> current_instruction_{0};
1169 std::uint16_t step_origin_depth_ = 0;
1170 InstructionId rt_stop_instruction_id_{};
1171 const SourceMapEntry *rt_stop_entry_ =
nullptr;
1172 std::uint32_t rt_stop_instruction_ = 0;
1174 std::uint16_t rt_stop_depth_ = 0;
1175 InstructionId skip_instruction_once_{};
1176 bool skip_instruction_valid_ =
false;