(root)/
strace-6.5/
tests/
mincore.c
       1  /*
       2   * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
       3   * Copyright (c) 2016-2021 The strace developers.
       4   * All rights reserved.
       5   *
       6   * SPDX-License-Identifier: GPL-2.0-or-later
       7   */
       8  
       9  #include "tests.h"
      10  #include <stdio.h>
      11  #include <sys/mman.h>
      12  
      13  static void
      14  print_mincore(const unsigned int pages, void *const addr,
      15  	      const size_t size, unsigned char *const vec)
      16  {
      17  	if (mincore(addr, size, vec))
      18  		perror_msg_and_skip("mincore");
      19  
      20  	printf("mincore(%p, %zu, [", addr, size);
      21  	for (unsigned int i = 0; i < pages; ++i) {
      22  		if (i)
      23  			printf(", ");
      24  		if (i >= DEFAULT_STRLEN) {
      25  			printf("...");
      26  			break;
      27  		}
      28  		printf("%u", vec[i] & 1);
      29  	}
      30  	puts("]) = 0");
      31  }
      32  
      33  static void
      34  test_mincore(const unsigned int pages)
      35  {
      36  	const size_t page_size = get_page_size();
      37  	const size_t size = pages * page_size;
      38  	void *const addr = tail_alloc(size);
      39  	unsigned char *const vec = tail_alloc(pages);
      40  
      41  	mincore(addr, size, NULL);
      42  	printf("mincore(%p, %zu, NULL) = -1 %s (%m)\n",
      43  	       addr, size, errno2name());
      44  
      45  	print_mincore(pages, addr, size, vec);
      46  	if (size)
      47  		print_mincore(pages, addr, size - page_size + 1, vec);
      48  }
      49  
      50  int main(void)
      51  {
      52  	test_mincore(1);
      53  	test_mincore(2);
      54  	test_mincore(DEFAULT_STRLEN);
      55  	test_mincore(DEFAULT_STRLEN + 1);
      56  
      57  	puts("+++ exited with 0 +++");
      58  	return 0;
      59  }