Esempio n. 1
0
int main(int argc, char **argv)
{
    g_fd = open(argv[1], O_RDWR);
    if (errno == ENOENT && g_fd < 0)
    {
        int i;
        for (i = 0; i < 120; i++)
        {
            g_fd = open(argv[1], O_RDWR);
            if (g_fd >= 0)
                break;
            printf("."); fflush(stdout);
            usleep(500000);
        }
        printf("\n");
    }
    if (g_fd < 0)
        return bitch("open");

//    reset();
//    set_config(0);
//    set_config(1);

    usb_set_connected(0, 1);
    claim_if(0);

#if 0
    msd();
#else
    palm();
#endif
    return 0;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
	int i;
	int total = 13;
	msd(input, 0, total-1, 0);
	printf("MSD sorted strings:\n");
	for(i=0; i<total; i++) {
		printf("input[%d]=%s\n", i, input[i]);
	}
	return 0;
}
Esempio n. 3
0
int main(int argc, char **argv)
{
    g_fd = open(argv[1], O_RDWR);
    if (g_fd < 0)
        return bitch("open");

    reset();

    usb_set_connected(0, 1);
    claim_if(0);

//    set_config(1); - the culprit!
    set_interface(0, 0);

    msd();
    return 0;
}
Esempio n. 4
0
// This algorithm is based on quick sort + counting sort
void msd(char **input, int low, int high, int digit)
{
	int i, r;
	if(high <= low) return;
	int count[256+2] = {0};
	char *aux[256]   = {NULL};
	for (i=low; i<=high; i++) // Compute frequency
		count[input[i][digit] + 2]++;

	for (i=0; i<256+1; i++)   // Transform counts to indices
		count[i+1] += count[i];

	for (i=low; i<=high; i++) // Distribute
		aux[count[input[i][digit] + 1]++] = input[i];

	for (i=low; i<=high; i++) // Copy back
		input[i] = aux[i - low];

	// Recursively sort for each character value
	for (r=0; r<256; r++)
		msd(input, low + count[r], low + count[r+1] - 1, digit+1);
}
Esempio n. 5
0
void AffinityLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
                                         const vector<bool>& propagate_down,
                                        const vector<Blob<Dtype>*>& bottom) {
  for (int bidx = 0; bidx < bottom.size(); ++bidx) {
    if (propagate_down[bidx]) {
      const Dtype* top_diff = top[bidx]->cpu_diff();
      Dtype* bottom_diff = bottom[bidx]->mutable_cpu_diff();
      const Dtype* min_data = min_index_[bidx]->cpu_diff();

      caffe_set(bottom[0]->count(), Dtype(0.0), bottom_diff);

      int inner_num = bottom[bidx]->width()
                  * bottom[bidx]->height();

      // Spread out the affinity losses to pixels
      for (int i = 0; i < bottom[0]->height() - 1; ++i) {
        for (int j = 0; j < bottom[0]->width() - 1; ++j) {
          Dtype lx = top_diff[i * bottom[0]->width() + j];
          Dtype ly = top_diff[inner_num + i * bottom[0]->width() + j];

          int mx = min_data[i * bottom[0]->width() + j];
          int my = min_data[bottom[0]->width()
              * bottom[0]->height() + i * bottom[0]->width() + j];

          // Only propagate to min index contributor of affinity graph
          bottom_diff[0 * inner_num + i * bottom[0]->width() + (j + mx)] -= lx;
          bottom_diff[0 * inner_num + (i + my) * bottom[0]->width() + j] -= ly;
          bottom_diff[1 * inner_num + i * bottom[0]->width() + (j + mx)] += lx;
          bottom_diff[1 * inner_num + (i + my) * bottom[0]->width() + j] += ly;
        }
      }
#ifdef CAFFE_AFFINITY_DEBUG
      {
        cv::Mat tmp;

        Dtype* prob_rd = bottom[bidx]->mutable_cpu_data();

        cv::Mat wrapped_prob(bottom[0]->height(), bottom[0]->width(),
                          cv::DataType<Dtype>::type,
                        prob_rd, sizeof(Dtype) * bottom[0]->width());
        cv::imshow("prob", wrapped_prob);

        cv::Mat wrapped_diff(bottom[0]->height(), bottom[0]->width(),
                          cv::DataType<Dtype>::type,
                        bottom_diff, sizeof(Dtype) * bottom[0]->width());

        Dtype sum = std::accumulate(bottom_diff,
                                    bottom_diff
                                    + bottom[0]->height() * bottom[0]->width(),
                                    0.0);

        Dtype mean = sum / (bottom[0]->width()*bottom[0]->height());

        std::vector<Dtype> msd(bottom[0]->height() * bottom[0]->width());
        std::transform(bottom_diff,
                       bottom_diff + (bottom[0]->height()*bottom[0]->width()),
                       msd.begin(), std::bind2nd(std::minus<Dtype>(), mean));

        Dtype sqsum = std::inner_product(msd.begin(),
                                         msd.end(), msd.begin(), 0.0);
        Dtype stdev = std::sqrt(sqsum / (bottom[0]->width()
            * bottom[0]->height()));

        wrapped_diff.convertTo(tmp, CV_32FC1, 1.0 / (2.0 * stdev),
            (stdev - mean) * 1.0 / (2.0 * stdev));

        cv::imshow("diff", tmp);
        cv::waitKey(2);
      }
#endif
    }
  }
}
Esempio n. 6
0
static void
executeOneCommand(JNIEnv* env) {
  switch (saCmdType) {
  case SA_CMD_SUSPEND_ALL: {
    if (suspended) {
      reportErrorToSA("Target process already suspended");
      return;
    }

    // We implement this by getting all of the threads and calling
    // SuspendThread on each one, except for the thread object
    // corresponding to this thread. Each thread for which the call
    // succeeded (i.e., did not return JVMDI_ERROR_INVALID_THREAD)
    // is added to a list which is remembered for later resumption.
    // Note that this currently has race conditions since a thread
    // might be started after we call GetAllThreads and since a
    // thread for which we got an error earlier might be resumed by
    // the VM while we are busy suspending other threads. We could
    // solve this by looping until there are no more threads we can
    // suspend, but a more robust and scalable solution is to add
    // this functionality to the JVMDI interface (i.e.,
    // "suspendAll"). Probably need to provide an exclude list for
    // such a routine.
    jint threadCount;
    jthread* threads;
    if (jvmdi->GetAllThreads(&threadCount, &threads) != JVMDI_ERROR_NONE) {
      reportErrorToSA("Error while getting thread list");
      return;
    }


    for (int i = 0; i < threadCount; i++) {
      jthread thr = threads[i];
      if (!env->IsSameObject(thr, debugThreadObj)) {
        jvmdiError err = jvmdi->SuspendThread(thr);
        if (err == JVMDI_ERROR_NONE) {
          // Remember this thread and do not free it
          suspendedThreads.push_back(thr);
          continue;
        } else {
          fprintf(stderr, " SA: Error %d while suspending thread\n", err);
          // FIXME: stop, resume all threads, report error
        }
      }
      env->DeleteGlobalRef(thr);
    }

    // Free up threads
    jvmdi->Deallocate((jbyte*) threads);

    // Suspension is complete
    suspended = true;
    break;
  }

  case SA_CMD_RESUME_ALL: {
    if (!suspended) {
      reportErrorToSA("Target process already suspended");
      return;
    }

    saCmdResult = 0;
    bool errorOccurred = false;
    jvmdiError firstError;
    for (int i = 0; i < suspendedThreads.size(); i++) {
      jthread thr = suspendedThreads[i];
      jvmdiError err = jvmdi->ResumeThread(thr);
      env->DeleteGlobalRef(thr);
      if (err != JVMDI_ERROR_NONE) {
        if (!errorOccurred) {
          errorOccurred = true;
          firstError = err;
        }
      }
    }
    suspendedThreads.clear();
    suspended = false;
    if (errorOccurred) {
      reportErrorToSA("Error %d while resuming threads", firstError);
      return;
    }
    break;
  }

  case SA_CMD_TOGGLE_BREAKPOINT: {
    saCmdBkptResWasError = 1;

    // Search line number info for all loaded classes
    jint classCount;
    jclass* classes;

    jvmdiError glcRes = jvmdi->GetLoadedClasses(&classCount, &classes);
    if (glcRes != JVMDI_ERROR_NONE) {
      reportErrorToSA("Error %d while getting loaded classes", glcRes);
      return;
    }
    JvmdiRefListDeallocator rld(env, (jobject*) classes, classCount);

    bool done = false;
    bool gotOne = false;
    jclass targetClass;
    jmethodID targetMethod;
    jlocation targetLocation;
    jint targetLineNumber;

    for (int i = 0; i < classCount && !done; i++) {
      fflush(stderr);
      jclass clazz = classes[i];
      char* srcName;
      jvmdiError sfnRes = jvmdi->GetSourceFileName(clazz, &srcName);
      if (sfnRes == JVMDI_ERROR_NONE) {
        JvmdiDeallocator de1(srcName);
        if (!strcmp(srcName, saCmdBkptSrcFileName)) {
          // Got a match. Now see whether the package name of the class also matches
          char* clazzName;
          jvmdiError sigRes = jvmdi->GetClassSignature(clazz, &clazzName);
          if (sigRes != JVMDI_ERROR_NONE) {
            reportErrorToSA("Error %d while getting a class's signature", sigRes);
            return;
          }
          JvmdiDeallocator de2(clazzName);
          if (packageNameMatches(clazzName + 1, saCmdBkptPkgName)) {
            // Iterate through all methods
            jint methodCount;
            jmethodID* methods;
            if (jvmdi->GetClassMethods(clazz, &methodCount, &methods) != JVMDI_ERROR_NONE) {
              reportErrorToSA("Error while getting methods of class %s", clazzName);
              return;
            }
            JvmdiDeallocator de3(methods);
            for (int j = 0; j < methodCount && !done; j++) {
              jmethodID m = methods[j];
              jint entryCount;
              JVMDI_line_number_entry* table;
              jvmdiError lnRes = jvmdi->GetLineNumberTable(clazz, m, &entryCount, &table);
              if (lnRes == JVMDI_ERROR_NONE) {
                JvmdiDeallocator de4(table);
                // Look for line number greater than or equal to requested line
                for (int k = 0; k < entryCount && !done; k++) {
                  JVMDI_line_number_entry& entry = table[k];
                  if (entry.line_number >= saCmdBkptLineNumber &&
                      (!gotOne || entry.line_number < targetLineNumber)) {
                    gotOne = true;
                    targetClass = clazz;
                    targetMethod = m;
                    targetLocation = entry.start_location;
                    targetLineNumber = entry.line_number;
                    done = (targetLineNumber == saCmdBkptLineNumber);
                  }
                }
              } else if (lnRes != JVMDI_ERROR_ABSENT_INFORMATION) {
                reportErrorToSA("Unexpected error %d while fetching line number table", lnRes);
                return;
              }
            }
          }
        }
      } else if (sfnRes != JVMDI_ERROR_ABSENT_INFORMATION) {
        reportErrorToSA("Unexpected error %d while fetching source file name", sfnRes);
        return;
      }
    }

    bool wasSet = true;
    if (gotOne) {
      // Really toggle this breakpoint
      jvmdiError bpRes;
      bpRes = jvmdi->SetBreakpoint(targetClass, targetMethod, targetLocation);
      if (bpRes == JVMDI_ERROR_DUPLICATE) {
        bpRes = jvmdi->ClearBreakpoint(targetClass, targetMethod, targetLocation);
        wasSet = false;
      }
      if (bpRes != JVMDI_ERROR_NONE) {
        reportErrorToSA("Unexpected error %d while setting or clearing breakpoint at bci %d, line %d",
                        bpRes, targetLocation, targetLineNumber);
        return;
      }
    } else {
      saCmdBkptResWasError = 0;
      reportErrorToSA("No debug information found covering this line");
      return;
    }

    // Provide result
    saCmdBkptResLineNumber = targetLineNumber;
    saCmdBkptResBCI        = targetLocation;
    saCmdBkptResWasSet     = (wasSet ? 1 : 0);
    {
      char* methodName;
      char* methodSig;
      if (jvmdi->GetMethodName(targetClass, targetMethod, &methodName, &methodSig)
          == JVMDI_ERROR_NONE) {
        JvmdiDeallocator mnd(methodName);
        JvmdiDeallocator msd(methodSig);
        strncpy(saCmdBkptResMethodName, methodName, SA_CMD_BUF_SIZE);
        strncpy(saCmdBkptResMethodSig,  methodSig, SA_CMD_BUF_SIZE);
      } else {
        strncpy(saCmdBkptResMethodName, "<error>", SA_CMD_BUF_SIZE);
        strncpy(saCmdBkptResMethodSig,  "<error>", SA_CMD_BUF_SIZE);
      }
    }
    break;
  }

  default:
    reportErrorToSA("Command %d not yet supported", saCmdType);
    return;
  }

  // Successful command execution
  saCmdResult = 0;
  saCmdPending = 0;
}