Using __errno2() to diagnose application problems

Use the __errno2() function when diagnosing problems in an application program. This function enables z/OS® XL C/C++ application programs to access additional diagnostic information, errno2 (errnojr), associated with errno. The __errno2 may be set by the z/OS XL C/C++ runtime library, z/OS UNIX callable services, or other callable services. The errno2 is intended for diagnostic display purposes only and is not a programming interface.

Note: Not all functions set errno2 when errno is set. In the cases where errno2 is not set, the __errno2() function may return a residual value. You may use the __err2ad() function to clear errno2 to reduce the possibility of a residual value being returned.

Figure 1 is an example of a routine using __errno2() and Figure 2 shows the sample output from that routine.

Figure 1. Example of a routine using __errno2() (AMODE 64)
#pragma runopts(posix(on))
#define _EXT
#include <stdio.h>
#include <errno.h>

int main(void) {
  FILE *f;
  f = fopen("testfile.dat", "r") 
  if (f==NULL) {
      perror("fopen() failed");
      printf("__errno2 = %08x\n", __errno2());
   }
   return 0;
}
Figure 2. Sample output of routine using __errno2() (AMODE 64)
fopen() failed: EDC5129I No such file or directory. (errno2=0x05620062)
__errno2 = 05620062  

Figure 3 is an example of a routine using the environment variable _EDC_ADD_ERRNO2 . Figure 4 shows the sample output from that routine. For more information about _EDC_ADD_ERRNO2, see z/OS XL C/C++ Programming Guidez/OS XL C/C++ Programming Guide.

Figure 3. Example of a routine using _EDC_ADD_ERRNO2 (AMODE 64)
#pragma runopts(posix(on))
#define _EXT
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main(void) {
  FILE *fp; 
  /* do NOT add errno2 to perror message */
  setenv("_EDC_ADD_ERRNO2", "0", 1);
  fp = fopen("testfile.dat", "r");
  if (fp == NULL) 
      perror("fopen() failed");
  return 0;
}
Figure 4. Sample output of a routine using _EDC_ADD_ERRNO2 (AMODE 64)
fopen() failed: EDC5129I No such file or directory. 

Figure 5 is an example of a routine using __err2ad() in combination with __errno2(). Figure 6 shows the sample output from that routine. For more information about __errno2() and __err2ad(), see z/OS XL C/C++ Runtime Library Reference.

Figure 5. Example of a routine using __err2ad() in combination with __errno2() (AMODE 64)
#pragma runopts(posix(on))
#define _EXT
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main(void) {
  FILE *f;  
  setenv("_EDC_ADD_ERRNO2", "0", 1);
  f = fopen("testfile.dat", "r");
  if (f == NULL) {
    perror("fopen() failed");
    printf("__errno2 = %08x\n", __errno2());
  }
  /* reset errno2 to zero */
  *__err2ad() = 0x0;
  printf("__errno2 = %08x\n", __errno2());
  f = fopen(*testfile.dat", "r");
  
  if (fp == NULL) {
    perror("fopen() failed");
    printf(*__errno2 = %08x\n", __errno2()); 
  }
  return 0;
}
Figure 6. Sample output of routine using __err2ad() in combination with __errno2() (AMODE 64)
fopen() failed: EDC5129I  No such file or directory.
__errno2 = 05620062
__errno2 = 00000000 
fopen() failed: EDC5129I  No such file or directory.
__errno2 = 05620062