Error while iterating over callee nodes in CallGraph

Despite making sure, it is not NULL, I still get a segmentation fault.

Code:

if((cgn->getFunction())==NULL)

continue;

errs()<getFunction()->getName()<<“\n”;

Error:

0 opt 0x00000000011d2302 llvm::sys::PrintStackTrace(_IO_FILE*) + 34

1 opt 0x00000000011d1eb4

2 libpthread.so.0 0x00007fea1847c330

3 libcgwalk.so 0x00007fea1767f596

4 opt 0x00000000010ddb45 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 709

5 opt 0x0000000000552724 main + 2452

6 libc.so.6 0x00007fea178a4f45 __libc_start_main + 245

7 opt 0x000000000056d865

Stack dump:

  1. Program arguments: opt -load build/proj/libcgwalk.so -cgwalk
  1. Running pass ‘Cgwalk’ on module ‘’.

Segmentation fault (core dumped)

Also, using getName() on the main function prints out an empty string - errs()<<cg[M.getFunction(“main”)]->getFunction()->getName()<<“\n”;
There seems to be some issue with CallGraph generation.

Despite making sure, it is not NULL, I still get a segmentation fault.

Code:
if((cgn->getFunction())==NULL)
        continue;
      errs()<<cgn->getFunction()->getName()<<"\n";

Error:
0 opt 0x00000000011d2302 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1 opt 0x00000000011d1eb4
2 libpthread.so.0 0x00007fea1847c330
3 libcgwalk.so 0x00007fea1767f596
4 opt 0x00000000010ddb45 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 709
5 opt 0x0000000000552724 main + 2452
6 libc.so.6 0x00007fea178a4f45 __libc_start_main + 245
7 opt 0x000000000056d865
Stack dump:
0. Program arguments: opt -load build/proj/libcgwalk.so -cgwalk
1. Running pass 'Cgwalk' on module '<stdin>'.
Segmentation fault (core dumped)

Also, using getName() on the main function prints out an empty string - errs()<<cg[M.getFunction("main")]->getFunction()->getName()<<"\n";
There seems to be some issue with CallGraph generation.

Given that your LLVM version is pretty old I'm not sure this will work:

The old/legacy pass manager is historically problematic when you cross
IR unit boundaries. I would just create the call graph from scratch. I
mean, use the CallGraph class but not the PM interface. That should work
in modern LLVM just fine, e.g., via llvm::CallGraph(Module &M).

To debug your segfault you might want to use a debug build of LLVM, even
for the stack dump hat would probably help a lot.