1

I'm struggling with the recover problem. I'm running a while loop that's more than 7 thousand iterations long. I get a segmentation fault because (i'm pretty sure) of the last or perhaps the last but one iteration. I would like to debug that part. I do not know how to skip the non-relevant iterations, I don't even know if there is a way to do that with debug50.

I could press 'next' 7312 times, but life is short.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t BYTE;
FILE *pf;
FILE *img;

int main(int argc, char *argv[])
{
   if (argc != 2)
  {
    printf("Usage: ./recover image\n");
  }


 BYTE buffer[512];
 pf = fopen(argv[1], "r");
 int ct = 0;
 char fname[8];



  while(fread(buffer, 512, 1, pf) || (!feof(pf)))
  {


      if (buffer[0] == 0xff &&
          buffer[1] == 0xd8 &&
          buffer[2] == 0xff &&
          (buffer[3] & 0xf0) == 0xe0)
          {
              if (img != NULL)
              {
                  fclose(img);
                  ct += 1;
              }

              sprintf(fname,"%03i.jpg", ct);
              img = fopen(fname,"w");
              fwrite(buffer, sizeof(buffer), 1, img);
          }
          else
          {
              if (img != NULL)
              {
                  fwrite(buffer, sizeof(buffer), 1, img);
              }

          }
  }
  fclose(pf);
  fclose(img);
  return 0;
}

I used sizeof(buffer) thinking it would handle the last buffer which is supposedly less than 512 bytes. What is strange is that "card.raw" is 3,744,768 bytes long which is exactly (512x7314).

If I delete the feof condition, the code works for the first 49 pics, so I'm missing the last one which in the last batch.

If I try (feof(pf) != 0)) I also get 49 pics with the message "File size limit exceeded", but no segmentation fault.

I also tried this and got the exact same result:

int x = 512;



  while(fread(buffer, x, 1, pf) || (feof(pf) != 0))
  {
      if (sizeof(buffer) < 512)
      x = sizeof(buffer);


      if (buffer[0] == 0xff &&
          buffer[1] == 0xd8 &&
          buffer[2] == 0xff &&
          (buffer[3] & 0xf0) == 0xe0)
          {
              if (img != NULL)
              {
                  fclose(img);
                  ct += 1;
              }

              sprintf(fname,"%03i.jpg", ct);
              img = fopen(fname,"w");
              fwrite(buffer, x, 1, img);
          }
          else
          {
              if (img != NULL)
              {
                  fwrite(buffer, x, 1, img);
              }

          }
  }
  fclose(pf);
  fclose(img);
  return 0;
}
1
  • I don't think debug50 can do that but maybe share your code and we can help you as to why you're getting a seg fault?
    – Ojou Nii
    Jun 9, 2020 at 15:25

1 Answer 1

0

You could put a printf inside your loop with the relevant info and dump it to a file. That would be something like: ./myprogram some_parameter > file.txt. The last line will be printed just before the program crashes. You can use some if sentences inside the loop to reduce the number of lines. For example: 'if (counter > 7000) printf("my variable counter is: %d", counter);'.

You can also use 'debug50' stoping inside the previous 'if' sentence so you can debug from there.

1
  • Interesting. I'll try that. This thing has kept me awake for too long though. I'll try it tomorrow ;-)
    – Alfonse
    Jun 9, 2020 at 21:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .