Melt 4 tablespoons of butter in a large frypan over low heat. Add the onions; cook and stir until they turn a deep brown, about 10 minutes. Stir in the garlic and cook for 2 minutes, until fragrant. Mix in the chicken stock and rosemary and increase the heat to medium-high. Cook until reduced by 1/3, about 8 minutes.
Meanwhile, bring a large pot of lightly salted water to a boil. Add the spaghetti and cook until tender, about 8 minutes. Drain, then add pasta to the sauce in the frypan. Stir in the remaining butter and Parmesan cheese. Season with salt and pepper.