Libvirt virDomainGetInfo() 导致分段错误
Libvirt virDomainGetInfo() results in segmentation fault
我正在按照 Libvirt 文档中的示例列出所有使用 virConnectListAllDomains. The function is working, and it shows the correct number of active domains. Now I am trying to use virDomainGetInfo 的域来打印有关域的信息,但我遇到了分段错误。下面是代码,下面是程序的输出
#include <stdio.h>
#include <libvirt/libvirt.h>
#include <stdlib.h>
int main(int argc, char** argv) {
virConnectPtr conn;
virDomainPtr *domains;
unsigned int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE;
int ret;
conn = virConnectOpen("qemu:///system");
if (conn == NULL)
{
fprintf(stderr, "Failed to open connection to qemu:///system\n");
}else
{
printf("Sucessfully connected\n");
}
ret = virConnectListAllDomains(conn,&domains,flags);
if(ret < 0 )
{
fprintf(stderr, "Failed to Get the List of Domains\n");
}else
{
printf("I have domain info of %d domains\n", ret );
}
for( int i = 0; i < ret; i++)
{
virDomainInfoPtr domainInfo;
int virRet;
virRet = virDomainGetInfo(domains[i], domainInfo);
if(virRet < 0 )
{
fprintf(stderr, "Failed to Get the Domain Info\n");
}else
{
printf("State : %c",domainInfo->state);
printf("maxMem : %lu",domainInfo->maxMem);
printf("memory : %lu",domainInfo->memory);
printf("nVRCPU : %u",domainInfo->nrVirtCpu);
printf("cpuTime: %llu",domainInfo->cpuTime);
}
virDomainFree(domains[i]);
}
free(domains);
virConnectClose(conn);
return 0;
}
输出
Sucessfully connected
I have domain info of 1 domains
Segmentation fault
传递要填写的结构,
变化:
virDomainInfoPtr domainInfo;
int virRet;
virRet = virDomainGetInfo(domains[i], domainInfo);
收件人:
virDomainInfo domainInfo;
int virRet;
virRet = virDomainGetInfo(domains[i], &domainInfo);
我正在按照 Libvirt 文档中的示例列出所有使用 virConnectListAllDomains. The function is working, and it shows the correct number of active domains. Now I am trying to use virDomainGetInfo 的域来打印有关域的信息,但我遇到了分段错误。下面是代码,下面是程序的输出
#include <stdio.h>
#include <libvirt/libvirt.h>
#include <stdlib.h>
int main(int argc, char** argv) {
virConnectPtr conn;
virDomainPtr *domains;
unsigned int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE;
int ret;
conn = virConnectOpen("qemu:///system");
if (conn == NULL)
{
fprintf(stderr, "Failed to open connection to qemu:///system\n");
}else
{
printf("Sucessfully connected\n");
}
ret = virConnectListAllDomains(conn,&domains,flags);
if(ret < 0 )
{
fprintf(stderr, "Failed to Get the List of Domains\n");
}else
{
printf("I have domain info of %d domains\n", ret );
}
for( int i = 0; i < ret; i++)
{
virDomainInfoPtr domainInfo;
int virRet;
virRet = virDomainGetInfo(domains[i], domainInfo);
if(virRet < 0 )
{
fprintf(stderr, "Failed to Get the Domain Info\n");
}else
{
printf("State : %c",domainInfo->state);
printf("maxMem : %lu",domainInfo->maxMem);
printf("memory : %lu",domainInfo->memory);
printf("nVRCPU : %u",domainInfo->nrVirtCpu);
printf("cpuTime: %llu",domainInfo->cpuTime);
}
virDomainFree(domains[i]);
}
free(domains);
virConnectClose(conn);
return 0;
}
输出
Sucessfully connected
I have domain info of 1 domains
Segmentation fault
传递要填写的结构, 变化:
virDomainInfoPtr domainInfo;
int virRet;
virRet = virDomainGetInfo(domains[i], domainInfo);
收件人:
virDomainInfo domainInfo;
int virRet;
virRet = virDomainGetInfo(domains[i], &domainInfo);