So I'm a noob when it comes to this sort of stuff.
I'm struggling to compile a climate model on macOS and I've boiled it down to what is going on here:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <sys/resource.h>
#include <sys/syscall.h>
static pid_t gettid(void)
{
return syscall(__NR_gettid);
}
/*
* Returns this thread's CPU affinity, if bound to a single core,
* or else -1.
*/
int get_cpu_affinity(void)
{
cpu_set_t coremask; /* core affinity mask */
CPU_ZERO(&coremask);
if (sched_getaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
fprintf(stderr,"Unable to get thread %d affinity.
%s\n",gettid(),strerror(errno));
}
int cpu;
int first_cpu = -1; /* first CPU in range */
int last_cpu = -1; /* last CPU in range */
for (cpu=0;cpu < CPU_SETSIZE;cpu++) {
if (CPU_ISSET(cpu,&coremask)) {
if (first_cpu == -1) {
first_cpu = cpu;
} else {
last_cpu = cpu;
}
}
}
return (last_cpu == -1) ? first_cpu : -1;
}
int get_cpu_affinity_(void) { return get_cpu_affinity(); } /* Fortran interface */
/*
* Set CPU affinity to one core.
*/
void set_cpu_affinity( int cpu )
{
cpu_set_t coremask; /* core affinity mask */
CPU_ZERO(&coremask);
CPU_SET(cpu,&coremask);
if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
fprintf(stderr,"Unable to set thread %d affinity. %s\n",gettid(),strerror(errno));
}
}
void set_cpu_affinity_(int *cpu) { set_cpu_affinity(*cpu); } /* Fortran interface */
When compiling I get a couple of errors:
First - identifier "cpu_set_t" is undefined, Second - identifier "CPU_SETSIZE" is undefined
I've done my googling and it seems to me that sched_getaffinitiy(), cpu_set_t and perhaps some other things are not present in macOS.
With all kinds of C programming I'm really out of my depth. I was wondering if anyone here knows of an alternative way of doing this for macOS and how I could go about doing that.
I've attached the full error report below.
Kind Regards,
Neil :)
Complete error report:
(python2) salvare:MASTERS Neil$ python run.py
Working directory for exp 'playground' already exists
RRTM compilation disabled. Namelist set to gray radiation.
Writing path_names to '/Users/Neil/MASTERS/ISCA_TEMP/playground/path_names'
Running compiler
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 21: module: command not found
loadmodules
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 3: module: command not found
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 4: module: command not found
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 5: module: command not found
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 6: module: command not found
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 23: module: command not found
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 24: ulimit: stack size: cannot modify limit: Operation not permitted
./compile_mppn.sh: line 13: module: command not found
./compile_mppn.sh: line 14: module: command not found
./compile_mppn.sh: line 15: module: command not found
- mppnccombine.c:162:24: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
sprintf(outfilename,argv[outputarg]); outlen=strlen(outfilename);
^~~~~~~~~~~~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^~~~~~~~~~~
mppnccombine.c:162:24: note: treat the string as an argument to avoid this
sprintf(outfilename,argv[outputarg]); outlen=strlen(outfilename);
^
"%s",
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^
1 warning generated.
ln: /Users/Neil/MASTERS/ISCA_TEMP/playground/exec/mppnccombine.x: File exists
Makefile is ready.
mpicc -Duse_libMPI -Duse_netCDF -Duse_LARGEFILE -DINTERNAL_FILE_NML -DOVERLOAD_C8 -DRRTM_NO_COMPILE -I/usr/local/include -D__IFC -c /Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c
......................................................................................................................../Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(35): error: identifier "__NR_gettid" is undefined
eturn syscall(__NR_gettid);
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(44): error: identifier "cpu_set_t" is undefined
cpu_set_t coremask; /* core affinity mask */
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(47): error: identifier "cpu_set_t" is undefined
if (sched_getaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(54): error: identifier "CPU_SETSIZE" is undefined
for (cpu=0;cpu < CPU_SETSIZE;cpu++) {
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(75): error: identifier "cpu_set_t" is undefined
cpu_set_t coremask; /* core affinity mask */
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(79): error: identifier "cpu_set_t" is undefined
if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
^
compilation aborted for /Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c (code 2)
make: *** [affinity.o] Error 2
ERROR: mkmf failed for fms_moist
CRITICAL - Compilation failed.
Traceback (most recent call last):
File "run.py", line 25, in <module>
exp.compile()
File "/Users/Neil/MASTERS/Isca/src/extra/python/gfdl/experiment.py", line 298, in compile
raise e
sh.ErrorReturnCode_1:
RAN: /bin/bash /Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh
STDOUT:
STDERR:
Your googling put you on the right track - sched_getaffinity() and the cpu_set_t structure that it uses are linux-specific, and are not available on all platforms. Mac OSX in particular seems to be missing them.
However, there are alternatives. I found a blog post from someone porting code to OSX who found an alternative using sysctlbyname(). The code on that site reimplements sched_getaffinity() on Mac OSX by requesting the machdep.cpu.core_count, and using that to build their own version of cpu_set_t.
Related
I am learning to write apps on ESP32 platform. When I was trying to compile my code, I got this error from linker:
undefined reference to `Serial_Init'
Where Serial_Init is a function declared in a file serial_cli.h which is in the same directory and workspace. What's more, I declared some macros there, and can use them no problem in my code, so I really don't understand where the error comes from.
Here's serial_cli.h:
#ifndef _SERIAL__H_
#define _SERIAL__H_
#include "driver/uart.h"
#define SERIAL_PORT UART_NUM_1
#define RTS_SIG_PINOUT UART_PIN_NO_CHANGE
#define CTS_SIG_PINOUT UART_PIN_NO_CHANGE
/**
* #brief This function initialises serial communication with PC
* #param uart_config_t type pointer to structure containing needed parameters
* #param int size of RX and TX buffer (in bytes)
* #param QueueHandle_t type pointer to structure containing UART queue
*/
void Serial_Init(uart_config_t*, const int, QueueHandle_t*);
#endif /* _SERIAL_H_ */
And here's serial_cli.c:
#include "serial_cli.h"
void Serial_Init(uart_config_t* uart_config, const int buffer_size, QueueHandle_t* queue)
{
ESP_ERROR_CHECK(uart_param_config(SERIAL_PORT, uart_config));
ESP_ERROR_CHECK(uart_set_pin(SERIAL_PORT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, RTS_SIG_PINOUT, CTS_SIG_PINOUT));
ESP_ERROR_CHECK(uart_driver_install(SERIAL_PORT, buffer_size, buffer_size, 10, queue, 0));
}
And finally, main body of the app:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "serial_cli.h"
#include "string.h"
void app_main(void)
{
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
.rx_flow_ctrl_thresh = 122
};
QueueHandle_t queue;
Serial_Init(&uart_config, 1024, &queue);
char* test_str = "This is a test string.\n";
while(1) {
uart_write_bytes(SERIAL_PORT, (const char*)test_str, strlen(test_str));
vTaskDelay(500);
}
}
Below I also include complete output that I get from console:
D:\Tools\ESP_IDF\examples\get-started\blink>idf.py build
Executing action: all (aliases: build)
Running ninja in directory d:\tools\esp_idf\examples\get-started\blink\build
Executing "ninja all"...
[1/8] Performing build step for 'bootloader'
ninja: no work to do.
[5/6] Linking CXX executable blink.elf
FAILED: blink.elf
cmd.exe /C "cd . && D:\Tools\ESP_IDF_container\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address #CMakeFiles\blink.elf.rsp -o blink.elf
&& cd ."
d:/tools/esp_idf_container/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(blink.c.obj):(.literal.app_main+0x8): undefined reference to `Serial_Init'
d:/tools/esp_idf_container/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(blink.c.obj): in function `app_main':
d:\tools\esp_idf\examples\get-started\blink\build/../main/blink.c:38: undefined reference to `Serial_Init'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
Thanks in advance for any feedback.
You need to add "serial_cli.c" to your main/CMakeLists.txt. Something like this:
idf_component_register(
SRCS
"blink.c"
"serial_cli.c"
...
See details in ESP IDF documentation
Code:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
Error:
Undefined symbols for architecture x86_64: "_clone", referenced
from:
_main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Linux doesn't prefix symbols with a leading _ so you're not using Linux.
But the clone(2) system call is Linux-specific, according to its man page.
clone() is Linux-specific and should not be used in programs intended
to be portable.
Probably you're using OS X or something. And you're compiling as C, so calling an un-declared function isn't a compile-time error (just a big warning). This is why it's a linker error instead of a compile-time error (and you ignored compiler warnings.)
And BTW, #define _GNU_SOURCE after including header files is pointless. You have to define feature-request macros before including headers to get them to define prototypes for GNU-only functions in cases where that's not already the default.
I'm attempting to write a small program in C that will open and read from a Berkeley 4.2 hash DB on a FreeBSD 9.1 system for testing, but I can't get it to compile. This is the first time I've written anything in C and compiled from a command line so I'm probably missing one thing that'll get it working, I don't know.
After searching all over and looking at documentation and source code on github, this is what I've got so far:
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <db.h>
int main()
{
DB * mydb;
u_int32_t open_flags = DB_RDONLY;
int ret;
ret = db_create(&mydb, NULL, 0);
if (ret != 0) {
printf("Error creating DB structure!");
return 1;
}
ret = mydb->open(mydb, NULL, "bsddb-py", NULL, DB_HASH, open_flags, 0);
if (ret != 0) {
printf("Error opening DB file!");
return 2;
}
mydb->close(mydb, 0);
}
I compile with this:
cc -ldb-4.2 db_test.c
And get this:
db_test.c: In function 'main':
db_test.c:20: error: 'DB_RDONLY' undeclared (first use in this function)
db_test.c:20: error: (Each undeclared identifier is reported only once
db_test.c:20: error: for each function it appears in.)
db_test.c:29: error: 'DB' has no member named 'open'
db_test.c:35: error: too many arguments to function 'mydb->close'
Apparently the compiler is hung up on using Berkeley 1.85 (dbopen and such) and it won't budge?
It looks like #include <db.h> will provide an interface to Berkeley 1.85 because that's what's installed by default on FreeBSD. We have Berkeley 4.2 installed via ports, and to avoid conflicts, the header that interfaces with 4.2 is put elsewhere - I was referencing the right library but not the right header.
So, I changed the include to:
#include <db42/db.h>
...and compiled with...
cc -I/usr/local/include/ -L/usr/local/lib/ -ldb-4.2 db_test.c -o db_test
Running the above source with that modification produced no visible output, which means it worked!
As a newbie to it, BSD is weird.
DB_RDONLY is contained in some header file that you are not #including. That should take care of all the line 20 errors.
Line 29: a DB struct apparently doesn't have a member named open. Recheck the struct/maybe you forgot to include the file that that struct is declared in.
35: Seems like the function close doesn't take 2 arguments. Recheck this in the header file/make sure you included the header file.
Not sure if anyone has any ideas here, I haven't seen this before. I'm writing a stub to test out my kernel module, when I check the value of the command in userspace I get a different value vs. when I take a look at the in kernel space.
Part of the stub:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "ain.h"
#include "ain_ioctl.h"
#define AI_DEVICE "/dev/ain"
void main()
{
int fd, error, ioctl_par = 0;
char* dev;
long ret;
dev = AI_DEVICE;
printf("Starting driver test\n");
fd = open(dev, O_RDWR);
if (fd < 0) {
/* Failed to open -> Print error-message and exit */
printf("%s failed to open, error: %s\n", dev, strerror(errno));
}
printf("Doing the IOCTL now... cmd: %d\n", AIN_IOC_GET_AN0_CONF);
fflush(stdout);
ret = ioctl(fd, AIN_IOC_GET_AN0_CONF, &ioctl_par);
The ain_ioctl.h file:
#define AIN_IOC_MAGIC 'e'
#define AIN_IOC_GET_AN0_CONF _IOR(AIN_IOC_MAGIC, 46, int)
The ioctl routine in the kernel:
int ain_ioctl (struct inode * inodep, struct file * filp, unsigned int cmd, unsigned long arg)
{
printk("In the ain_ioctl function, cmd: %d. type: %d, dir: %d, nr: %d, size: %d\n",
cmd, _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
printk("Testing against command: %d. type: %d, dir: %d, nr: %d, size: %d\n",
AIN_IOC_GET_AN0_CONF, _IOC_TYPE(AIN_IOC_GET_AN0_CONF), _IOC_DIR(AIN_IOC_GET_AN0_CONF),
_IOC_NR(AIN_IOC_GET_AN0_CONF), _IOC_SIZE(AIN_IOC_GET_AN0_CONF));
Now I would have expected identical output in the user space print as in the kernel. And in the first set of prints in the kernel to the second. However that's not what I'm seeing...
Output:
mike#linux-4puc:~> ./a.out
Starting driver test
Doing the IOCTL now... cmd: -2147195602
mike#linux-4puc:~> dmesg | tail
[75253.205136] In the ain_ioctl function, cmd: -1078168112. type: 117, dir: 2, nr: 208, size: 16316
[75253.205140] Testing against cmd: -2147195602. type: 101, dir: 2, nr: 46, size: 4
Anyone have any ideas on why my command is acting differently when I pass it to the kernel via the ioctl command vs when I just check the values by hard coding them (as I am doing in my prints)?
The only warnings I'm seeing when I build seem nothing to do with the ioctl calls:
makedepend: warning: ignoring option -Wall
makedepend: warning: ignoring option -Wall
makedepend: warning: ain.c (reading /usr/src/linux/include/linux/compiler-gcc.h), line 94: incomplete include == "#include gcc_header(__GNUC__)"
makedepend: warning: ain.c (reading /usr/src/linux/include/linux/string.h, line 13): cannot find include file "stdarg.h"
Thanks.
The -1078168112 (why aren't you printing these in hex?) looks like a stack pointer. Possibly &ioctl_par. This suggests that your ioctl method is receiving different parameters than you expected.
In the current kernel source I see ioctl methods taking 3 parameters, not 4. The 4-argument ioctl seems to be an older interface.
Do you get any warnings during the module compilation? Pay attention to them!
Alan Curry's answer was not the complete "correct" answer, but it lead me to the solution. The hex value of the command was way off so I took a look at other ioctl calls in the kernel.
The system I have is based off an older 2.4X kernel, and I'm updating it for 3.1. The issue here is the parameter list for the ioctl call. Having the inode pointer in the parameter list was causing the problem as it was taking the file pointer to be the command.
Proper solution:
long ain_ioctl (struct file * filp, unsigned int cmd, unsigned long arg) {
...
I have been coming across errors in compilation of my signal handler program written in C language with gcc in displaying the dumped register values after occurance of Segmentation fault. When i tried to access it using the code:
void print_registers(FILE *fd, ucontext_t *ctx, bool fpu = false)
{
const char *flags_str[] = {
"CF", 0, "PF", 0, "AF", 0, "ZF", "SF", "TP", "IF", "DF",
"OF", 0, 0, "NT", 0, "RF", "VM", "AC", "VIF", "VIP", "ID"
};
greg_t *regs = ctx->uc_mcontext.gregs;
void *eip[1] = { (void*)regs[REG_EIP] };
char **symbol = backtrace_symbols(eip, 1);
fprintf(fd, "Registers:\neip is at ");
backtrace_symbols_fd(eip, 1, fd->_fileno);
size_type flags = regs[REG_EFL];
fprintf(fd, "eflags: %x [ ", flags);
for (size_type i = 0; i < sizeof(flags_str) / sizeof(flags_str[0]); ++i) {
if (!flags_str[i]) continue;
if (flags & (1 << i)) fprintf(fd, "%s ", flags_str[i]);
}
size_type iopl = (flags & 0x3000) >> 12;
fprintf(fd, "] iopl: %i\n"
"eax: %08x\tebx: %08x\tecx: %08x\tedx: %08x\n"
"esi: %08x\tedi: %08x\tebp: %08x\tesp: %08x\n"
"cs: %04x\tgs: %04x\tfs: %04x\n"
"ds: %04x\tes: %04x\tss: %04x\n",
iopl,
regs[REG_EAX], regs[REG_EBX], regs[REG_ECX], regs[REG_EDX],
regs[REG_ESI], regs[REG_EDI], regs[REG_EBP], regs[REG_ESP],
regs[REG_CS], regs[REG_GS], regs[REG_FS],
regs[REG_DS], regs[REG_ES], regs[REG_SS]);
}
}
I tried the code by adding
#include<sys/ucontext.h>
as well as
#define _GNU_SOURCE
#ifndef REG_EIP
#define REG_EIP 0x23b46F
#endif
But, the error appearing is:
‘REG_EIP’ undeclared (first use in this function)
(Each undeclared identifier is reported only once for each function it appears in.)
and the error is appearing for all registers
I tried many documents...but couldn't get the solution.
Can anybody share the details for resolving this error.
Advance thanks to all repliers
I believe you should either have #define _GNU_SOURCE as the first line of your source file, or better put -D_GNU_SOURCE in your CFLAGS (on the command line). Then make sure you include <signal.h> and <ucontext.h>.
Try defining __USE_GNU before including <ucontext.h:
#define __USE_GNU
#include <ucontext.h>
You don't need to include <sys/ucontext.h> explicitly, <ucontext.h> will do that.
Try using 32 bit as those are 32 bit mode values. gcc -m32 should solve this.
For me, this was resolved with:
yum remove openssl-devel.x86_64
yum install openssl-devel.i686
on CentOS 6.4 (x86_64)
Hope this helps.