dfaf77
diff -up papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c.orig papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c
dfaf77
--- papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c.orig	2013-08-06 12:12:20.000000000 -0400
dfaf77
+++ papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c	2017-06-15 22:41:58.784904523 -0400
dfaf77
@@ -236,14 +236,15 @@ static int find_next_no_aliases(int code
dfaf77
         current_pmu++;
dfaf77
         SUBDBG("Incrementing PMU: %#x\n",current_pmu);
dfaf77
 
dfaf77
+        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
+        ret = pfm_get_pmu_info(current_pmu, &pinfo);
dfaf77
+
dfaf77
 	/* Off the end, so done iterating */
dfaf77
-        if (current_pmu>PFM_PMU_MAX) {
dfaf77
+        if (ret==PFM_ERR_INVAL) {
dfaf77
            return PFM_ERR_NOTFOUND;
dfaf77
         }
dfaf77
  
dfaf77
-        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
-        pfm_get_pmu_info(current_pmu, &pinfo);
dfaf77
-        if (pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
dfaf77
+        if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
dfaf77
      }
dfaf77
 
dfaf77
      current_event=pinfo.first_event;
dfaf77
@@ -533,12 +534,21 @@ get_event_first_active(int pmu_type)
dfaf77
 
dfaf77
   pmu_idx=0;
dfaf77
 
dfaf77
-  while(pmu_idx
dfaf77
+	/* We loop forever here and exit if pfm_get_pmu_info() fails.	*/
dfaf77
+	/* Before we only went up to PFM_PMU_MAX but this is set at	*/
dfaf77
+	/* compile time and might not reflect the number of PMUs if	*/
dfaf77
+	/* PAPI is dynamically linked against libpfm4.			*/
dfaf77
+
dfaf77
+  while(1) {
dfaf77
 
dfaf77
     /* clear the PMU structure (required by libpfm4) */
dfaf77
     memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
     ret=pfm_get_pmu_info(pmu_idx, &pinfo);
dfaf77
 
dfaf77
+    if (ret==PFM_ERR_INVAL) {
dfaf77
+	    break;
dfaf77
+    }
dfaf77
+
dfaf77
     if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) {
dfaf77
 
dfaf77
       pidx=pinfo.first_event;
dfaf77
@@ -1372,14 +1382,23 @@ _pe_libpfm4_init(papi_vector_t *my_vecto
dfaf77
    retval=pfm_get_pmu_info(0, &(event_table->default_pmu));
dfaf77
    
dfaf77
    SUBDBG("Detected pmus:\n");
dfaf77
-   for(i=0;i
dfaf77
+   i=0;
dfaf77
+   while(1) {
dfaf77
       memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
       retval=pfm_get_pmu_info(i, &pinfo);
dfaf77
-      if (retval!=PFM_SUCCESS) {
dfaf77
-	 continue;
dfaf77
+
dfaf77
+      /* We're done if we hit an invalid PMU entry			*/
dfaf77
+      /* We can't check against PFM_PMU_MAX				*/
dfaf77
+      /* as that might not match if libpfm4 is dynamically linked	*/
dfaf77
+
dfaf77
+      if (retval==PFM_ERR_INVAL) {
dfaf77
+	      break;
dfaf77
       }
dfaf77
 
dfaf77
-      if (pmu_is_present_and_right_type(&pinfo,pmu_type)) {
dfaf77
+      if ((retval==PFM_SUCCESS) && (pinfo.name != NULL) &&
dfaf77
+	(pmu_is_present_and_right_type(&pinfo,pmu_type))) {
dfaf77
+
dfaf77
+
dfaf77
 	 SUBDBG("\t%d %s %s %d\n",i,pinfo.name,pinfo.desc,pinfo.type);
dfaf77
 
dfaf77
          detected_pmus++;
dfaf77
@@ -1402,6 +1421,7 @@ _pe_libpfm4_init(papi_vector_t *my_vecto
dfaf77
 	       found_default=1;
dfaf77
 	 }
dfaf77
       }
dfaf77
+	i++;
dfaf77
    }
dfaf77
    SUBDBG("%d native events detected on %d pmus\n",ncnt,detected_pmus);
dfaf77
 
dfaf77
@@ -1421,7 +1441,7 @@ _pe_libpfm4_init(papi_vector_t *my_vecto
dfaf77
                                    event_table->default_pmu.num_fixed_cntrs;
dfaf77
 
dfaf77
    SUBDBG( "num_counters: %d\n", my_vector->cmp_info.num_cntrs );
dfaf77
-   
dfaf77
+
dfaf77
    /* Setup presets, only if Component 0 */
dfaf77
    if (cidx==0) {
dfaf77
       retval = _papi_load_preset_table( (char *)event_table->default_pmu.name, 
dfaf77
diff -up papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c.orig papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c
dfaf77
--- papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c.orig	2013-08-06 12:12:20.000000000 -0400
dfaf77
+++ papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c	2017-06-15 22:50:08.700238377 -0400
dfaf77
@@ -238,14 +238,15 @@ static int find_next_no_aliases(int code
dfaf77
         current_pmu++;
dfaf77
         SUBDBG("Incrementing PMU: %#x\n",current_pmu);
dfaf77
 
dfaf77
+        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
+        ret=pfm_get_pmu_info(current_pmu, &pinfo);
dfaf77
+
dfaf77
 	/* Off the end, so done iterating */
dfaf77
-        if (current_pmu>PFM_PMU_MAX) {
dfaf77
+        if (ret==PFM_ERR_INVAL) {
dfaf77
            return PFM_ERR_NOTFOUND;
dfaf77
         }
dfaf77
  
dfaf77
-        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
-        pfm_get_pmu_info(current_pmu, &pinfo);
dfaf77
-        if (pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
dfaf77
+        if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
dfaf77
      }
dfaf77
 
dfaf77
      current_event=pinfo.first_event;
dfaf77
@@ -531,12 +532,20 @@ get_event_first_active(int pmu_type)
dfaf77
 
dfaf77
   pmu_idx=0;
dfaf77
 
dfaf77
-  while(pmu_idx
dfaf77
+  /* We loop forever here and exit if pfm_get_pmu_info() fails.	*/
dfaf77
+  /* Before we only went up to PFM_PMU_MAX but this is set at	*/
dfaf77
+  /* compile time and might not reflect the number of PMUs if	*/
dfaf77
+  /* PAPI is dynamically linked against libpfm4.		*/
dfaf77
+  while(1) {
dfaf77
 
dfaf77
     /* clear the PMU structure (required by libpfm4) */
dfaf77
     memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
     ret=pfm_get_pmu_info(pmu_idx, &pinfo);
dfaf77
 
dfaf77
+    if (ret==PFM_ERR_INVAL) {
dfaf77
+	    break;
dfaf77
+    }
dfaf77
+
dfaf77
     if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) {
dfaf77
 
dfaf77
       pidx=pinfo.first_event;
dfaf77
@@ -1356,14 +1365,21 @@ _peu_libpfm4_init(papi_vector_t *my_vect
dfaf77
    my_vector->cmp_info.num_cntrs=0;
dfaf77
 
dfaf77
    SUBDBG("Detected pmus:\n");
dfaf77
-   for(i=0;i
dfaf77
+   i=0;
dfaf77
+   while(1) {
dfaf77
       memset(&pinfo,0,sizeof(pfm_pmu_info_t));
dfaf77
       retval=pfm_get_pmu_info(i, &pinfo);
dfaf77
-      if (retval!=PFM_SUCCESS) {
dfaf77
-	 continue;
dfaf77
+
dfaf77
+      /* We're done if we hit an invalid PMU entry                    */
dfaf77
+      /* We can't check against PFM_PMU_MAX                           */
dfaf77
+      /* as that might not match if libpfm4 is dynamically linked     */
dfaf77
+
dfaf77
+      if (retval==PFM_ERR_INVAL) {
dfaf77
+	      break;
dfaf77
       }
dfaf77
 
dfaf77
-      if (pmu_is_present_and_right_type(&pinfo,pmu_type)) {
dfaf77
+      if ((retval==PFM_SUCCESS) && (pinfo.name != NULL) &&
dfaf77
+	  (pmu_is_present_and_right_type(&pinfo,pmu_type))) {
dfaf77
 	 SUBDBG("\t%d %s %s %d\n",i,pinfo.name,pinfo.desc,pinfo.type);
dfaf77
 
dfaf77
          detected_pmus++;
dfaf77
@@ -1372,6 +1388,7 @@ _peu_libpfm4_init(papi_vector_t *my_vect
dfaf77
          my_vector->cmp_info.num_cntrs += pinfo.num_cntrs+
dfaf77
                                    pinfo.num_fixed_cntrs;
dfaf77
       }
dfaf77
+	i++;
dfaf77
    }
dfaf77
    SUBDBG("%d native events detected on %d pmus\n",ncnt,detected_pmus);
dfaf77