@@ -10,13 +10,9 @@ ProbeExplorer::ProbeExplorer(int argc, char** argv) {
1010 has_invalid_probes_ = false ;
1111 DC_LOG_TRACE (" ProbeExplorer::ProbeExplorer - end" );
1212}
13-
14- // Extracts probes based on configuration and exclusion file
15- std::vector<std::shared_ptr<Probe>> ProbeExplorer::extractProbes () {
16- DC_LOG_TRACE (" ProbeExplorer::extractProbes - start" );
13+ std::unordered_map<std::string, std::unordered_set<std::string>> ProbeExplorer::Extract_Exclusions () {
14+ DC_LOG_TRACE (" ProbeExplorer::validate_exclusion_file - start" );
1715 std::unordered_map<std::string, std::unordered_set<std::string>> exclusionMap;
18-
19- // Load exclusion map from file if specified
2016 if (!configManager_->probe_exclusion_file_path .empty () &&
2117 std::filesystem::exists (configManager_->probe_exclusion_file_path )) {
2218 std::ifstream ifs (configManager_->probe_exclusion_file_path );
@@ -27,7 +23,14 @@ std::vector<std::shared_ptr<Probe>> ProbeExplorer::extractProbes() {
2723 int arr_len = json_object_array_length (jobj);
2824 for (int i = 0 ; i < arr_len; ++i) {
2925 json_object* probe_obj = json_object_array_get_idx (jobj, i);
30- if (!probe_obj) continue ;
26+ if (!probe_obj) {
27+ DC_LOG_WARN (" the %dth element of exclusion file is missing (null pointer returned)." , i);
28+ continue ;
29+ }
30+ if (json_object_get_type (probe_obj) == json_type_null) {
31+ DC_LOG_WARN (" exclusion file contains explicit JSON null at %dth element." , i);
32+ continue ;
33+ }
3134 json_object* name_obj = nullptr ;
3235 json_object* funcs_obj = nullptr ;
3336 if (json_object_object_get_ex (probe_obj, " name" , &name_obj) &&
@@ -39,20 +42,40 @@ std::vector<std::shared_ptr<Probe>> ProbeExplorer::extractProbes() {
3942 int func_len = json_object_array_length (funcs_obj);
4043 for (int j = 0 ; j < func_len; ++j) {
4144 json_object* func_obj = json_object_array_get_idx (funcs_obj, j);
45+
4246 if (func_obj && json_object_get_type (func_obj) == json_type_string) {
47+ // check the function name
48+ std::string func_name = json_object_get_string (func_obj);
49+ if (func_name.find (' /' )!= std::string::npos || func_name.find (' \\ ' ) != std::string::npos || func_name.find (' ' ) != std::string::npos){
50+ DC_LOG_WARN (" Exclusion file contains invalid function name '%s' for probe '%s'. Skipping this function." ,
51+ func_name.c_str (), probe_name.c_str ());
52+ continue ;
53+ }
4354 func_set.insert (json_object_get_string (func_obj));
4455 }
4556 }
4657 exclusionMap[probe_name] = std::move (func_set);
58+ }else {
59+ DC_LOG_WARN (" Exclusion file entry at index %d is missing 'name' or 'functions' field, or they are of incorrect type." , i);
4760 }
4861 }
62+ }else {
63+ DC_LOG_WARN (" Exclusion file is not a valid JSON array." );
4964 }
5065 if (jobj) json_object_put (jobj);
5166 } else {
5267 DC_LOG_ERROR (" Failed to open exclusion probes file: %s" ,
5368 configManager_->probe_exclusion_file_path .string ().c_str ());
5469 }
5570 }
71+ return exclusionMap;
72+
73+ DC_LOG_TRACE (" ProbeExplorer::validate_exclusion_file - end" );
74+ }
75+ // Extracts probes based on configuration and exclusion file
76+ std::vector<std::shared_ptr<Probe>> ProbeExplorer::extractProbes () {
77+ DC_LOG_TRACE (" ProbeExplorer::extractProbes - start" );
78+ auto exclusionMap = Extract_Exclusions ();
5679
5780 // Log the contents of the exclusion map for debugging
5881 DC_LOG_DEBUG (" Exclusion Map Contents:" );
@@ -417,11 +440,67 @@ std::vector<std::shared_ptr<Probe>> ProbeExplorer::extractProbes() {
417440 DC_LOG_TRACE (" ProbeExplorer::extractProbes - end" );
418441 return probes;
419442}
443+ void ProbeExplorer::create_exclusion_file (std::vector<std::shared_ptr<Probe>> probes) {
444+ DC_LOG_TRACE (" ProbeExplorer::create_exclusion_file - start" );
445+ json_object* jexarray = json_object_new_array ();
446+ // Serialize each probe to JSON without functions
447+ for (const auto & probe : probes) {
448+ json_object* jexclude = nullptr ;
449+ switch (probe->type ) {
450+ case ProbeType::SYSCALLS :
451+ jexclude = std::dynamic_pointer_cast<SysCallProbe>(probe)->toJson (false );
452+ break ;
453+ case ProbeType::KPROBE :
454+ jexclude = std::dynamic_pointer_cast<KProbe>(probe)->toJson (false );
455+ break ;
456+ case ProbeType::UPROBE :
457+ jexclude = std::dynamic_pointer_cast<UProbe>(probe)->toJson (false );
458+ break ;
459+ case ProbeType::USDT :
460+ jexclude = std::dynamic_pointer_cast<USDTProbe>(probe)->toJson (false );
461+ break ;
462+ case ProbeType::CUSTOM :
463+ jexclude = std::dynamic_pointer_cast<CustomProbe>(probe)->toJson (false );
464+ break ;
465+ default :
466+ DC_LOG_ERROR (" Unknown probe type encountered." );
467+ continue ; // Skip unknown types
468+ }
469+ if (!jexclude) {
470+ DC_LOG_ERROR (" Failed to serialize probe for exclusion: %s" , probe->name .c_str ());
471+ continue ; // Skip serialization failure
472+ }
473+ json_object_array_add (jexarray, jexclude);
474+ }
475+ if (!configManager_->probe_exclusion_file_path .empty () &&
476+ !std::filesystem::exists (configManager_->probe_exclusion_file_path )) {
477+ const char * exclude_json_str = json_object_to_json_string_ext (jexarray, JSON_C_TO_STRING_PRETTY );
478+ std::ofstream ofs (configManager_->probe_exclusion_file_path );
479+ if (ofs.is_open ()) {
480+ ofs << exclude_json_str;
481+ ofs.close ();
482+ } else {
483+ DC_LOG_ERROR (" Failed to open file: %s" , configManager_->probe_exclusion_file_path .c_str ());
484+ }
485+ }
486+
487+ DC_LOG_TRACE (" ProbeExplorer::create_exclusion_file - end" );
488+ }
489+
490+
420491
421492// Writes extracted probes to a JSON file and returns the probe list
422493std::vector<std::shared_ptr<Probe>> ProbeExplorer::writeProbesToJson () {
423494 DC_LOG_TRACE (" ProbeExplorer::writeProbesToJson - start" );
424495 auto probes = extractProbes ();
496+ if (probes.empty ()) {
497+ DC_LOG_WARN (" No valid probes extracted. Skipping JSON write." );
498+ return probes;
499+ }
500+ if (!configManager_->probe_exclusion_file_path .empty () &&
501+ !std::filesystem::exists (configManager_->probe_exclusion_file_path )) {
502+ create_exclusion_file (probes);
503+ }
425504 json_object* jarray = json_object_new_array ();
426505
427506 // Serialize each probe to JSON
0 commit comments