View on GitHub

GDB VLA support

Variable Length Array support for C99 and Fortran

This repository hosts the GDB Variable Length Array (VLA) implementation for C99 and Fortran (dynamic arrays).


A variable length array (VLA) is an array, whose size resp. length is computed at run time and not at compile time for languages, which have automatic storage duration. This means that a variable will be allocated and deallocated automatically when the program flow enters and leaves its context. VLA's will be also called dynamic arrays in the Fortran area. In the past VLA's were just an GCC extension and later the concept was included into C99. Although C99 supports dynamic arrays, C++ doesn't, also not with the latest standard C++0x.

This project will add VLA support for C99 and Fortran to GDB. This means that it is not possible to get the type or the value of a VLA using GDB in C and Fortran.


A basic C99 example of a variable length array, whose size will be computed at run-time:

void func(int n) {
  int array[n];

Example for a VLA in Fortran:

program VLA
  implicit none
  real, allocatable :: array(:)
  allocate (array(100))
end program VLA

VLA approach

Dwarf allows certain attributes e.g upper/lower bound, allocated/associated status to be computed dynamically. To support this dwarf features with the current gdb type-system, this project will introduce dynamic type properties, which will be "normalized" when requested. This means types with dynamic properties are converted to types with static properties and can then be handled by GDB like before this series. To convert a type with dynamic properties into one with static properties access to the inferior memory is required. Therefore we hooked into the following value constructors:

as they require an inferior address in addition to a type to instantiate a value. If the passed type has dynamic properties, we create a copy of it and resolve the dynamic values into static ones using the copy. This ensures that there will be no side effects, which can occur when modifying the original type. The copy is also required to not overwrite the dynamic attributes, which might be needed by a later operation. Given the following code snippet:
struct value *val = value_at (my_vla_type, at_address);
this was always true:
TYPE_LENGTH (value_type (val)) == TYPE_LENGTH (my_vla_type)
This is not the case anymore after applying this patch series. Type normalization is done in the mentioned value constructors and might change the value type including attributes, such as the length.

Structure of the repository

The repository is a clone of the GDB repository, which will be updated on a non-regular basis. The main development of the VLA feature is done on branches.

Current active branches: