linux-0.11调试教程,fflush()函数实现分析和FILE类型定义

来源:互联网 发布:ecs网络拓扑图图标 编辑:程序博客网 时间:2024/06/08 13:12

fflush()

    __bwrflush()
        __iowrite()
            write()



typedef struct __iobuf {

  __stdiobuf_t *__rptr;            /* pointer into read buffer */
  __stdiobuf_t *__rend;            /* point at end of read buffer */
  __stdiobuf_t *__wptr;            /* pointer into write buffer */
  __stdiobuf_t *__wend;            /* point at end of write buffer */
  __stdiobuf_t *__base;            /* base of buffer */
  __stdiosize_t __bufsiz;        /* size of buffer */
  short __flag;                /* flags */
  char __file;                /* channel number */
  __stdiobuf_t __buf;            /* small buffer */
  int (*__filbuf) _STDIO_P_((struct __iobuf *));      /* fill input buffer */
  int (*__flsbuf) _STDIO_P_((int, struct __iobuf *)); /* flush output buffer */
  int (*__flush) _STDIO_P_((struct __iobuf *));    /* flush buffer */
  struct __iobuf *__next;        /* next in chain */
} FILE;



__bwrflush处下断点的情形:

第一块是断点,第二块是堆栈,0x442a8是FILE * 指针,

第三块是FILE*类型结构,缓冲区地址是0x4840c。



/* Flush a write buffer

 *
 * __bwrflush is a function that flushes the output buffer.
 */
static int __bwrflush F1(register FILE *, fp)

{
  register __stdiosize_t length;        /* bytes to write */

  if (! TESTFLAG(fp, _IOERR)
#ifndef        OPEN3
      && (! TESTFLAG(fp, _IOAPPEND) || lseek(fileno(fp), 0L, SEEK_END) != -1L)
#endif
     ) {
    length = BYTESINWRITEBUFFER(fp);
    INITWRITEBUFFER(fp);
    if (__iowrite(fileno(fp), (char *) fp->__base, length) == length)
      return 0;
  }
  SETFLAG(fp, _IOERR);
  return EOF;
}



__iowrite处下断点时的情形:

__iowrite之后下断点,会看到虚拟机中出现了字符f




附上__iowrite的定义:

#include "stdiolib.h"

/*LINTLIBRARY*/

__stdiosize_t __iowrite F3(int, fd, char *, p, __stdiosize_t, n)

{
  register int wrote;            /* bytes written by write call */
  register __stdiosize_t wb;        /* byte to write each call */
  __stdiosize_t w;            /* total bytes written */

  for (w = 0; ; p += wrote) {
    wb = n;
    if (wb > INT_MAX)
      wb = INT_MAX;
    do
      wrote = write(fd, p, (unsigned int) wb);                 //注意这里,终于看到write()函数了!!!
    while (wrote == -1 && (errno == EINTR || errno == EAGAIN));
    if (wrote == -1)
      break;
    w += wrote;
    if ((n -= wrote) == 0)
      break;
  }
  return w;
}


原创粉丝点击